How To Send E-Mail From MATLAB

Yup! With a single function you can send E-Mail from within MATLAB. Like this:

% Send the email
sendmail('', 'Mail from MATLAB', ...
    'Hello Jimmy! This is a test mesage from MATLAB!', ...
    {'sub_folder/signals.m', 'system.mdl'})

The usage of sendmail function is quite simple: Just tell MATLAB the recipient, subject and your message. If you want, you can also attach some files to the E-Mail, use curly brackets to include all the attachments.

Send E-Mail

Image comes from .:I abutaleb I:.@Flickr

1 Minute Configuration for Sendmail

Before we can send E-Mails, we should tell MATLAB something about our own E-Mail settings.

% Define these variables appropriately:
mail_address = '';            % My E-Mail Address
user_name = 'linli';                          % My Username
password = 'likeiwilltellyoumypassword';      % My E-Mail Password
smtp_server = ';               % My SMTP Server
% Then this code will set up the preferences properly:
setpref('Internet', 'E_mail', mail_address);
setpref('Internet', 'SMTP_Username', user_name);
setpref('Internet', 'SMTP_Password', password);
setpref('Internet', 'SMTP_Server', smtp_server);
props = java.lang.System.getProperties;
props.setProperty('mail.smtp.auth', 'true');
props.setProperty('mail.smtp.socketFactory.class', '');
props.setProperty('mail.smtp.socketFactory.port', '465');

It’s done! Now feel free to sendmail from MATLAB.

Configure Your Gmail – MATLAB

If you prefer to use Gmail, here is the code ready for you.

setpref('Internet', 'E_mail', '');
setpref('Internet', 'SMTP_Username', '');
setpref('Internet', 'SMTP_Password', 'YOURPASSWORD');
setpref('Internet', 'SMTP_Server', '');
props = java.lang.System.getProperties;
props.setProperty('mail.smtp.socketFactory.class', '');
props.setProperty('mail.smtp.socketFactory.port', '465');

You can copy & paste it into you startup.m, so you can sendmail right after you start your MATLAB. If you don’t know what is a startup.m file, take a look at my post: Set Your Customized Startup File for MATLAB.


Category: MATLAB/Simulink.

9 Responses to “How To Send E-Mail From MATLAB”

Leave a Reply