javamail发件时报错Client was not authenticated to send anonymous mail

时间:2024-03-19 22:10:16

利用javamai发件时,报错如下:

com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM [HK2PR02CA0208.apcprd02.prod.outlook.com]

	at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2267)
	at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1758)
	at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1257)
	at com.xhl.leads.leadsmail.sendmail.service.impl.SendMailServiceImpl.sendHtmlMailNoRecordCat(SendMailServiceImpl.java:425)
	at com.xhl.leads.leadsmail.sendmail.service.impl.SendMailServiceImpl$$FastClassBySpringCGLIB$$581c17fa.invoke(<generated>)
	at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:738)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
	at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:85)
	at com.xhl.leads.leadsmail.common.aspect.ControllerMethodExecutionLogAspect.catTransactionProcess(ControllerMethodExecutionLogAspect.java:61)
	at sun.reflect.GeneratedMethodAccessor154.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:629)
	at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:618)
	at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:70)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
	at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
	at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:673)
	at com.xhl.leads.leadsmail.sendmail.service.impl.SendMailServiceImpl$$EnhancerBySpringCGLIB$$6c128f52.sendHtmlMailNoRecordCat(<generated>)
	at com.xhl.leads.leadsmail.sendmail.controller.SendMailController$2.run(SendMailController.java:275)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)

解决办法:

1.添加props.setProperty("mail.smtp.starttls.enable","true");属性

2.连接session时,用PasswordAuthentication获取session实例。

Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");// "smtp"
props.setProperty("mail.smtp.host", host);// smtp.qq.com
props.setProperty("mail.smtp.port", port);// 465
props.setProperty("mail.smtp.auth", "true");// "true"
props.setProperty("mail.smtp.starttls.enable","true");
Session session = Session.getInstance(props, new javax.mail.Authenticator() {
		protected PasswordAuthentication getPasswordAuthentication() {
		                return new PasswordAuthentication(from, passWord);
		}
});

即:在应用中绑定邮箱时,勾选"STARTTLS加密传输"。

javamail发件时报错Client was not authenticated to send anonymous mail

题外话,我试了用PasswordAuthentication去拿session实例,但是发现不是所有邮箱都能用PasswordAuthentication来获取session实例。所以我做了判断:如果用户勾选了"STARTTLS加密传输",我就用PasswordAuthentication来获取session,否则直接获取普通的session,即:session = Session.getDefaultInstance(props);

逻辑代码如下:

if("1".equals(mailAccount.getEncryptionTransmissionFlag())) {
	session = Session.getInstance(props, new javax.mail.Authenticator() {
		     protected PasswordAuthentication getPasswordAuthentication() {
		            return new PasswordAuthentication(from, passWord);
		     }
	});
}else {
	session = Session.getDefaultInstance(props);
}