Java mail无法发送邮件,阿里云服务器禁用25端口,只能使用SSL方式发送!

阿里云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端口;

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值