我尝试用本地网络中的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; //属性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.getInstance(道具,新的MyAuthenticator(用户名,密码)); 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, +\\\\\\我的电子邮件中没有垃圾邮件!); Transport.send(message); System.out.println(Done); } catch(MessagingException e){throw new RuntimeException(e); } }
} 

这是我的验证类:

 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(){返回新的PasswordAuthentication(user,pw); } } 

我使用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: 25EHLO 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 failedException 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]
 

解决方案

我通过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 withfinal 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 withSession 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);}}
}

最后一点,您可能需要禁用/更改防病毒设置才能允许访问25端口。

mail.smtp.auth.ntlm.domain相关推荐

  1. ntlm-auth java_JAVAMAIL:AUTH NTLM失败

    我尝试在本地网络的java发送邮件,使用Microsoft Exchange ServerJAVAMAIL:AUTH NTLM失败 有我的代码: import java.io.UnsupportedE ...

  2. com.sun.mail.smtp.SMTPSendFailedException:554 5.2.0 STOREDRV.Submission.Exception

    使用outlook发送邮件报异常:com.sun.mail.smtp.SMTPSendFailedException:554 5.2.0 STOREDRV.Submission.Exception 我 ...

  3. SpringBoot使用JavaMailSender发送邮件:com.sun.mail.smtp.SMTPSendFailedException: 451 MI:SFQ 163 smtp7

    SpringBoot使用JavaMailSender发送邮件时,报错如下: 2020-08-08 07:32:21,237 ERROR --- [http-nio-8080-exec-1] cn.co ...

  4. com.sun.mail.smtp.SMTPSendFailedException: 550 Invalid User

    使用springboot集成的邮件发送功能,参考了文章:https://blog.csdn.net/gfd54gd5f46/article/details/77414560 无论是SimpleMail ...

  5. com.sun.mail.smtp.protected void rcptTo() 方法

    RFC 822 RFC 822是电子邮件的标准格式,电子邮件除了是由一个Internet用户传递给另一个用户的信息之外,还必须包含附加的服务信息,这两个部分加在一起叫做电子邮件的标准格式,外文简称RF ...

  6. WordPress插件-WP Mail SMTP

    WordPress 电子邮件 SMTP 寄件插件 在您的WordPress站点上遇到了无法发送邮件的问题?您并不孤单.目前有超过200万个网站正在使用WP Mail SMTP来可靠地发送邮件. 我们的 ...

  7. WP Mail SMTP配置谷歌邮箱

    最近发现网站注册时无法发送邮件给用户,查找了一些资料后发现原来主机不支持mail()函数,所以导致无法发送邮件.但是这个问题可以通过安装插件的方式进行解决,WP邮件SMTP是解决这个问题的一个常用插件 ...

  8. 使用 WP Mail SMTP 发送邮件给网站管理员

    前段时间客户提需求,需要在前端做个寻求合作的输入框,商户输入内容后以发邮件的方式将网店地址发送给管理员邮箱,再从邮箱里审核链接,从而避开因公开联系方式而导致经常收到垃圾电话或微信的烦恼 安装 WP M ...

  9. php pear mail smtp 不验证,怎么在php中使用pear_smtp实现一个邮件发送功能

    怎么在php中使用pear_smtp实现一个邮件发送功能 发布时间:2020-12-22 16:24:35 来源:亿速云 阅读:76 作者:Leah 怎么在php中使用pear_smtp实现一个邮件发 ...

最新文章

  1. oracle两张表 比较好,比较Oracle两张表的数据是否一样
  2. [译文]c#扩展方法(Extension Method In C#)
  3. 你在京东购买的商品,是怎么出现在抖音里面的?
  4. 离开时自动提示设为首页
  5. Vue nextTick 机制
  6. __METHOD__
  7. 一步步编写操作系统 24 编写内核加载器
  8. java php 等,路径 上级路径,上上级路径表示方法
  9. 试图登录,但是网络登陆服务没有启动成功
  10. paip.mysql 全文索引查询空白解决
  11. Citespace和vosviewer【理工、经管、法学、教育、农学、文史、医学、艺术】
  12. Win11谷歌的IDM插件用不了怎么解决?如何解决win11idm插件问题
  13. 2011-6-9 有趣的Google能发声音乐电吉他Logo
  14. mysql auto_increment 原理_mysql原理之Auto_increment
  15. 产品功能留存分析矩阵
  16. Java字母加数字组合比较大小
  17. Comet OJ - 2019国庆欢乐赛 G 后缀数组
  18. msm8953 LK通过cmdline向Kernel传递LCD参数过程分析
  19. WORD文档打开文件时老提示发送错误报告,或者打印不全
  20. SpringBoot项目中ModelMapper配置以及使用

热门文章

  1. C#界面里Form.Icon 属性的使用
  2. 模拟信号隔离放大器变送器 导轨安装DIN11 IPO EM系列
  3. c++ continue语句
  4. 新一配:全球每天消耗上百亿个二维码,一旦用完了怎么办?
  5. 历经五主而不衰的风流皇后:南朝萧皇后
  6. android软件路况软件,随身路况利器 安卓专用APP测试解析
  7. OpenAPI报错集合
  8. ObjectARX所有版本下载地址(新增2010下载地址)
  9. JZOJ-TGB817-SOL
  10. iview 带有Page分页组件集合的复杂表格