Sending Emails with MATLAB

Hello,
I am currently running a script in matlab that sends an email out when it is through with a .pdf attachment. This was working flawlessly two days ago and now I can't seem to get it to work.
I am recieving the following error message:
Error using sendmail (line 175)
530 5.7.57 Client not authenticated to send mail. [BLAP220CA0019.NAMP220.PROD.OUTLOOK.COM]
Here is my code:
mail = 'email@address.com'; % false email for forum
password = 'password'; % false pass for forum
mailingList = {'people@email.com'}; % false email for forum
server = 'smtp-mail.outlook.com';
props = java.lang.System.getProperties;
props.setProperty('mail.smtp.port','587');
props.setProperty('mail.smtp.starttls.enable','true');
setpref('Internet','E_mail',mail);
setpref('Internet','SMTP_Server',server);
setpref('Internet','SMTP_Username',mail);
setpref('Internet','SMTP_Password',password);
messageBody = sprintf(' Good morning team!');
messageBody = sprintf('%s\n\n Here is the data analysis from yesterdays data. Have a great day!', messageBody);
messageBody = sprintf('%s\n\n - Brian Gregory', messageBody);
messageBody = sprintf('%s\n\n\n *This email was generated and sent automatically via MATLAB', messageBody);
sendmail(mailingList, ...
"Data Analysis - "+date, ...
messageBody,fileDir7);

 Accepted Answer

Adam Danz
Adam Danz on 14 Jun 2021
Edited: Adam Danz on 24 Jun 2025
You probably have to re-enable the feature in your email account (more info for gmail).
If you're using gmail, the link to enable less secure app acces: https://myaccount.google.com/lesssecureapps
Make sure it's turned on.
If that's not the problem I'll remove this answer so your questions is recategorized as unanswered.
2025 update
Google turned off less secure app access for third party apps 😖. Here's the steps I took to get my email function up and running again.
  1. From the gmail account associated with the gmail address, turn on 2-step authentication. Then log into the account.
  2. Search google for "App Passwords" and follow the instructions. This will generate a 16-digit password that may contain spaces -- keep the spaces.
  3. This part is option but I stored the encrypted password in matlab using setSecret and my email function retrieves the password using getSecret.

9 Comments

Thank you!
That feature has turned off once on me too. It's probably an internal safety feature of gmail.
Yeah I was using outlook but I couldn't figure this feature out on it so I made a gmail because it's easier to manage in my opinion.
If you aren't doing this already, I recommend creating an dedicated gmail account for this purpose that is isolated from your google profile. This opens the doors to security problems but there's no way around it.
I think this is more difficult as of jan 2025 since google has turned off less secure app access for third party apps which use password/username data. is there a further work around?
It is more difficult, but not impossible. My matlab emailer is up and running again. I've updated my answer.
I have gmail set up without 2 factor authentication. I find that Thunderbird is able to read messages when authentication is my standard password. However, I find that Thunderbird is unable to send messages via gmail with my standard password.
There are hints that Thunderbird might start working to send email if I set up an app password... but that would require enabling 2 factor authentication, which I am reluctant to do.
I have similar concerns. I use a completely separate gmail account solely for my MATLAB email function's access. The main (only?) way I use it is to get notified when a long analysis or process is complete. I have some scrapers or analyses that run for days. I'll call the email function at the end of the process or in the event of a error so I can be notified of the event. I haven't found another purpose to email from MATLAB but i'm sure there are interesting use cases out there. I'd be interested in hearing them.
Nikolaos
Nikolaos on 27 Oct 2025
Edited: Nikolaos on 27 Oct 2025
Ty for the updated answer. I used a 16-digit password and matlab sends email successfully now. Great comment sir!

Sign in to comment.

More Answers (0)

Categories

Asked:

on 14 Jun 2021

Edited:

on 27 Oct 2025

Community Treasure Hunt

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

Start Hunting!