java mail 发送qq企业邮箱报错

J2EE 码拜 9年前 (2016-03-19) 1249次浏览
import java.security.GeneralSecurityException;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import com.sun.mail.util.MailSSLSocketFactory;
public class MailSend {
private Properties props = new Properties();
private String from;
private Authenticator auth = null;
public void setForm(String from){
   this.from = from;
}
public void setSSLSecurity(){
	String SSL_FACTORY="javax.net.ssl.SSLSocketFactory"; 

    props.setProperty("mail.smtp.host","smtp.exmail.qq.com") ;
    props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
    props.setProperty("mail.smtp.socketFactory.fallback", "false") ;
    props.setProperty("mail.smtp.port","465") ;
    props.setProperty("mail.smtp.socketFactory.port","465") ;
    props.setProperty("mail.smtp.auth","true") ;
    props.put("mail.smtp.starttls.enable","true");    
   MailSSLSocketFactory sf = null;
try {
	sf = new MailSSLSocketFactory();
	sf.setTrustAllHosts(true); 
} catch (GeneralSecurityException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
}  
    
   // also use following for additional safety  
   //props.put("mail.smtp.ssl.checkserveridentity", "true");  
   props.put("mail.smtp.ssl.socketFactory", sf);  
}
//set security user name and password
public void setAuth(String userName, String userPassword){
   auth = new MailAuthenticator(userName, userPassword);
   setSSLSecurity();
   setForm(userName);
}
public boolean sendMail(String toAddr, String ccAddr, String subject, String message) throws AddressException, MessagingException{
    Session session = Session.getDefaultInstance(props, auth);
    Message msg = new MimeMessage(session);
    msg.setFrom(new InternetAddress(from));
    InternetAddress[] tos = InternetAddress.parse(toAddr);
    msg.setRecipients(Message.RecipientType.TO, tos);
    if(ccAddr != null){
     InternetAddress[] cc = InternetAddress.parse(ccAddr);
     msg.setRecipients(Message.RecipientType.CC, cc);
    }
    msg.setSubject(subject);
    msg.setText(message);
    Transport.send(msg);
    return true;
}
public static void main(String[] args) {
	MailSend ms = new MailSend();
	ms.setAuth("**@***.com", "*****");
	try {
		ms.sendMail("*****@qq.com", null, "test ssl", "test ssl runing");
	} catch (AddressException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (MessagingException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}
}

直接运行main方法是没问题的,,,是可以发送出去的,
但在web项目中使用就报错了,,,
导入的mail.jar是javamail-1.4.7.jar,从oracle官网上下载来的,
错误如下

javax.mail.AuthenticationFailedException: 550 ???*±????¨
	at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:826)
	at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:761)
	at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:685)
	at javax.mail.Service.connect(Service.java:317)
	at javax.mail.Service.connect(Service.java:176)
	at javax.mail.Service.connect(Service.java:125)
	at javax.mail.Transport.send0(Transport.java:194)
	at javax.mail.Transport.send(Transport.java:124)
	at com.ssc.sysUtil.qqMail.MailSend.sendMail(MailSend.java:73)
	at com.ssc.sysUtil.EmailSender.sendEmail(EmailSender.java:86)
	at com.ssc.service.user.UserServiceImpl.addBindEmail(UserServiceImpl.java:464)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:601)
	at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
	at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
	at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
	at com.sun.proxy.$Proxy11.addBindEmail(Unknown Source)
	at com.ssc.web.user.person.UserBaseInfo.sendBindEmail(UserBaseInfo.java:58)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:601)
	at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
	at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426)
	at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414)
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
	at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261)
	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581)
	at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
	at java.lang.Thread.run(Thread.java:722)
解决方案

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明java mail 发送qq企业邮箱报错
喜欢 (0)
[1034331897@qq.com]
分享 (0)