I've seen several posts that offer a solution to something very similar to my situation but for whatever reason it does not work for me.
For example, here it's presented as a working sample illustrating OP's related problem and here as an actual answer.
I need to send e-mail as myself from my Exchange account via SMTP using SSO UserID and Password. This all happens in a restricted corporate environment.
From what I understand from the debug info I'm successfully connecting to an SMTP server and then fail user authentication with 530 5.7.1 Client was not authenticated
. NTLM
authentication itself seems to be enabled.
DEBUG:
DEBUG: setDebug: JavaMail version 1.5.4
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth false
DEBUG SMTP: trying to connect to host "server.bank.com", port 25, isSSL false
220 server1.corp.bank.com Microsoft ESMTP MAIL Service ready at Mon, 24 Aug 2015 17:15:24 -0400
DEBUG SMTP: connected to host "server.bank.com", port: 25
EHLO server2.corp.bank.com
250-server1.corp.bank.com Hello [xxx.xxx.xxx.xxx]
250-SIZE
250-PIPELINING
250-DSN
250-ENHANCEDSTATUSCODES
250-X-ANONYMOUSTLS
250-AUTH NTLM LOGIN
250-X-EXPS GSSAPI NTLM
250-8BITMIME
250-BINARYMIME
250-CHUNKING
250-XEXCH50
250-XRDST
250 XSHADOW
DEBUG SMTP: Found extension "SIZE", arg ""
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "DSN", arg ""
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "X-ANONYMOUSTLS", arg ""
DEBUG SMTP: Found extension "AUTH", arg "NTLM LOGIN"
DEBUG SMTP: Found extension "X-EXPS", arg "GSSAPI NTLM"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "BINARYMIME", arg ""
DEBUG SMTP: Found extension "CHUNKING", arg ""
DEBUG SMTP: Found extension "XEXCH50", arg ""
DEBUG SMTP: Found extension "XRDST", arg ""
DEBUG SMTP: Found extension "XSHADOW", arg ""
DEBUG SMTP: use8bit false
MAIL FROM:<first.last@bank.com>
530 5.7.1 Client was not authenticated
DEBUG SMTP: got response code 530, with response: 530 5.7.1 Client was not authenticated
Essential part of my code:
static void sendEmail(){
Properties props = System.getProperties();
props.setProperty("mail.smtp.host", host);
props.setProperty("mail.smtp.port", "25");
// props.setProperty("mail.debug", "true");
// props.setProperty("mail.debug.auth", "true");
props.setProperty("mail.smtp.starttls.enable","true");
props.setProperty("mail.smtp.auth.mechanisms", "NTLM");
props.setProperty("mail.smtp.auth.ntlm.domain", user_sso_domain);
// Session session = Session.getDefaultInstance(props);
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user_sso_id, user_sso_password);
}
});
session.setDebug(true);
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(email_from));
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(email_to));
message.setSubject("Test Message");
message.setText("This is my test message");
Transport.send(message);
} catch (Exception ex) {
ex.printStackTrace();
}
}
I would appreciate your suggestions for further troubleshooting.
EDIT
Working code after implementing the fix and better practices from the accepted answer:
private static void sendEmail(){
Properties props = System.getProperties();
props.setProperty("mail.smtp.host", host);
props.setProperty("mail.smtp.port", "25");
// props.setProperty("mail.debug", "true");
// props.setProperty("mail.debug.auth", "true");
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.smtp.starttls.enable","true");
props.setProperty("mail.smtp.auth.mechanisms", "NTLM");
props.setProperty("mail.smtp.auth.ntlm.domain", user_sso_domain);
Session session = Session.getInstance(props);
session.setDebug(true);
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(email_from));
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(email_to));
message.setSubject("Test Message");
message.setText("This is my test message");
Transport.send(message, user_sso_id, user_sso_password);
} catch (Exception ex) {
ex.printStackTrace();
}
}
You need to set "mail.smtp.auth" to "true".
Or, better yet, fix these common mistakes and make your program much simpler.