使用java mail发送邮件,配置多个smtp账户循环发送。其中一个账户能发送成功,其他账户都发送失败,出现如下错误:
java.lang.IllegalStateException: Can't overwrite cause
at java.lang.Throwable.initCause(Unknown Source)
at javax.mail.MessagingException.setNextException(MessagingException.java:51)
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1625)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1119)
找了半天也不知道啥原因。后来注意到,在session创建的时候,使用的是
public static Session getDefaultInstance(Properties props,
Authenticator authenticator)
API中有一段说明:
Note also that the Properties object is used only the first time this method is called, when a new Session object is created. Subsequent calls return the Session object that was created by the first call, and ignore the passed Properties object. Use the getInstance method to get a new Session object every time the method is called.
就是properties只初始化一次,以后都用同一个。据此,改用getInstance替代 getDefaultInstance,问题依旧。加Debug,发现如下错误:
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "SIZE", arg "52428800"
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN"
DEBUG SMTP: Found extension "AUTH=LOGIN", arg ""
DEBUG SMTP: Found extension "MAILCOMPRESS", arg ""
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Attempt to authenticate
DEBUG SMTP: check mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM
DEBUG SMTP: AUTH LOGIN command trace suppressed
DEBUG SMTP: AUTH LOGIN succeeded
DEBUG SMTP: use8bit false
MAIL FROM:<abccc@qxqpw.com>
501 mail from address must be same as authorization user
DEBUG SMTP: got response code 501, with response: 501 mail from address must be same as authorization user
原来是发件人填错了