Hi all
I have been trying setting up the mailing system using oauth2 for SMTP.
I have an account of Microsoft with the Exchnage online License. I have followed the exact steps
I have registered the New-ServicePrincipal and given full access for mailbox.
I have the following code:
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
public class MailSend {
public static void sendMail(String accessToken) {
// Compose the email
String subject = "Your Subject";
String body = "Your email body.";
String fromEmail = "email address removed for privacy reasons";
String toEmail = "email address removed for privacy reasons";
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.office365.com");
props.put("mail.smtp.port", "587");
props.put("mail.smtp.starttls.enable", true);
try{
Session session = Session.getInstance(props, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("", accessToken);
}
});
session.setDebug(true);
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(fromEmail));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail));
message.setSubject(subject);
message.setText(body);
// Send the email
Transport.send(message);
System.out.println("Email sent successfully!");
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
public static String aquireAccessToken() throws MalformedURLException {
ConfidentialClientApplication app = ConfidentialClientApplication.builder(
clientId,
ClientCredentialFactory.createFromSecret(clientSecret))
.build();
ClientCredentialParameters parameters = ClientCredentialParameters.builder(scopes).build();
IAuthenticationResult result = app.acquireToken(parameters).join();
// Use the accessToken for SMTP authentication
return result.accessToken();
}
public static void main(String[] args) throws Exception{
String token = main.java.TokenProvider.aquireAccessToken();
main.java.MailSend.sendMail(token);
}
I am putting the correct tenantId, ClientSecret and clientId.
My authentication is successfull which can be seen in the logs but I always get the 530 error.
The logs shows:
530 5.7.57 Client not authenticated to send mail. [FR2P281CA0136.DEUP281.PROD.OUTLOOK.COM 2023-09-22T12:40:18.555Z 08DBBA5C08041989]
DEBUG SMTP: got response code 530, with response: 530 5.7.57 Client not authenticated to send mail. [FR2P281CA0136.DEUP281.PROD.OUTLOOK.COM 2023-09-22T12:40:18.555Z 08DBBA5C08041989]
RSET
DEBUG SMTP: EOF: [EOF]
DEBUG SMTP: MessagingException while sending, THROW:
com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.57 Client not authenticated to send mail. [FR2P281CA0136.DEUP281.PROD.OUTLOOK.COM 2023-09-22T12:40:18.555Z 08DBBA5C08041989]
at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2374)
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1808)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1285)
at javax.mail.Transport.send0(Transport.java:231)
at javax.mail.Transport.send(Transport.java:100)
at main.java.MailSend.sendMail(MailSend.java:44)
at main.java.Main.main(Main.java:8)
QUIT
javax.mail.MessagingException: Can't send command to SMTP host;
nested exception is:
java.net.SocketException: Connection or outbound has closed
at com.sun.mail.smtp.SMTPTransport.sendCommand(SMTPTransport.java:2431)
at com.sun.mail.smtp.SMTPTransport.sendCommand(SMTPTransport.java:2418)
at com.sun.mail.smtp.SMTPTransport.close(SMTPTransport.java:1394)
at javax.mail.Transport.send0(Transport.java:233)
at javax.mail.Transport.send(Transport.java:100)
at main.java.MailSend.sendMail(MailSend.java:44)
at main.java.Main.main(Main.java:8)
Caused by: java.net.SocketException: Connection or outbound has closed
at sun.security.ssl.SSLSocketImpl$AppOutputStream.write(SSLSocketImpl.java:1181)
at com.sun.mail.util.TraceOutputStream.write(TraceOutputStream.java:116)
at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:82)
at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:140)
at com.sun.mail.smtp.SMTPTransport.sendCommand(SMTPTransport.java:2429)
... 6 more
I fail to understand the exact cause. Any support will be helpful.
Regards
Kinjal