Azz*_*adi 3 java email exchange-server ntlm jakarta-mail
我尝试使用microsoft Exchange服务器在本地网络中发送带有java的电子邮件
有我的代码:
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的authentificator类:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
我使用NTLM机制,但我收到此错误:
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]
Run Code Online (Sandbox Code Playgroud)
请帮助我,我过去几天搜索解决方案,但我一无所获
我有这个工作通过NTLM连接到我们的Exchange 2010服务器.
NTLM使用您的Windows登录名和密码进行身份验证,而不是您的电子邮件地址和密码.
我做了以下更改:
1)用户名应该是Windows登录名,而不是电子邮件地址.NTLM使用您的Windows凭据进行身份验证.
2)mail.smtp.auth.ntlm.domain应该是你的Windows域 - 即斜杠前面的部分,如果你通常以"MYDOMAIN\id12345"作为用户名登录你的Windows机器.
更新的代码如下:
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
对于它的价值,你的原始帖子帮助我解决了我连接已经持续了好几天的问题.
最后一点,您可能需要禁用/更改防病毒设置以允许在端口25上进行访问.
| 归档时间: |
|
| 查看次数: |
15804 次 |
| 最近记录: |