MATLAB Answers

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

144 views (last 30 days)
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: ??? Error using ==> sendmail 530 5.7.0 Must issue a STARTTLS command first b19sm1973874ana

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 12 Jul 2016
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!')
In R2013a, the following command might also resolve the issue:
props.setProperty('mail.smtp.starttls.enable','true');
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.

  4 Comments

Show 1 older comment
Image Analyst
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.
Brad Stiritz
Brad Stiritz on 5 Feb 2017
>Note that the above commands are undocumented and may change in future MATLAB releases..
Yair Altman recently discussed sendmail and this specific answer on his blog: link

Sign in to comment.

More Answers (2)

rubini r
rubini r on 9 Jun 2016
HAI... I AM USING MATLAB R2013a
I TRIED SENDMAIL CODING BUT I GET THE ERROR....
530 5.7.0 Must issue a STARTTLS command first. 5sm29774614pfn.46 - gsmtp
I SOLVE THIS ERROR BY USING COMMAND IN COMMAND WINDOW
JUST TYPE THIS COMMANDS
props = java.lang.System.getProperties;
props.setProperty( 'mail.smtp.starttls.enable', 'true' )
then run your sendmail coding ,it was worked....
Coding
mail ='123@gmail.com'; % Your mail id
password ='hai@123'; % Yore password
host = 'smtp.mail.com';
port = '587';
setpref('Internet','E_mail',mail);
setpref('Internet','SMTP_Server',host);
sendmail({'kalai@gmail.com',... 'kalai1@gmail.com'},'Matlab',... 'hai',{'C:\New folder\license.txt',... 'C:\New folder\j.pdf'}) % Receiver mail id and also send many more receiver ... send also files like doc,pdf,image,rar files
if u use any sender mail id just open and close sender mail in your using system ....because it solve authentication error by using this .

  0 Comments

Sign in to comment.


Iddo Weiner
Iddo Weiner on 1 Nov 2016
Hi,
I'm having trouble with running this code. I tried the suggestion above:
%
% parameters
mail = 'mymail@gmail.com'; % my gmail address
password = 'mypassword'; % my gmail password
host = 'smtp.mail.com';
% preferences
setpref('Internet','SMTP_Server', host);
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
sendmail(mail,'Test from MATLAB','Hello! This is a test from MATLAB!')
But I get this error:
%
Error using sendmail (line 171)
Authentication failed.
Any ideas what might be causing this?
Thanks

Products


Release

R2006b

Community Treasure Hunt

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

Start Hunting!