package com.mail.test;import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;public class SendMail {
public SendMail() {
} public static void main(String[] args) {
try {
Authenticator auth = new PopupAuthenticator();
Properties mailProps = new Properties();
mailProps.put("mail.smtp.host", "smtp.163.com");
mailProps.put("mail.smtp.auth", "true");
mailProps.put("username", "donglei271267415");
mailProps.put("password", "haidaopaopao");
Session mailSession = Session.getDefaultInstance(mailProps, auth);
MimeMessage message = new MimeMessage(mailSession);
message.setFrom(new InternetAddress("[email protected]"));
message.setRecipient(Message.RecipientType.TO, new InternetAddress(
"[email protected]"));
message.setSubject("Mail Test");
MimeMultipart multi = new MimeMultipart();
BodyPart textBodyPart = new MimeBodyPart();
textBodyPart.setText("Hello World!");
multi.addBodyPart(textBodyPart);
message.setContent(multi);
message.saveChanges();
Transport.send(message);
} catch (Exception ex) {
System.err.println("邮件发送失败的原因是:" + ex.getMessage());
System.err.println("具体错误原因:");
ex.printStackTrace(System.err);
}
}
}class PopupAuthenticator extends Authenticator {
// public PasswordAuthentication getPasswordAuthentication() {
// String username = "donglei271267415"; // 163邮箱登录帐号
// String pwd = "haidaopaopao"; // 登录密码
// return new PasswordAuthentication(username, pwd);
//
// }
String username = "donglei271267415"; // 163邮箱登录帐号
String pwd = "haidaopaopao"; // 登录密码 public PopupAuthenticator() {
super();
} public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, pwd);
}
}
为什么一直发送不了!报错!
邮件发送失败的原因是:Sending failed;
nested exception is:
class javax.mail.MessagingException: 553 You are not authorized to send mail, authentication is required具体错误原因:
javax.mail.SendFailedException: Sending failed;
nested exception is:
class javax.mail.MessagingException: 553 You are not authorized to send mail, authentication is required at javax.mail.Transport.send0(Transport.java:218)
at javax.mail.Transport.send(Transport.java:80)
at com.mail.test.SendMail.main(SendMail.java:31)
import javax.mail.internet.*;
import java.util.*;public class SendMail {
public SendMail() {
} public static void main(String[] args) {
try {
Authenticator auth = new PopupAuthenticator();
Properties mailProps = new Properties();
mailProps.put("mail.smtp.host", "smtp.163.com");
mailProps.put("mail.smtp.auth", "true");
mailProps.put("username", "donglei271267415");
mailProps.put("password", "haidaopaopao");
Session mailSession = Session.getDefaultInstance(mailProps, auth);
MimeMessage message = new MimeMessage(mailSession);
message.setFrom(new InternetAddress("[email protected]"));
message.setRecipient(Message.RecipientType.TO, new InternetAddress(
"[email protected]"));
message.setSubject("Mail Test");
MimeMultipart multi = new MimeMultipart();
BodyPart textBodyPart = new MimeBodyPart();
textBodyPart.setText("Hello World!");
multi.addBodyPart(textBodyPart);
message.setContent(multi);
message.saveChanges();
Transport.send(message);
} catch (Exception ex) {
System.err.println("邮件发送失败的原因是:" + ex.getMessage());
System.err.println("具体错误原因:");
ex.printStackTrace(System.err);
}
}
}class PopupAuthenticator extends Authenticator {
// public PasswordAuthentication getPasswordAuthentication() {
// String username = "donglei271267415"; // 163邮箱登录帐号
// String pwd = "haidaopaopao"; // 登录密码
// return new PasswordAuthentication(username, pwd);
//
// }
String username = "donglei271267415"; // 163邮箱登录帐号
String pwd = "haidaopaopao"; // 登录密码 public PopupAuthenticator() {
super();
} public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, pwd);
}
}
为什么一直发送不了!报错!
邮件发送失败的原因是:Sending failed;
nested exception is:
class javax.mail.MessagingException: 553 You are not authorized to send mail, authentication is required具体错误原因:
javax.mail.SendFailedException: Sending failed;
nested exception is:
class javax.mail.MessagingException: 553 You are not authorized to send mail, authentication is required at javax.mail.Transport.send0(Transport.java:218)
at javax.mail.Transport.send(Transport.java:80)
at com.mail.test.SendMail.main(SendMail.java:31)
mail.skyinfocn.com
你要用认证登录才可以。
具体写法不写了:看这个类: javax.mail.Authenticator;
自己百度。
改成这样就可以了:
String username = "[email protected]"; // 163邮箱登录帐号
设置传输协议javax.mail.Transport transport = mailSession.getTransport("smtp");
然后用 transport.connect(host, userName, pwd);方法访问邮件服务器
试一下吧
还有问题可以看我blog
http://blog.csdn.net/viszl/archive/2010/09/02/5857909.aspx