0 0

web项目中页面修改javamail发件人信息后发邮件失败5

我们的web项目使用javamail发送邮件。页面可修改发送邮件的服务器、发件人信息,但是现在在页面修改发件人邮箱地址和密码之后,发送邮件失败。重起Tomcat服务器之后,又可以正常发送邮件,各位大神,有谁知道为什么呢?
public boolean sendMail(String subject, String body){
		Properties props = new Properties();
		props.put("mail.smtp.host", server);
		props.put("mail.smtp.auth", needAuth);
		props.put("mail.smtp.sender", sender);
		props.put("mail.smtp.password", password);
		
		// 判断是否需要身份认证
		MailAuthenticator authenticator = null;
		if (needAuth) {
			authenticator = new MailAuthenticator(sender, password);
		}
		
		try {
			Session session = Session.getDefaultInstance(props, authenticator);
			MimeMessage message = new MimeMessage(session);

			InternetAddress from = null;
			if(null != sender && !"".equals(sender)){
				from = new InternetAddress(sender);
			}
			
			message.setFrom(from);
			String[] str = to.split(",");
			InternetAddress[] address = new InternetAddress[str.length]; 
			for (int i = 0; i < str.length; i++) {
				address[i] = new InternetAddress(str[i]);
			}
			message.setRecipients(Message.RecipientType.TO,address);
			
			message.setSubject(subject, "UTF-8");
			message.setSentDate(new Date());
			MimeBodyPart mbp = new MimeBodyPart();
			mbp.setContent(body, "text/html;charset=UTF-8");
			Multipart multipart = new MimeMultipart();
			multipart.addBodyPart(mbp);
			message.setContent(multipart);
			Transport.send(message);
			
			return true;
		} catch (Exception e) {
			return false;
		}

问题补充:修改发件人之后,报如下异常:

com.sun.mail.smtp.SMTPSendFailedException: 550 Invalid User 684966a9-37c9-4b09-ba32-ea282cf46b58
;
  nested exception is:
com.sun.mail.smtp.SMTPSenderFailedException: 550 Invalid User 684966a9-37c9-4b09-ba32-ea282cf46b58

at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2057)
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1580)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1097)
at javax.mail.Transport.send0(Transport.java:195)
at javax.mail.Transport.send(Transport.java:146)
at com.rptauto.util.MailUtil.sendMail(MailUtil.java:164)
at com.rptauto.service.impl.MonitorProcessServiceImpl.sendIdleNofity(MonitorProcessServiceImpl.java:552)
at com.rptauto.service.impl.MonitorProcessServiceImpl.dealIdleStart(MonitorProcessServiceImpl.java:286)
at com.rptauto.service.impl.MonitorProcessServiceImpl.monitorTimepoint(MonitorProcessServiceImpl.java:220)
at com.rptauto.service.impl.MonitorProcessServiceImpl$$FastClassByCGLIB$$5de188e1.invoke(<generated>)
at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:149)
at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:696)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:631)
at com.rptauto.service.impl.MonitorProcessServiceImpl$$EnhancerByCGLIB$$a602d879.monitorTimepoint(<generated>)
at com.rptauto.service.webservice.ClientMainServiceImpl.monitorTimepoint(ClientMainServiceImpl.java:134)
at com.rptauto.service.webservice.ClientMainServiceImpl$$FastClassByCGLIB$$6696aa53.invoke(<generated>)
at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:149)
at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:696)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:631)
2014年9月25日 15:28

2个答案 按时间排序 按投票排序

0 0

  authenticator = new MailAuthenticator(sender, password); 

Session session = Session.getDefaultInstance(props, authenticator); 

我猜应该是这两行代码有问题:
1、你看看你的 sender, password 作用域范围是什么?是不是静态变量?如果你要动态修改生效,那肯定是要每次都获取最新的值,或者如果你用缓存的话你在修改后需要同步修改缓存的值。

第二行代码我也不知道怎么检查。你跟踪看看。

2014年9月26日 17:08
0 0

你能定位到发送失败的时候是哪一行报的问题吗?可以加些log或者自己debug跟踪一下,needAuth这个值是不是为null的时候会触发一些运行时异常呢?

2014年9月25日 15:47

相关推荐

Global site tag (gtag.js) - Google Analytics