Using gmail after May 30, 2022

I am currently using Matlab to send emails using Gmail with "less secure apps" selected to "ON" under Google Security. This topic has been discussed at length in several threads and I currently have no problems, however, as per Google: "To help keep your account secure, starting May 30, 2022, ​​Google will no longer support the use of third-party apps or devices which ask you to sign in to your Google Account using only your username and password."
So, after May, 30th, I will no longer be able to use Matlab to send emails via Gmail. Does anyone have a work around for this?

6 Comments

Walter, Thank you for your suggestion. The first article you reference on using OAuth, under the sub-heading "Use the Gmail SMTP Server", requires the use of Googles "less secure apps" feature, which is partly what is going away on the May 30th date. Perhaps my description of the issue was not very clear, as I am no expert in this area, but the Google "less secure apps" is what allows Matlab to currently use Gmail. Without the "less secure apps" set to "ON" (or the complete lack of this feature after May 30th), the mail server responds with an authentication error and no mail gets sent.
Although a paid service, Google Workspace is intriguing, but this too will run into the same problem, just at a later unspecified date. Here is the full quote from Google:
"To help keep your account secure, starting May 30, 2022, ​​Google will no longer support the use of third-party apps or devices which ask you to sign in to your Google Account using only your username and password.
Please note this deadline does not apply to Google Workspace or Google Cloud Identity customers. The enforcement date for these customers will be announced on the Workspace blog at a later date.
For more information, please continue reading.Special Note on Apple Device Sign-Ins. Users who have not recently signed into their Google Account using only username and password will be able to only make new sign in attempts using the Google account type starting from February 28, 2022. Existing users may continue to sign into their Google Account using their username and password until May 30, 2022."
"Older scanners or printers might not support OAuth, which lets apps and devices share your Google account data without requiring your username or password. In this case, take these steps before you set up the device:
Turn on less secure apps in the Admin console."
So if you cannot use OAuth (now) then you start by turning on Less Secure.
But flip that around: If you need to turn on Less Secure if a condition is not met, then the implications are that you do not need to turn on Less Secure if you can do OAuth. And if the challenge is that you will not be able to turn on Less Secure later, then we can hypothesize that there will be no problem using OAuth since it does not use Less Secure anyhow.
Walter, I beleive I follow what you are saying, but I'm not sure how it applies. I do not have a device such as a printer or scanner. I am simply trying to use Matlab's sendmail() function, to send an email from Matlab using my existing Gmail account. More specifically, I am using a sendmail wrapper script from the File Exchange: send_mail_message() kindly supplied by "Pradyumna" dated June 2008.
I will point out that this was working with my exisiting AOL account, but then stopped. I got that working again as AOL allowed Matlab to be recognized and instead of logging in with my AOL password, I use a special "one-time" 16 character code provided by AOL that it somehow uses to recognize that Matlab is trusted and allowed to log in through the AOL account. I was hoping there was a similar solution for Gmail, as the current solution of "less secure apps" is going away, so I will not longer be able to use Matlab to send an email from my Gmail account. I would prefer the Gmail account version over AOL as Gmail is free.
"Less Secure Apps" is turned on in Google security.
Going to get myself in trouble but here is the actual code:
%Specify the Gmail accout and password.
mail = 'MyNameHere@gmail.com'; %Your GMail email address
password = 'MyPassword'; %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');
%Send the email.
sendmail(emailto,subject,message,attachment)
This all currently works fine as is but will stop working on May 30th as the "Less Secure Apps" goes away and Google Gmail will no longer allow Matlab to send emails through a Gmail account. I will reeive an authentication error, just like I will now if "Less Secure Apps" is not turned on.
I think I have it working. In Google Security "Less Secure Apps" is turned OFF. Enable 2-Step authentication and log in to the Google Gmail account. Search for "App Paswords", and under "Select app" type "MATLAB" without the quotes, then press "Generate" button. This produces a 16-digit passcode that is then used in place of the Gmail account password. So use the above code but replace
password = 'MyPassword'; %Your GMail password
with
password = 'MyPasscode'; %Your GMail 16-digit passcode
It sounds like you should post that last comment as an answer. That way other people can try this solution as well.

Sign in to comment.

 Accepted Answer

Joseph Hejl
Joseph Hejl on 16 Mar 2022
I think I have it working. In Google Security "Less Secure Apps" is turned OFF. Enable 2-Step authentication and log in to the Google Gmail account. Search for "App Paswords", and under "Select app" type "MATLAB" without the quotes, then press "Generate" button. This produces a 16-digit passcode that is then used in place of the Gmail account password. So use the above code but replace
password = 'MyPassword'; %Your GMail password
with
password = 'MyPasscode'; %Your GMail 16-digit passcode

6 Comments

thank you very much!
Ash Ash. Glad I could help. Knew this was an upcoming problem, but couldn't find the solution. Got the idea from my AOL account, which warned me of a similar deadline but gave an indication of how to generate a special 16-digit passcode for trusted apps, like MATLAB. Took some more searching, as Google was much less clear, but ended up having a similar 16-digit passcode generator. I'm sure this is going to save a lot of people some unnecessary future headaches, and really doesn't require any modifications to the existing MATLAB code other than to use the 16-digit passcode instead of the usual Gmail password.
@Joseph Hejl, greetings.
Does this procedure work perpetually?
Or everytime I want to access gmail through MATLAB I have to add a new 16-digit password?
I'm asking that because I made the procedure. It took a while (perhaps some google server update time) and it worked.
But now (another day) it is showing the same authentication required error as before.
Well, I apparently found the persistent error.
I found this solution, from Jesus (ironically rsrs) and the only thing I have to add was the line below:
props.setProperty('mail.smtp.auth','true');
Then, it work great.
Also complementing from another cases in the forum:
  • I recognized my activies on the computer I'm using, as well as the activity of creating the app password in Gmail.
As a matter of fact, if it is not performed, it might keeping everybody in trouble looking only at MATLAB solutions.
Cheers
drummer, once the 16 digit passcode has been created, it works every time, no need to recreate the passcode.
Could you please provide the current whole setting up to send email in MATLAB?

Sign in to comment.

More Answers (1)

In my side works perfect the folowing code
server='smtp.gmail.com';
setpref('Internet','E_mail', mail);
setpref('Internet','SMTP_Server', server);
setpref('Internet','SMTP_Username', mail);
setpref('Internet','SMTP_Password', password);
props = java.lang.System.getProperties
props.setProperty( 'mail.smtp.auth', 'true' );
props.setProperty( 'mail.smtp.user', mail );
props.setProperty( 'mail.smtp.password', password );
props.setProperty( 'mail.smtp.host', server );
props.setProperty( 'mail.smtp.port', '587' );
props.setProperty( 'mail.smtp.starttls.enable', 'true' );
Also,pay attention that App Paswordsincludesspaces between groups of numbers when you copy it from Google.

Products

Release

R2020b

Asked:

on 15 Mar 2022

Answered:

on 21 Nov 2023

Community Treasure Hunt

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

Start Hunting!