-
-
Save dgleich/9243281 to your computer and use it in GitHub Desktop.
function recipient = matlabmail(recipient, message, subject, sender, psswd) | |
% MATLABMAIL Send an email from a predefined gmail account. | |
% | |
% MATLABMAIL( recipient, message, subject ) | |
% | |
% sends the character string stored in 'message' with subjectline 'subject' | |
% to the address in 'recipient'. | |
% This requires that the sending address is a GMAIL email account. | |
% | |
% MATLABMAIL( recipient, message, subject, sender, passwd ) | |
% | |
% avoids using the stored credentials. | |
% | |
% Note: Authentication failed when my gmail account had 2-step verification enabled. | |
% | |
% Example: | |
% | |
% There's no example because we don't know your email address! | |
% Try to adapt the following: | |
% | |
% pause(60*(1+randi(5))); matlabmail('[email protected]', 'done pausing', 'command complete'); | |
% | |
% See also SENDMAIL | |
if nargin<4 | |
sender = '[email protected]'; | |
psswd = 'password_of_dummy_address'; | |
end | |
setpref('Internet','E_mail',sender); | |
setpref('Internet','SMTP_Server','smtp.gmail.com'); | |
setpref('Internet','SMTP_Username',sender); | |
setpref('Internet','SMTP_Password',psswd); | |
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(recipient, subject, message); | |
With Ubuntu running MATLAB Version: 9.0.0.341360 (R2016a)
Error using sendmail (line 171)
Exception reading response;
java.lang.IllegalArgumentException: Empty key
However, this works on my MacOS with R2012a proving it can work in principle (and I do have my google settings allowing less secure apps). Does one need to create some kind of encryption key?
I had a similar problem and it was because I was using OpenJDK (Ubuntu 16.04, Matlab 2017a).
I had to use OpenJDK to avoid a display bug in Matlab's default JVM. But once I fixed that bug and went back to
the default JVM, the sendmail() worked correctly.
Would be nice to know how to fix sendmail for OpenJDK though
good working code.In sender, Gmail account setting will be changed.
https://www.google.com/settings/security/lesssecureapps
whatever i do. the port is not changing to 465 as it should be
Error using sendmail (line 175)
Could not connect to SMTP host: smtp.gmail.com, port: 25;
Connection refused: connect
@ameer: You have to enable less secure apps by visiting https://www.google.com/settings/security/lesssecureapps