JavaMail发邮件是出现553 authentication is required,163 smtp10错误
白隐 2016-11-11 10:18:54 package cn.test.mail;
import java.util.Date;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
import org.junit.Test;
public class Test1 {
@Test
public void fun1() throws Exception {
// 配置文件对象
Properties props = new Properties();
// 邮箱服务地址
props.setProperty( "mail.smtp.host ", "smtp.163.com ");
// 是否进行验证
props.setProperty("mail.smtp.auth", "true");
// 创建一个会话
Session session = Session.getInstance(props);
// 打开调试,会打印与邮箱服务器回话的内容
session.setDebug(true);
Message message = new MimeMessage(session);
// 如果发送人没有写对,那么会出现 javamail 550 Invalid User
// 如果发送人写的和使用的帐号不一致,则会出现 553 Mail from must equal authorized user
InternetAddress from = new InternetAddress("wy_wsj@163.com");
from.setPersonal(MimeUtility.encodeText("<wy_wsj@163.com>"));
message.setFrom(from);
InternetAddress to = new InternetAddress("wy_wsj@sina.com");
message.setRecipient(Message.RecipientType.TO, to);
message.setSubject(MimeUtility.encodeText("强哥邀请,谁敢不从!"));
message.setText("强哥邀请你访问我的博客");
message.setSentDate(new Date());
Transport transport = session.getTransport("smtp");
// 具体你使用邮箱的smtp地址和端口,应该到邮箱里面查看,如果使用了SSL,网易的端口应该是 465/994
transport.connect("smtp.163.com", 25, "wy_wsj@163.com", "授权码");
transport.sendMessage(message, message.getAllRecipients());
transport.close();
System.out.println("发送完毕");
}
}
---------------------------------------------------------
package cn.test.mail;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message.RecipientType;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import org.junit.Test;
public class Demo1 {
@Test
public void fun1() throws Exception {
/*
* 得到session
*/
Properties prop = new Properties();
prop.setProperty("mail.smtp.host", "smtp.163.com");
prop.setProperty("smtp.mail.auth", "true");
Authenticator auth = new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("wy_wsj", "授权码");
}
};
Session session = Session.getInstance(prop, auth);
session.setDebug(true);
/*
* 创建MimeMessage
*/
MimeMessage msg = new MimeMessage(session);
//设置发件人
msg.setFrom(new InternetAddress("wy_wsj@163.com"));
//设置收件人
msg.setRecipient(RecipientType.TO, new InternetAddress("wy_wsj@sina.com"));
//设置标题
msg.setSubject("测试");
msg.setContent("测试邮箱", "text/html;charset=utf-8");
//发邮件
Transport.send(msg);
}
}
===================================================================
第一段代码就可以发送邮件成功
第二段代码就会出现:553 authentication is required,163 smtp10 错误
DEBUG: setDebug: JavaMail version 1.3.1
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth false
DEBUG SMTP: trying to connect to host "smtp.163.com", port 25
220 163.com Anti-spam GT for Coremail System (163com[20141201])
DEBUG SMTP: connected to host "smtp.163.com", port: 25
EHLO hi-PC
250-mail
250-PIPELINING
250-AUTH LOGIN PLAIN
250-AUTH=LOGIN PLAIN
250-coremail 1Uxr2xKj7kG0xkI17xGrU7I0s8FY2U3Uj8Cz28x1UUUUU7Ic2I0Y2UFh4DPvUCa0xDrUUUUj
250-STARTTLS
250 8BITMIME
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN"
DEBUG SMTP: Found extension "AUTH=LOGIN", arg "PLAIN"
DEBUG SMTP: Found extension "coremail", arg "1Uxr2xKj7kG0xkI17xGrU7I0s8FY2U3Uj8Cz28x1UUUUU7Ic2I0Y2UFh4DPvUCa0xDrUUUUj"
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: use8bit false
MAIL FROM:<wy_wsj@163.com>
553 authentication is required,163 smtp10,DsCowACnKpkEKSVYlYOkBw--.41210S2 1478830341
QUIT