Use of sendmail function to send an email from a gmail account

I am trying to send an email, using the MATLAB sendmail function. I have been following the instructions of the sendmail function analysis in Mathworks on this link: https://uk.mathworks.com/help/matlab/import_export/sending-email.html
The code which I used on the command window is the following:
setpref ('Internet','E_mail','myemailaddress@gmail.com');
setpref ('Internet','SMTP_Server','smtp.gmail.com') ;
setpref('Internet',SMTP_Username','myusername');
setpref('Internet','SMTP_Password','mypassword') ;
sendmail('emailofreceiver@gmail.com','texttobesent') ;
The message that I am getting after running those commands is the following:
Error using sendmail (line 169) 530 5.7.0 Must issue a STARTTLS command first. i67sm1310611wri.61 - gsmtp
From what I understand, I must change the arguments inside one of the setpref functions that I call, though I am not sure what exactly to include in them. Any help would be very much appreciated! Thank you in advance!

 Risposta accettata

solution:
setpref('Internet','SMTP_Server','smtp.gmail.com');
setpref('Internet','E_mail','myemailaddress@gmail.com');
setpref('Internet','SMTP_Username','myusername');
setpref('Internet','SMTP_Password','mypassword');
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');
sendmail('emailofreceiver@gmail.com','texttobesent') ;

11 Commenti

Hi, and thanks for your answer. I run this code, with putting my info where need be, and the result that I get from MATLAB is the following:
Error using sendmail (line 169)
Authentication failed.
Not sure why this is returned.
Edit: I just needed to turn on access for less secure apps through google, and it worked.
I am getting following error even after turning on access for less secure apps
Error using sendmail (line 175) Could not connect to SMTP host: smtp.gmail.com, port: 25; Connection timed out: connect
Did you find any method to solve this? I also receive the same error after fixing the security level of Gmail
Me too. The password should be right, but I got this:
Error using sendmail (line 175) Authentication failed.
Make sure you visit https://myaccount.google.com/security#signin and in the section "Apps with account access" change "Allow less secure apps" to ON
Wonsup Lee
Wonsup Lee il 19 Ago 2018
Modificato: Wonsup Lee il 19 Ago 2018
If you use 2-Step Verification, refer to this site: https://support.google.com/accounts/answer/185833 . You need to get an 'app-specific password' that should be used instead of your real Gmail password.
I found this helped in one case: After trying from one instance of Matlab, I had to go into my Google account and acknowledge that the recent login activity was actually me. The next try with Matlab then worked.
excellent its working Mr walter & Royeth
i was getting error as "authentication failed"
then i looked out for your tip on turning on the app security in google settings
Thank you
I am still having problems with authentication failed!!
A) I have disabled 2 step verification, turned on 'less secure apps' and introduce used my normal password on matlab code: didnt work.
B) I also tried getting the app password but matlab doesn´t recognize it (Do you need to verify it somehow thrugh matlab???).
Thanks for this solution. I've been trying to do this for a while.
For those of you still having trouble after granting access in google security settings, there's a discussion under this answer pointing to solutions to problems with antivirus software, firewalls, and security certificate issues.

Accedi per commentare.

Più risposte (1)

Am having MATLAB 2012a.. it is asking for activation whenever opening MATLAB. After activation process completed also, repeatedly asking to activate. How to solve this pbm. Pls help me...

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by