javamail 发送邮件时 No authentication mechansims supported by both server and client

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javamail 发送邮件时 No authentication mechansims supported by both server and client相关的知识,希望对你有一定的参考价值。

项目需求要更换发邮件服务器,变更成中转邮箱服务器发邮件。结果总是报 No authentication mechansims supported by both server and client 。 

搜了一堆东东,都不靠谱最后自己不断调试成功搞定。借鉴网址如下:

http://stackoverflow.com/questions/8615730/javamail-with-ms-exchange-no-authentication-mechansims-supported-by-both-server

原因:中转服务器不需要验证用户名密码啥的,所以链接的时候 

MAIL_SMTP_AUTH 要设为 false。

transport.connect();//中转服务器邮箱不验证  连接的时候不要传东西,哪怕是 null 或 空 。它会认为你要验证。

(首先保证自己的发邮件服务器和应用服务器想通的。并且邮件服务器端口正确,默认是25(telnet 192.168.1.114 25))

javax.mail.AuthenticationFailedException: No authentication mechansims supported by both server and client
        at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:765)
        at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:685)
        at javax.mail.Service.connect(Service.java:274)
        at javax.mail.Service.connect(Service.java:91)
        at com.zhaopin.common.mail.util.MailSendUtil.<clinit>(MailSendUtil.java:108)
        at com.zhaopin.cdcpm.thread.MailThread.run(MailThread.java:17)
=================begin send mail=======================
15:06:31,232 ERROR [com.zhaopin.common.mail.util.MailSendUtil] (com.zhaopin.common.mail.util.MailSendUtil:155) - When sending mail, here catch exception!!!!! 
javax.mail.AuthenticationFailedException: No authentication mechansims supported by both server and client
        at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:765)
        at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:685)
        at javax.mail.Service.connect(Service.java:274)
        at javax.mail.Service.connect(Service.java:91)
        at com.zhaopin.common.mail.util.MailSendUtil.send(MailSendUtil.java:135)
        at com.zhaopin.cdcpm.thread.MailThread.run(MailThread.java:17)
javax.mail.AuthenticationFailedException: No authentication mechansims supported by both server and client
        at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:765)
        at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:685)
        at javax.mail.Service.connect(Service.java:274)
        at javax.mail.Service.connect(Service.java:91)
        at com.zhaopin.common.mail.util.MailSendUtil.send(MailSendUtil.java:135)
        at com.zhaopin.cdcpm.thread.MailThread.run(MailThread.java:17)

  

static {
		log.info("----- start init mail environment -----");
		Authenticator auth = new Authenticator() {
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(MailConfig.getMailFromUserName(), MailConfig.getMailFromUserPswd());
			}
		};
	
		Properties props = new Properties();
	
		// 设置发送邮件的邮件服务器的属性
		props.put(MAIL_SMTP_HOST, MailConfig.getMailHost());
	
		// 需要经过授权,也就是有户名和密码的校验,这样才能通过验证(一定要有这一条)
		props.put(MAIL_SMTP_AUTH, MailConfig.isValidate());
	
		// 用刚刚设置好的props对象构建一个session
//		session = Session.getDefaultInstance(props, auth);
		session = Session.getDefaultInstance(props, null);//中转服务器邮箱不验证
	
		try {
			// 发送邮件
			transport = session.getTransport("smtp");
			// 连接服务器的邮箱
//			transport.connect(MailConfig.getMailHost(), MailConfig.getMailFromUserName(), MailConfig.getMailFromUserPswd());
			transport.connect();//中转服务器邮箱不验证
			log.info("----- Connect with " + MailConfig.getMailHost() + " successfully -----");
		} catch (Exception e) {
			log.error("----- Connect with " + MailConfig.getMailHost() + " failed -----", e);
		}
	}

  

以上是关于javamail 发送邮件时 No authentication mechansims supported by both server and client的主要内容,如果未能解决你的问题,请参考以下文章

JavaMail发送邮件应添加客户端授权密码而不是登陆密码

使用JavaMail发送邮件,465端口开启ssl加密传输

javamail Authenticator 的构造函数是如何工作的? [复制]

javamail使用小记

java James

springbootJavaMail发送邮件