I am trying to add the field FROM in my matlab function to send an email with outlook.
This function works (without the from):
function sendolmail(to,subject,body,attachments, from)
%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');
mail = h.CreateItem('olMail');
mail.Subject = subject;
mail.To = to;
mail.BodyFormat = 'olFormatHTML';
mail.HTMLBody = body;
% THIS PART DOES NOT WORK
if nargin ==5
mail.From = from;
end
% 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;
However, when I add from then I get the error:
No public property From exists for class Interface.00063034_0000_0000_C000_000000000046.
As already stated, there is no From
attribute in MailItem
objects. There are many attributes referring to the sender: Sender
, SenderEmailAddress
, SenderEmailType
, SenderName
... but all of them, except Sender
, are read-only. This means they cannot be set, and you must rely uniquely on the Sender
property, which accept object instances of type AddressEntry.
I'm not sure that this will work, because such mechanic would be easily abused by malicious users... but you can try the following:
if (nargin == 5)
recipient = h.Session.CreateRecipient(from);
mail.Sender = recipient.AddressEntry;
end