开发环境:jdk 1.6
开发工具:myeclipse10
package Email;
import java.security.GeneralSecurityException;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;
import com.sun.mail.util.MailSSLSocketFactory;
public class Text1 extends Thread {
private String mailAdr;
private String content;
private String subject;
public Text1(String mailAdr, String subject, String content) {
// TODO Auto-generated constructor stub
super();
this.mailAdr = mailAdr;
this.subject = subject;
this.content = content;
}
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
try {
sendMail(mailAdr, subject, content);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void sendMail(String mailAdr, String subject, String content) throws Exception {
// TODO Auto-generated method stub
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
final Properties props = new Properties();
// 表示SMTP发送邮件,需要进行身份验证
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtp.163.com");
// 端口
/* props.put("mail.smtp.port", 25); */
// smtp登陆的账号、密码 ;需开启smtp登陆
props.setProperty("mail.debug", "true");
props.put("mail.user", "187********@163.com");
//smtp开启后生成的许可码
props.put("mail.password", "lll***");
//特别需要注意,要将ssl协议设置为true,否则会报530错误
props.put("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.ssl.socketFactory", sf);
Authenticator authenticator = new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
// 用户名、密码
String userName = props.getProperty("mail.user");
String password = props.getProperty("mail.password");
return new PasswordAuthentication(userName, password);
}
};
// 使用环境属性和授权信息,创建邮件会话
Session mailSession = Session.getInstance(props, authenticator);
// 创建邮件消息
MimeMessage message = new MimeMessage(mailSession);
// 设置发件人
try {
InternetAddress form = new InternetAddress(
props.getProperty("mail.user"));
message.setFrom(form);
// 设置收件人
InternetAddress to = new InternetAddress(mailAdr);
message.setRecipient(RecipientType.TO, to);
// 设置抄送
// InternetAddress cc = new InternetAddress("luo_aaaaa@yeah.net");
// message.setRecipient(RecipientType.CC, cc);
// 设置密送,其他的收件人不能看到密送的邮件地址
// InternetAddress bcc = new InternetAddress("aaaaa@163.com");
// message.setRecipient(RecipientType.CC, bcc);
// 设置邮件标题
message.setSubject(subject);
// 设置邮件的内容体
message.setContent(content, "text/html;charset=UTF-8");
// 发送邮件
Transport.send(message);
} catch (MessagingException e) {
e.printStackTrace();
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Text1 d = new Text1("3955*****@qq.com", "测试邮件标题",
"测试邮件001 <br>内容....");
d.start();
System.out.println("程序继续...");
}
}
代码来自:https://blog.csdn.net/w286554018/article/details/78667002
问题:
如图1所示,参考初始的https://blog.csdn.net/xuhailiang0816/article/details/78414211这篇文章,但是抛出
javax.mail.AuthenticationFailedException
查遍全网,都是SMTP设置的问题,密码需使用设置后生成的许可码,但是我再三确认没有问题。
2,转换思路,检查自身防火墙,telnet smtp.163.com 25,没有任何问题。
3,从头开始,参考https://blog.csdn.net/w286554018/article/details/78667002代码,抛出异常
没有加载资源:/META-INF/javamail.providers
豁然开朗,此类解决方案很多,大抵因为JavaEE的中邮类与邮件包冲突,无法指向邮件包所致,在JavaEE的中删掉邮件即可,问题解决。
4,异常抛出之所以不同,因为加入了props.setProperty(“mail.debug”,“true”),论异常准确抛出的重要性。
生产环境:office365,TLS加密方式
debug抛出:
Client was not authenticated to send anonymous mail during MAIL FROM
参考:https://blog.csdn.net/sinat_33324214/article/details/52910186
成功解决问题。