sending emails through Matlab

31 Ansichten (letzte 30 Tage)
Lalitha Srilal Kannangara Arachchige
Kommentiert: Omar Salah am 5 Aug. 2021
I have tried with following code.
mail = 'my_email@gmail.com';
password = 'my_password';
setpref('Internet','SMTP_Server','smtp.gmail.com');
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.starttls.enable','true');
%props.setProperty('mail.smtp.socketFactory.class', 'javax.net.ssl.SSLSocketFactory');
%props.setProperty('mail.smtp.socketFactory.port','25');
sendmail(sent_email,'Test from MATLAB','Hello! This is a test from MATLAB!')
But it gives the following error after allowing access to the low-security application through Gmail account.
"Error using sendmail (line 175)
Could not connect to SMTP host: smtp.gmail.com, port: 25, response: 421"
can anyone help me to solve this issue?

Antworten (1)

Kojiro Saito
Kojiro Saito am 9 Jul. 2018
As this page says, smtp.gmail.com uses port 465 (SSL) or 587 (TLS), so you need to specify port number.
props.setProperty('mail.smtp.socketFactory.port','465');
Also, by default, Gmail blocks less secure apps, so you need to enable it following the steps in this page. If you have Google accounts on your own, a simple way is enabling less secure apps from https://myaccount.google.com/lesssecureapps.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by