package com.fr.function;
import java.io.IOException;
import java.security.Security;
import java.util.Date;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import com.sun.mail.util.MailSSLSocketFactory;
public class MailTest {
public static void main(String[] args) throws Exception {
MailUtil.sendEmil465("*****@qq.com", "国家能源集团邮箱测试4");
}
/**
* 使用加密的方式,利用465端口进行传输邮件,开启ssl
* @param to 为收件人邮箱
* @param message 发送的消息
*/
public static void sendEmil465(String to, String message) {
try {
final String smtpHost="mail.chnenergy.com.cn";
final String smtpPort="465";
final String username = "****@chnenergy.com.cn";
final String password = "****";
final String subject="国家能源集团邮件发送测试5";
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
//设置邮件会话参数
Properties props = new Properties();
//邮箱的发送服务器地址
props.setProperty("mail.smtp.host", smtpHost);
props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.setProperty("mail.smtp.socketFactory.fallback", "false");
//SSL认证,注意腾讯邮箱是基于SSL加密的,所有需要开启才可以使用,很多人不成功是因为漏写了下面的代码
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
props.put("mail.smtp.ssl.socketFactory", sf);
props.setProperty("mail.smtp.ssl.enable", "true");
//邮箱发送服务器端口,这里设置为465端口
props.setProperty("mail.smtp.port", smtpPort);
props.setProperty("mail.smtp.socketFactory.port", smtpPort);
props.put("mail.smtp.auth", "true");
//获取到邮箱会话,利用匿名内部类的方式,将发送者邮箱用户名和密码授权给jvm
Session session = Session.getDefaultInstance(props, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
session.setDebug(true);
//通过会话,得到一个邮件,用于发送
Message msg = new MimeMessage(session);
//设置发件人
msg.setFrom(new InternetAddress(username));
//设置收件人,to为收件人,cc为抄送,bcc为密送
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
//msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(to, false));
//msg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(to, false));
msg.setSubject(subject);
//设置邮件消息
msg.setText(message);
//设置发送的日期
msg.setSentDate(new Date());
//调用Transport的send方法去发送邮件
Transport.send(msg);
} catch (Exception e) {
e.printStackTrace();
}
}
}
465端口失败原因,注释
In earlier releases it was necessary to explicitly set a socket
factory property to enable use of SSL. In almost all cases, this
is no longer necessary. SSL support is built in. However, there
is one case where a special socket factory may be needed.
JavaMail now includes a special SSL socket factory that can simplify
dealing with servers with self-signed certificates. While the
recommended approach is to include the certificate in your keystore
as described above, the following approach may be simpler in some cases.
The class com.sun.mail.util.MailSSLSocketFactory can be used as a
simple socket factory that allows trusting all hosts or a specific set
of hosts. For example:
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
// or
// sf.setTrustedHosts(new String[] { "my-server" });
props.put("mail.smtp.ssl.enable", "true");
// also use following for additional safety
//props.put("mail.smtp.ssl.checkserveridentity", "true");
props.put("mail.smtp.ssl.socketFactory", sf);
Use of MailSSLSocketFactory avoids the need to add the certificate to
your keystore as described above, or configure your own TrustManager
as described below.(使用MailSSLSocketFactory避免了需要添加证书,你的密钥库如上所述,或配置自己的TrustManager。如下所述。