阿里云ECS禁掉了25端口,造成了在服务器不能使用Java mail连接第三方邮件服务器,申请解封25端口时,也被拒绝,并建议使用465端口或云邮,因此只能使用SSL的发送方式,网上的很多代码都不能运行或出现异常,现总结一下,发出正确代码,并亲测通过,本地或服务器下均可正常发出,代码如下:
import java.util.Date;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import com.sun.mail.util.MailSSLSocketFactory;
public class SendMail {
public static void main(String[] args){
Properties prop = new Properties();
prop.setProperty("mail.transport.protocol", "smtp");
prop.setProperty("mail.smtp.host", "smtp.abc.cn");
prop.setProperty("mail.smtp.auth", "true");
prop.setProperty("mail.smtp.port", "465");
prop.setProperty("mail.smtp.socketFactory.port", "465");
prop.setProperty("mail.smtp.ssl.enable", "true");
try {
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
prop.put("mail.smtp.ssl.trust", "*");
prop.put("mail.smtp.ssl.socketFactory", sf);
// 创建session,并验证用户名和密码
Session session = Session.getDefaultInstance(prop, new Authenticator(){
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("sender", "123456");
}});
// 开启Session的debug模式,这样就可以查看到程序发送Email的运行状态
session.setDebug(true);
Message message = createSimpleMail(session, joinInfo);
Transport.send(message);
} catch (Exception e) {
e.printStackTrace();
}
}
public static MimeMessage createSimpleMail(Session session, JoinInfo joinInfo) throws Exception {
// 创建邮件对象
MimeMessage message = new MimeMessage(session);
// 指明邮件的发件人
message.setFrom(new InternetAddress("sender@abc.cn"));
// 指明邮件的收件人
message.setRecipient(Message.RecipientType.TO, new InternetAddress("addressee@abc.cn"));
// 邮件的标题
message.setSubject("这是标题!");
// 邮件的文本内容
message.setContent("这是邮件内容", "text/html;charset=UTF-8");
// 设置邮件消息发送的时间
message.setSentDate(new Date());
// 返回创建好的邮件对象
return message;
}
}
以上是代码内容,亲测OK!
网上很多类似的代码,但在发送时,会报以下异常:
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2211)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:740)
at javax.mail.Service.connect(Service.java:388)
at javax.mail.Service.connect(Service.java:246)
at javax.mail.Service.connect(Service.java:195)
at javax.mail.Transport.send0(Transport.java:254)
at javax.mail.Transport.send(Transport.java:124)
…
就是找不到SSL的证书,当然可以上传第三方邮件的证书(如何上传就不多说了,网上说的很多!),但也可以不用这么麻烦,重点是这些代码:
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
prop.put("mail.smtp.ssl.trust", "*");// *为信任所有网站,也可以单独填写第三方邮件的HOST,如:mail.abc.cn
prop.put("mail.smtp.ssl.socketFactory", sf); // 信任站点证书
备注:
1、需要引入java的mail.jar包,如果需要发送附件,则需要多引入一个activation.jar包;
2、linux环境下,不需要在防火墙上特意放开465端口;