1、测试代码:
import java.io.IOException;
import javax.mail.MessagingException;
import javax.mail.Session;
import org.junit.Test;
import cn.itcast.mail.Mail;
import cn.itcast.mail.MailUtils;
/**
* 测试MailUtils,作用是发邮件
* 底层依赖的是javamail:mail.jar、activation.jar
* @author miaoxingren
*
*/
public class MailUtilsTest {
/**
* 发邮件
* @throws IOException
* @throws MessagingException
*/
@Test
public void sent() throws MessagingException, IOException{
/*
* 1.登录邮件服务器
* MailUtils.createSession(服务器地址,登录名,密码);
* 2.创建邮件对象
* 发件人
* 收件人
* 主题
* 正文
* 3.发
* 需要第一步得到session、和第二步的邮件对象
*/
Session session = MailUtils.createSession("smtp.163.com", "xxx", "xxx");
Mail mail = new Mail("xxx1@163.com", "xxx2@163.com", "测试邮件一封", "<a href='http://www.baidu.com'>百度</a>");
MailUtils.send(session, mail);
}
}
2、使用的jar包情况:
3、出现的异常信息:
javax.mail.AuthenticationFailedException: 535 Error: authentication failed
at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:648)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:583)
at javax.mail.Service.connect(Service.java:313)
at javax.mail.Service.connect(Service.java:172)
at javax.mail.Service.connect(Service.java:121)
at javax.mail.Transport.send0(Transport.java:190)
at javax.mail.Transport.send(Transport.java:120)
at cn.itcast.mail.MailUtils.send(MailUtils.java:91)
at MailUtilsTest.sent(MailUtilsTest.java:42)
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.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
4、注:①邮箱的账号密码都正确,并且开启了SMTP协议
②试过底层删除mail和activation文件夹,方法无效
③目前使用的是另一种解决办法:将Java EE 5 Library 改为6的 上述出现的异常就是这种办法的
查了很久,还是没解决,希望哪位大神帮忙看看,帮我这个菜鸟解决下,好人一生平安,谢谢!