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_of_jimmy@somedomain.com', '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 = 'linli@obasic.net';            % My E-Mail Address
user_name = 'linli';                          % My Username
password = 'likeiwilltellyoumypassword';      % My E-Mail Password
smtp_server = 'mail.obasic.net;               % 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', 'javax.net.ssl.SSLSocketFactory');
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', 'YOURMAIL@gmail.com');
setpref('Internet', 'SMTP_Username', 'YOURMAIL@gmail.com');
setpref('Internet', 'SMTP_Password', 'YOURPASSWORD');
setpref('Internet', 'SMTP_Server', 'smtp.gmail.com');
props = java.lang.System.getProperties;
props.setProperty('mail.smtp.auth','true');
props.setProperty('mail.smtp.socketFactory.class', 'javax.net.ssl.SSLSocketFactory');
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.

Related posts:

  1. How To Twitter From MATLAB
  2. What’s on Your MATLAB Shortcut Toolbar
  3. Better tic & toc in MATLAB
  4. Exclamation Mark Your MATLAB?
  5. How To Make Magnifier Plot in MATLAB

Category: MATLAB/Simulink.

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

Leave a Reply