Clear Filters
Clear Filters

Error in sending mail through matlab

19 views (last 30 days)
Anandha
Anandha on 9 Apr 2024
Answered: Nivedita on 3 May 2024
Im trying to send a mail through matlab but im keep getting this error
my code was like
mail = 'myuniversitymail@univ.edu';
password = 'mypassword';
mailingList = {'receivermail@gmail.com'};
server = 'smtp-mail.outlook.com';
props = java.lang.System.getProperties;
props.setProperty('mail.smtp.port','587');
props.setProperty('mail.smtp.starttls.enable','true');
props.setProperty('mail.smtp.ssl.trust', '*')
setpref('Internet','E_mail',mail);
setpref('Internet','SMTP_Server',server);
setpref('Internet','SMTP_Username',mail);
setpref('Internet','SMTP_Password',password);
messageBody = sprintf(' Good morning ');
messageBody = sprintf('%s\n\n Here is the data analysis from yesterdays data. Have a great day!', messageBody);
messageBody = sprintf('%s\n\n\n *This email was generated and sent automatically via MATLAB', messageBody);
sendmail(mailingList,"Data Analysis",messageBody,filename);
And the error is like,
Error using sendmail (line 184)
Can't send command to SMTP host;
No appropriate protocol (protocol is disabled or cipher suites are inappropriate)
Error in project (line 73)
sendmail(mailingList,"Data Analysis",messageBody,filename);
Pls help me to resolve this issue asap....

Answers (1)

Nivedita
Nivedita on 3 May 2024
Hi Anandha,
The error message you are encountering suggests an issue with the security protocol used by MATLAB to establish a connection with your SMTP server. This can occur due to a mismatch between the security requirements of the SMTP server and the capabilities/configuration of the MATLAB environment.
Ensure that your network or firewall settings are not blocking the connection to the SMTP server. Some institutions have strict network policies that might block certain outbound connections.
You can try using a different email service (e.g., Gmail's SMTP server) with its corresponding settings as follows:
mail = 'sender_address@gmail.com'; % Your Gmail address
password = 'sender_password'; % Your Gmail password
mailingList = {'receiver_address@gmail.com'}; % Recipient's email address
server = 'smtp.gmail.com'; % Gmail's SMTP server
props = java.lang.System.getProperties;
props.setProperty('mail.smtp.port','587'); % Use 465 for SSL
props.setProperty('mail.smtp.auth','true'); % Enable authentication
props.setProperty('mail.smtp.starttls.enable','true'); % Enable STARTTLS
props.setProperty('mail.smtp.ssl.trust', 'smtp.gmail.com'); % Trust the Gmail SMTP server
setpref('Internet','E_mail',mail);
setpref('Internet','SMTP_Server',server);
setpref('Internet','SMTP_Username',mail);
setpref('Internet','SMTP_Password',password);
messageBody = sprintf('Good morning');
messageBody = sprintf('%s\n\nHere is the data analysis from yesterday''s data. Have a great day!', messageBody);
messageBody = sprintf('%s\n\n\n*This email was generated and sent automatically via MATLAB', messageBody);
% Assuming 'filename' is defined elsewhere in your script
sendmail(mailingList, "Data Analysis", messageBody, filename);
If you have 2-Step Verification enabled on your Google account (which is highly recommended for security reasons), you cannot use your regular password. Instead, you will need to generate an App Password specifically for use in your MATLAB script.
You can try to directly access the App Passwords page by using this URL: https://security.google.com/settings/security/apppasswords
Generate a 16 character password and use it instead of your regular password in the code above. It should work.

Products

Community Treasure Hunt

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

Start Hunting!