在编写发送邮件代码时候遇到以下异常
com.sun.mail.smtp.SMTPSendFailedException: 550 Invalid User
;
nested exception is:
com.sun.mail.smtp.SMTPSenderFailedException: 550 Invalid User
false
at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2109)
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1610)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1118)
at javax.mail.Transport.send0(Transport.java:195)
at javax.mail.Transport.send(Transport.java:124)
at com.xmh.SendMailUtils.sendMail(SendMailUtils.java:124)
at com.xmh.test.SendMailUtilsTest.SendMailTest(SendMailUtilsTest.java:14)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)
Caused by: com.sun.mail.smtp.SMTPSenderFailedException: 550 Invalid User
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1617)
... 28 more
查找问题半天,在网上搜相关信息也没到有用的,在此做个记录。
这个错误的描述可以看出是不可用的用户,但是指向的代码出处是发送邮件的那行
Transport.send(message);
让问题变得很难找。用一个账号显示这个错误,用其他账号测试,同样报这个错误,并确认了两个账号都正确,就排除了账号问题。继续排查,后来终于找到根源:
是在进行发件人设置的时候,将发件人的地址设置为了发件人用户名
如下:
message.setFrom(new InternetAddress(username));
应该改为
message.setFrom(new InternetAddress(sendAddress));
sendAddress就是邮箱地址了