Can I send e-mail through MATLAB using Microsoft Outlook?

296 views (last 30 days)
I want to send e-mail through MATLAB, but the SENDMAIL command does not save the outgoing message for future reference. I would like to send mail through MS Outlook so that I may reference it later.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 22 Feb 2017
Edited: MathWorks Support Team on 22 Feb 2017
The ActiveX/COM interface can be used to control MS Outlook from MATLAB and send e-mails while using all the features available in MS Outlook. The function below is an example of how this can be done. Note that this function works almost the same as SENDMAIL.
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');
mail = h.CreateItem('olMail');
mail.Subject = subject;
mail.To = to;
mail.BodyFormat = 'olFormatHTML';
mail.HTMLBody = body;
% Add attachments, if specified.
if nargin == 4
for i = 1:length(attachments)
% Send message and release object.
Examples of how to use this function would be the following:
The first will example includes a link, the second a picture.
sendolmail('','Test link','Test message including a <A HREF=<>>Link</A>', {'C:\attachment1.txt' 'C:\attachment2.txt'});
sendolmail('','Test image',
'Test message including an image <img src="">')
Alternatively, you can use SENDMAIL but BCC the e-mail to yourself so that you may retain a copy.

Sign in to comment.

More Answers (3)

Yucheng on 2 Sep 2014
Edited: David on 17 Jan 2017
Thanks for sharing this, the process to send images in the email main body is exactly what I am after.
However, there is one minor error. In the example, there is a redundant symbol "<" after the img src=" code, and the correct one should be:
'Test message including an image <img src="">>')
  1 Comment
Vandana Ravichandran
Vandana Ravichandran on 22 Feb 2017
Thank you for pointing out the error, I have updated the answer to include your suggested change

Sign in to comment.

Shantanu on 29 Oct 2015
Hi, I am not able to send an email using above mentioned code for multiple email id. Please can you provide an example for how to send an email for multiple email id?
Virane Gahlot
Virane Gahlot on 14 May 2018
Really a very simple approach but u need not to convert the sender addres to string u can directly send the string followed by single qote in function argument.

Sign in to comment.

christina on 18 Sep 2017
Hi, thank you very much for this code. I would like to send a standardized mail to some recipients which should have at least some formating as paragraphs and maybe also font sizes. Is this possible? Thank you!

Community Treasure Hunt

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

Start Hunting!