OAuth 2 Flow for Office365 SMTP and JavaMail - java

I have a Web Application in which one of the module was used for sending emails from the application to the clients. It was using basic authentication for sending emails but now there is a requirement for implementing OAuth2 Authentication in the process. I am new to this mailing and Authentication stuff. I would be really grateful if someone could explain me where to start and what process should i folllow.
It is a legacy project and we are not using Spring. Instead, we are using Struts 1.2 with Java 7.
Current JavaMail properties are :
mailProperties.put("mail.smtp.host", host);
mailProperties.put("mail.smtp.auth", "true");
mailProperties.put("mail.smtp.starttls.enable", "true");
mailProperties.put("mail.smtp.port", "587");
mailProperties.put("mail.smtp.ssl.protocols", "TLSv1.2");

Related

Gmail will remove "Less secure apps" on the 30th of May. What does that mean for javax mail?

Google is removing the access to gmail for "Less secure apps" starting the 1st of April (Gmail notification).
That probably means that we won't be able to use javax mail with a gmail account anymore. Are there any workarounds to this?
If you want to keep using imaplib then The easiest fix for the depiction of less secure apps is to switch to using an apps password.
Another option would be to swtich to using Xoauth2 javax mail appears to support that Oauth2
Properties props = new Properties();
props.put("mail.imap.ssl.enable", "true"); // required for Gmail
props.put("mail.imap.auth.mechanisms", "XOAUTH2");
Session session = Session.getInstance(props);
Store store = session.getStore("imap");
store.connect("imap.gmail.com", username, oauth2_access_token);
How to create a Apps Password for connecting to Google's SMTP server.

own java mail client & yandex smtp server - javax.mail.AuthenticationFailedException

evening,
i'm trying to do my own email client and then error came.
im using javax.mail library
i did try several methods for sending, most of them crash on sad places. anyway, one method i consider with potential is fine until end when message pops up:
m02 error-2: javax.mail.AuthenticationFailedException: 535 5.7.8 Error: authentication failed: Your message looks like spam. You need to use web for sending or prove you are not a robot using the following link http://ya.cc/[deleted] where [deleted] is originally few letters string.
i saw with gmail, you need to set something in settings to be able to use your own client, i expected this would be similar case but i couldn't find any settings about it in yandex mailbox settings.
another point, smtp address i have found at some forum so i presume it is possible to use own client
properties i set:
properties.put("mail.smtp.host", host);
properties.put("mail.smtp.socketFactory.port", port_ssl);
properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.port", port_ssl);
where
private String host = "smtp.yandex.com";
private String port_ssl = "465";
other code, im using classic way: Session for Authenticator, then Message and Transport.send. anyway, i think problem is with properties or mailbox settins? i just cant think of where.
question: how can i fix it so i can send an email with my client?
note: reason for using yandex is thanks to simple sign up since i dont have phone number and gmail requires one. if you know about other email service where own client should work & no need for phone that is also nice alternative answer
As an alternative answer, you could try the service https://protonmail.com, it should not require phone number and it can be reachable by SMTP clients using these settings https://mailsettings.co/protonmail-smtp-server-settings

Spring Boot - spring-boot-starter-mail Sending Email for Smartermail not working

I have an existing Java Spring Boot project that been using Gmail for sending out email such as forgot password for our web system.
Recently we are moving to production as we are using Smartermail as our company email. The same Java Mail Code wrapper from Spring Boot that is working with Gmail SMTP is now not working after changing the configuration to smartermail SMTP.
However, I have tried to connect it from my Android phone email app using the same Smartermail configuration and credential, and it is all working.
Here is my application.properties content snapshot:
# Email setting
com.eurogain.portal.emailFrom=postmaster#myowndomain.com
spring.mail.host=mail.myowndomain.com
spring.mail.port=465
spring.mail.username=user1#myowndomain.com
spring.mail.password=password
spring.mail.properties.mail.smtp.connectiontimeout=15000
spring.mail.properties.mail.smtp.timeout=15000
spring.mail.properties.mail.smtp.writetimeout=15000
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
There are no error, and the above code the reason I put the timeout shorter is because without it, it will keep on running without any stop.
Any advise or tips? Appreciate the helps.
Ok, it was confirmed by the system admin that the port 465 here is for secure encryption type. The non-secure port is 587.
Still, in that case, does that mean Java Sprint Boot email doesn't support the secure type?

Unable to connect to Office 365 SMTP Server via Jakarta Mail using OAuth 2

I am currently trying to integrate OAuth2 into an existing e-mail infrastructure of a java application. The application is using Jakarta mail, which according to their documentation supports OAuth2 (https://eclipse-ee4j.github.io/mail/OAuth2). For some reason I am struggeling to connect to the Office 365 SMTP Server, while connecting via IMAP works perfectly fine. So here is what I have been doing so far:
Create Office 365 Developer Account, populate with users and user data.
Log into the azure backend, configure an app registration, including callback url etc. and the following api rights: https://i.stack.imgur.com/lXjER.png
Use the following authentication url to create an authentication code:
https://login.microsoftonline.com/{my_tenant_id}/oauth2/v2.0/authorize?
client_id={my_client_id}&
state=state_to_check&
redirect_uri=http://localhost:5555/callback/authorization&
scope=offline_access https://outlook.office.com/IMAP.AccessAsUser.All https://outlook.office.com/POP.AccessAsUser.All https://outlook.office.com/SMTP.Send
&response_type=code
As you can see i am using the following scopes:
offline_access
https://outlook.office.com/IMAP.AccessAsUser.All
https://outlook.office.com/POP.AccessAsUser.All
https://outlook.office.com/SMTP.Send
Retrieve the authorization code and use it to get refresh and access token, which gets me the following response:
{
"token_type": "Bearer",
"scope": "https://outlook.office.com/IMAP.AccessAsUser.All https://outlook.office.com/Mail.Read https://outlook.office.com/Mail.Read.All https://outlook.office.com/Mail.Read.Shared https://outlook.office.com/Mail.ReadBasic https://outlook.office.com/Mail.ReadWrite https://outlook.office.com/Mail.Send https://outlook.office.com/Mail.Send.All https://outlook.office.com/Mail.Send.Shared https://outlook.office.com/POP.AccessAsUser.All https://outlook.office.com/SMTP.Send https://outlook.office.com/User.Read",
"expires_in": 3599,
"ext_expires_in": 3599,
"access_token": ...,
"refresh_token": ...
}
So I would say, everything is working as expected so far regaring the OAuth 2.0 authentication process. Now, going on to use the access token to get access to the users email account, I added the following few lines in the e-mail logic of our application to enabe IMAP via OAuth:
[more props stuff here]
if (useOauth) {
props.put("mail." + protocol + ".auth", "true");
props.put("mail." + protocol + ".auth.mechanisms", "XOAUTH2");
props.put("mail." + protocol + ".auth.login.disable", "true");
props.put("mail." + protocol + ".auth.plain.disable", "true");
}
return Session.getInstance(props);
This works perfectly fine and I can connect via IMAP, read folders, messages, etc. My problem is, if I try to modify our code in a similar way for SMTP I get the following error:
Exception in thread "main" jakarta.mail.AuthenticationFailedException: 451 4.7.0 Temporary server error. Please try again later. PRX4 [AM9P191CA0011.EURP191.PROD.OUTLOOK.COM]
at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:947)
at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:858)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:762)
at jakarta.mail.Service.connect(Service.java:342)
at jakarta.mail.Service.connect(Service.java:222)
at jakarta.mail.Service.connect(Service.java:243)
at Application.main(Application.java:52)
I had a look through the following example application that I have found on github (https://github.com/eino-makitalo/vesa-mailtest/tree/master/src/main) and the few answers on stackoverflow to see if I have missed any properties to set specifically for SMTP but I keep running into the same error, using the following configuration:
Properties props = new Properties();
props.put("mail.smtp.auth.xoauth2.disable", "false");
props.put("mail.smtp.auth.mechanisms", "XOAUTH2");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host","smtp.office365.com");
props.put("mail.smtp.port", "587");
props.put("mail.transport.protocol","smtp");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.auth.login.disable","true");
props.put("mail.smtp.auth.plain.disable","true");
props.put("mail.debug.auth", "true");
Session session = Session.getInstance(props);
session.setDebug(true);
Transport transport = session.getTransport("smtp");
transport.connect( username, token);
Now I am hoping, that maybe someone has run into this issue before, and can help me out. The only questions I can find regarding the exception postet above are all related to custom exchange server setups, and how you should configure DNS setup on these servers. But I dont think this should be relevant for me, as I am not trying to connect to a custom exchange server.
UPDATE:
So I tried the same configuration with the google service, and it works for both IMAP and SMTP, so it is for sure a problem with the Microsoft Services. But I am still not sure what more I can try to make it work.
Okay, found the problem: For some reason I did not think to try and explicitly request the openId scope. Not sure why, but for some reason I had it in my head that it will be requested automatically, if you don't specify it explicitly. After requesting openId explicitly both SMTP and IMAP work.

how will I send Mail using SSMTP

props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.port", portnumber);
props.put("mail.smtp.host", "smtp.office365.com");
props.put("mail.smtp.auth", "true");
I am using this code for sending email in java using my linux machine.
Please tell me which OS service it is using internally currently for sending mail.and
I want to change this service to ssmtp . How will I send through ssmtp.
The code you show seems to use the Java Mail API, which is included in your java project and used here as an SMTP client to send the mail. The SMTP server used here is : smtp.office365.com
However, ssmtp is not a service as you mentioned. It is another email client.
I invite you to have a deeper look into these tools to have a clearer idea before using them.

Categories

Resources