Sending an email in Matlab
I have figured out “sending an email in matlab” from https://www.mathworks.com/help/matlab/import_export/sending-email.html
https://www.mathworks.com/help/matlab/ref/sendmail.html
and some web searching.
% setting up an email address and SMTP server information with ‘setpref’ Matlab function.
mail = ‘xxxxx@gmail.com'; % gmail address
password = 'xxxxxxxx'; % gamail password
% outgoing SMTP server address
% setpref('Internet','SMTP_Server','mail.server.network');
setpref('Internet','SMTP_Server','smtp.gmail.com');
% setting up my email address and password
% setpref('Internet','E_mail','youraddress@yourserver.com');
setpref('Internet','E_mail',mail);
setpref('Internet','SMTP_Username',mail);
setpref('Internet','SMTP_Password',password);
% using ‘sendmail’ command
% Because ‘sendmail’ does not support HTML format messages, we can send HTML
% message using attachment option
% According to Matlab manual, ‘sendmail’ function does not support email servers that require
% authentication.
% User need to change system setting and set preferences for the SMTP user name and 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');
% I am not sure what the above line means but maybe find some information from
% https://support.google.com/a/answer/176600?hl=en
% recipient email address
sendmail('xxxxxxxx@hotmail.com','Hello From MATLAB!');
When I ran this code, I’ve got the following error message.
Through web searching, I found a kind guy explained why there is an authentication error when using ‘sendmail’.
(https://www.mathworks.com/matlabcentral/answers/409343-sending-emails-through-matlab)
Need an additional setup in my gmail account to resolve the authentication error. Please see the following website for further information.
https://support.google.com/a/answer/6260879?hl=en
We need to turn on to allow less secure apps at the following address https://myaccount.google.com/lesssecureapps
I could check the authentication error in my gmail account. When I tried to send an email without allowing less secure apps, I’ve got the following message from google.
Finally, everything works fine after turning on the less secure apps, and I can check the email.
There are some options in Matlab such as using multiple recipients and attaching files. But I may not use Matlab often to send emails.