ntlm-auth java,JAVAMAIL:AUTH NTLM失败

i try to send email with java in local network,using microsoft exchange server

there is my code :

import java.io.UnsupportedEncodingException;

import java.util.Properties;

import javax.mail.Message;

import javax.mail.MessagingException;

import javax.mail.PasswordAuthentication;

import javax.mail.Session;

import javax.mail.Transport;

import javax.mail.internet.AddressException;

import javax.mail.internet.InternetAddress;

import javax.mail.internet.MimeMessage;

public class Main {

public static void main(String[] args) {

final String username = "username@MyDomain.com";

final String password = "password";

Properties props = new Properties();

props.put("mail.smtp.auth", "true");

props.put("mail.debug", "true");

props.put("mail.smtp.host", "exchange_host.MyDomain.com");

props.put("mail.smtp.port", "25");

props.put("mail.smtp.auth.mechanisms","NTLM");

props.put("mail.smtp.auth.ntlm.domain","MyDomain");

Session session = Session.getInstance(props,new MyAuthenticator(username,password));

try {

Message message = new MimeMessage(session);

message.setFrom(new InternetAddress("from_adress@MyDomain.com"));

message.setRecipients(Message.RecipientType.TO,

InternetAddress.parse("recipent_adresse"));

message.setSubject("Testing Subject");

message.setText("Dear Mail Crawler,"

+ "\n\n No spam to my email, please!");

Transport.send(message);

System.out.println("Done");

} catch (MessagingException e) {

throw new RuntimeException(e);

}

}

}

and this is my authentificator class :

import javax.mail.Authenticator;

import javax.mail.PasswordAuthentication;

public class MyAuthenticator extends Authenticator {

String user;

String pw;

public MyAuthenticator (String username, String password)

{

super();

this.user = username;

this.pw = password;

}

public PasswordAuthentication getPasswordAuthentication()

{

return new PasswordAuthentication(user, pw);

}

}

i use the NTLM mechanism but i get this error :

DEBUG: JavaMail version 1.4.7

DEBUG: successfully loaded resource: /META-INF/javamail.default.providers

DEBUG: Tables of loaded providers

DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle]}

DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]}

DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map

DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]

DEBUG SMTP: useEhlo true, useAuth true

DEBUG SMTP: useEhlo true, useAuth true

DEBUG SMTP: trying to connect to host "echange_server.MyDomain.com", port 25, isSSL false

220 echange_server.MyDomain.com Microsoft ESMTP MAIL Service ready at Mon, 30 Sep 2013 09:01:08 +0100

DEBUG SMTP: connected to host "echange_server.MyDomain.com", port: 25

EHLO host.MyDomain.com

250-echange_server.MyDomain.com Hello [xx.xx.xx.xx]

250-SIZE

250-PIPELINING

250-DSN

250-ENHANCEDSTATUSCODES

250-STARTTLS

250-X-ANONYMOUSTLS

250-AUTH NTLM

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 "STARTTLS", arg ""

DEBUG SMTP: Found extension "X-ANONYMOUSTLS", arg ""

DEBUG SMTP: Found extension "AUTH", arg "NTLM"

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: Attempt to authenticate using mechanisms: NTLM

DEBUG SMTP: AUTH NTLM failed

Exception in thread "main" java.lang.RuntimeException: javax.mail.AuthenticationFailedException: 250-exchange_host.MyDomain.com Hello [xx.xx.xx.xx]

at testPakcage.Main.main(Main.java:51)

Caused by: javax.mail.AuthenticationFailedException: 250-exchange_host.MyDomain.com Hello [xx.xx.xx.xx]

please help me,i past several day to search solution but i found nothing

解决方案

I have this working connecting to our Exchange 2010 server via NTLM.

NTLM uses your Windows login and password for authentication rather than your email address and password.

I made the following changes:

1) Username should be Windows login, NOT email address. NTLM uses your Windows credentials for authentication.

2) mail.smtp.auth.ntlm.domain should be your windows domain - i.e. the part before the slash if you normally log on to your Windows machine with "MYDOMAIN\id12345" as username.

Updated code below:

public class Main {

public static void main(String[] args) {

// *** CHANGED ***

final String username = "id12345"; // ID you log into Windows with

final String password = "MyWindowsPassword";

Properties props = new Properties();

props.put("mail.smtp.auth", "true");

props.put("mail.debug", "true");

props.put("mail.smtp.host", "exchangeserver.mydomain.com");

props.put("mail.smtp.port", "25");

props.put("mail.smtp.auth.mechanisms","NTLM");

// *** CHANGED ***

props.put("mail.smtp.auth.ntlm.domain","WINDOMAIN"); // Domain you log into Windows with

Session session = Session.getInstance(props,new MyAuthenticator(username,password));

try {

Message message = new MimeMessage(session);

message.setFrom(new InternetAddress("john.smith@mydomain.com"));

message.setRecipients(Message.RecipientType.TO,

InternetAddress.parse("the.recipient@mydomain.com"));

message.setSubject("Test email");

message.setText("TEST EMAIL");

Transport.send(message);

System.out.println("Done");

} catch (MessagingException e) {

e.printStackTrace();

}

}

public static class MyAuthenticator extends Authenticator {

String user;

String pw;

public MyAuthenticator (String username, String password)

{

super();

this.user = username;

this.pw = password;

}

public PasswordAuthentication getPasswordAuthentication()

{

return new PasswordAuthentication(user, pw);

}

}

}

For what it's worth, your original post helped me solve the problems I have had connecting which have been going on for several days.

One final point, you probably need to disable / change antivirus settings to allow access out on port 25.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
移植curl但是zlib无法使能,如何解决该问题 Host setup: arm-unknown-linux-gnueabihf Install prefix: /opt/rootfs/curl-7.79.0/curl-7.79.0/_install Compiler: arm-linux-gnueabihf-gcc CFLAGS: -Werror-implicit-function-declaration -O2 -Wno-system-headers -pthread CPPFLAGS: -isystem /opt/rootfs/openssl-1.1.1/openssl-1.1.1/_install/include LDFLAGS: -L/opt/rootfs/openssl-1.1.1/openssl-1.1.1/_install/lib LIBS: -lssl -lcrypto -ldl -lpthread curl version: 7.79.0 SSL: enabled (OpenSSL) SSH: no (--with-{libssh,libssh2}) zlib: no (--with-zlib) brotli: no (--with-brotli) zstd: no (--with-zstd) GSS-API: no (--with-gssapi) GSASL: no (libgsasl not found) TLS-SRP: enabled resolver: POSIX threaded IPv6: enabled Unix sockets: enabled IDN: no (--with-{libidn2,winidn}) Build libcurl: Shared=yes, Static=yes Built-in manual: enabled --libcurl option: enabled (--disable-libcurl-option) Verbose errors: enabled (--disable-verbose) Code coverage: disabled SSPI: no (--enable-sspi) ca cert bundle: no ca cert path: no ca fallback: no LDAP: no (--enable-ldap / --with-ldap-lib / --with-lber-lib) LDAPS: no (--enable-ldaps) RTSP: enabled RTMP: no (--with-librtmp) PSL: no (libpsl not found) Alt-svc: enabled (--disable-alt-svc) HSTS: enabled (--disable-hsts) HTTP1: enabled (internal) HTTP2: no (--with-nghttp2, --with-hyper) HTTP3: no (--with-ngtcp2, --with-quiche) ECH: no (--enable-ech) Protocols: DICT FILE FTP FTPS GOPHER GOPHERS HTTP HTTPS IMAP IMAPS MQTT POP3 POP3S RTSP SMB SMBS SMTP SMTPS TELNET TFTP Features: AsynchDNS HSTS HTTPS-proxy IPv6 Largefile NTLM NTLM_WB SSL TLS-SRP UnixSockets alt-svc
06-13

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值