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

gmailMATLABsmtp

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!

Best Answer

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') ;
Related Question