writing outlook email add a signature

11 次查看(过去 30 天)
Hello everybody,
thanks in advance for your help. I am writing outlook email through Matlab;
h = actxserver('outlook.Application');
mail = h.CreateItem('olMail');
mail.Subject = Subject;
mail.BodyFormat = 'olFormatHTML';
mail.HTMLBody = BodyText;
mail.To = emailadress;
mail.Send;
h.release;
BodyText is on HTML format. I am trying to add a signature that already exists in Outlook but I dont how to do it. Anyone has a solution ?
If not possible maybe I can add my signature at the end of BodyText but I don't know how to add a logotype image that will be correctly displayed in the sent email (given that BodyText is created by a word document) ?
Thanks in advance for you help
Arnaud

采纳的回答

Cris LaPierre
Cris LaPierre 2018-12-28
It appears to depend on if a signature is added by default to your emails or not. You can see one way of accomplishing it on stackexchange here.
Another good place to look for help is apparently VBA discussion forums. Here's a good one. Of course, you need to adapt the VBA to run in MATLAB, but it's pretty similar.
Since I don't have a default signature in my emails, I thought I'd take a stab coding something to add my external signature to a MALTAB generated outlook message. This worked for me. I'm borrowing heavily from the 2nd link I shared to get the signature, as well as this MATLAB Answers post on sending mail through Outlook. I've only had to make a couple simple modifications.
function sendolmail(to,subject,body,attachments)
%Sends email using MS Outlook. The format of the function is
%Similar to the SENDMAIL command.
% Create object and set parameters.
h = actxserver('outlook.Application');
% Extract signature html. In Windows 10, the htm file is located here
% C:\Users\<UserName>\AppData\Roaming\Microsoft\Signatures
sFile = 'C:\Users\****\AppData\Roaming\Microsoft\Signatures\External.htm';
sig = getSig(h,sFile);
mail = h.CreateItem('olMail');
mail.Subject = subject;
mail.To = to;
mail.BodyFormat = 'olFormatHTML';
mail.HTMLBody = [body '<br><br>' sig];
% Add attachments, if specified.
if nargin == 4
for i = 1:length(attachments)
mail.attachments.Add(attachments{i});
end
end
% Send message and release object.
mail.Send;
h.release;
end
% Modified GetBoiler function from Ron de Bruin's Excel Automation post
function sig = getSig(h, sFile)
fso = h.CreateObject('Scripting.FileSystemObject');
ts = fso.GetFile(sFile).OpenAsTextStream(1, -2);
sig = ts.ReadAll;
ts.Close;
end
  14 个评论
Cris LaPierre
Cris LaPierre 2019-1-1
I think you'll need to use the approach that captures the signature from a message (example 1 in Ron de Bruin's page. It looks like the following html code is being used to add a line
<p class=MsoNormal><o:p>&nbsp;</o:p>
You want to be careful to only remove the 2 extra lines, and not anything else. I can use strrep with the following code to send myself an email using a signature with an image:
mail.Display;
sig = mail.HTMLBody;
old = '<p class=MsoNormal><o:p>&nbsp;</o:p></p><p class=MsoNormal><o:p>&nbsp;</o:p></p><p class=MsoNormal><a name="_MailAutoSig">';
new = '<p class=MsoNormal><a name="_MailAutoSig">';
sig = strrep(sig,old,new);
Arnaud Bitoun
Arnaud Bitoun 2019-1-1
Thank you very much Cris, it seems that this code is working for me;
h = actxserver('outlook.Application');
mail = h.CreateItem('olMail');
% Signature with image
mail.Display;
sig = mail.HTMLBody;
ToDelete='<o:p>&nbsp;</o:p></span></p>';
sig = erase(sig,ToDelete);
its not the more elegant code we have seen but this DIY is working,
Thanks again for you help Cris,
Arnaud

请先登录,再进行评论。

更多回答(0 个)

产品


版本

R2016b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by