NeuraConnect Lab

Understanding the networked brain through its injury

Automated Mail Through MATLAB


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


To send emails through MATLAB,  we need to set up an App Password ( a 16-digit passcode). Search for "sign in with app passwords".

Then call the function below
function SendEmailFromMatlab(emailto,subject,message,attachment)
%Specify the Gmail accout and password.
mail = 'XXXX';    % App Password (a 16-digit passcode)
password = 'XXXXX'; %Your GMail password
% Set up Gmail SMTP service and email preferences.
setpref('Internet','E_mail',mail);
setpref('Internet','SMTP_Server','smtp.gmail.com');
%%%setpref('Internet','SMTP_Server','smtp.aol.com');  This works for AOL account
setpref('Internet','SMTP_Username',mail);
setpref('Internet','SMTP_Password',password);
% Gmail server.
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');
props.setProperty('mail.smtp.starttls.enable','true');
sendmail(emailto,subject,message,attachment)
For example
emailto='[email protected]'
subject='Hello World'
attachment='./dog.png';
name='a dog picture'
message={'Dear friends ' ,... 
['Here is' name '.'], ...
char(10), ...
'Good day'}
SendEmailFromMatlab(emailto,subject,message,attachment)