MATLAB Answers

How do I use SENDMAIL to send email from MATLAB 7.2 (R2006a) via the GMail server or Yahoo server?

Asked by MathWorks Support Team on 24 Jan 2013
Latest activity Commented on by Christoph
on 23 Dec 2014

I would like to send an email from within MATLAB via the GMail/Yahoo server. If I try to do this using a script such as the following:

Using Gmail Server:

setpref('Internet','SMTP_Server','smtp.gmail.com');
setpref('Internet','E_mail','an.example.email.address@gmail.com');
sendmail('an.example.email.address@gmail.com','Test email', 'Test');

Using Yahoo Server:

setpref('Internet','SMTP_Server','smtp.mail.gmail.com');
setpref('Internet','E_mail','an.example.email.address@yahoo.com');
sendmail('an.example.email.address@yahoo.com','Test email', 'Test');

I receive the following error:

 
 ??? Error using ==> sendmail
 530 5.7.0 Must issue a STARTTLS command first b19sm1973874ana

  0 Comments

1 Answer

Answer by MathWorks Support Team on 24 Jan 2013
 Accepted answer

This change has been incorporated into the documentation in Release 2011a (R2011a). For previous releases, read below for any additional information:

To send email using SENDMAIL via the GMail/Yahoo server, you can execute the following in the MATLAB Prompt:

Gmail Server:

% Define these variables appropriately:
mail = 'sendemail.example.mathworks@gmail.com'; %Your GMail email address
password = 'testing1234';  %Your GMail password
setpref('Internet','SMTP_Server','smtp.gmail.com');

Yahoo Server:

% Define these variables appropriately:
mail = 'sendemail.example_mathworks@yahoo.com'; %Your Yahoo email address
password = 'testing1234';  %Your Yahoo password
setpref('Internet','SMTP_Server','smtp.mail.yahoo.com');

Gmail/Yahoo Servers:

setpref('Internet','E_mail',mail);
setpref('Internet','SMTP_Username',mail);
setpref('Internet','SMTP_Password',password);
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');
% Send the email.  Note that the first input is the address you are sending the email to
sendmail(mail,'Test from MATLAB','Hello! This is a test from MATLAB!')

Note that the above commands are undocumented and may change in future MATLAB releases. Also, note that SENDMAIL does not support servers that require username and password authentications in MATLAB 7.1 (R14SP3) and before and hence the above commands will not work with those releases.

  3 Comments

Vish
on 13 Dec 2014

This code doesn't work. I am getting error

Could not connect to SMTP host: smtp.mail.yahoo.com, port: 25; Connection refused: connect

Image Analyst
on 13 Dec 2014

You have an incorrect username/password or else you have a firewall issue. Note: the Mathworks never answers questions on officially posted topics like this. You'd have to post a new question.

Christoph
on 23 Dec 2014

If you have a MATLAB license with valid SMS, the best option is to contact support: http://www.mathworks.com/support/contact_us/


Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply today