package com.tdy.tiger.utils;
/**
* ClassName: TestSendMail
* Description:
* date: 2022/2/7 14:44
*
* @author robotname a
* @author dev whz
* @since JDK 1.8
*/
import com.sun.mail.smtp.SMTPAddressFailedException;
import javax.mail.*;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.Properties;
public class TestSendMail {
public static void main(String[] args) {
Properties props = new Properties();
props.put("mail.smtp.host", "192.168.0.94");
props.put("mail.smtp.port", 25);
props.put("mail.smtp.auth", false);
Authenticator auth = new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("wanghongzhan@qq.com",
"mima");
}
};
props.put("mail.smtp.host", "192.168.0.94");
props.put("mail.smtp.auth", "true");
props.put("mail.from", "wanghongzhan@qq.com");//发件人名称
props.put("mail.smtp.reportsuccess", true);
Session session = Session.getInstance(props, auth);
session.setDebug(false);
try {
MimeMessage msg = new MimeMessage(session);
msg.setFrom();
msg.setRecipients(Message.RecipientType.TO, "wanghon@qq.com");
msg.setSubject("JavaMail hello world example");
msg.setSentDate(new Date());
msg.setText("Hello, world!\n");
Transport.send(msg);
} catch (SMTPAddressFailedException ex) {
// 250是发送OK的 捕获到250 Mail OK
// 550 User not found: 461503821@163.com 550错误 只能获取到Invalid
// Addresses没有发现这个邮箱
System.out
.println("SMTPAddressFailedException---------------------------"
+ ex.getReturnCode());
System.out
.println("SMTPAddressFailedException---------------------------"
+ ex.getCause().getMessage());
} catch (MessagingException mex) {
System.out.println("MessagingException------" + mex.getMessage());
}
}
}
主要是用的这个异常 SMTPAddressFailedException
参考链接
https://blog.csdn.net/mouhk/article/details/8244665
https://www.iteye.com/blog/lijie-insist-2223823 这个特别全