Java发送网易企业邮箱邮件
/**
此方法使用smtp协议
查看网易企业邮箱各种协议的主机和端口可访问https://qiye.163.com/help/client-profile.html查看
**/
public void sendEmail() throws Exception {
String host = "smtphz.qiye.163.com";
String username = "xxx"; // 发件人的用户名
String password = "xxx"; // 发件人的密码
String subject = "subject"; // 邮件标题
String content = "你好,这是一封测试邮件,无需回复";
Properties properties = new Properties();
properties.put("mail.smtp.auth",true);
properties.put("mail.smtp.port",465);
properties.put("mail.smtp.timeout",60000);
properties.put("mail.smtp.ssl.enable",true);
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
properties.put("mail.smtp.ssl.socketFactory",sf);
JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage,true,"utf-8");
String nick = MimeUtility.encodeText("测试");
messageHelper.setFrom(new InternetAddress(nick+"<"+userName+">")); // 格式要求,强制
messageHelper.setTo("xxxx@163.com"); // 收件人邮箱
messageHelper.setSubject(subject);
messageHelper.setText(content,true);
javaMailSender.setHost(host);
javaMailSender.setUsername(userName);
javaMailSender.setPassword(password);
// javaMailSender.setPort(465); 此方法默认的邮箱协议为smtp
javaMailSender.setJavaMailProperties(properties);
javaMailSender.send(mimeMessage);
}
问题:
1、遇到用户不存在的问题,可能是因为网易服务器地址错误
2、遇到javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or…)问题,可以查看 https://blog.csdn.net/Beyourselfsun/article/details/127956774?spm=1001.2014.3001.5502