javamail使用SSL加密方式465端口
2017-05-11 15:42
344 查看
目前使用javamail发送邮件一般使用25端口,由于25端口是一个简单的邮件发送协议,所以经常会被滥用发送垃圾邮件,因此在一些服务器比如阿里云上会封禁该端口的使用.
解决的办法就是使用465端口:465端口是为SMTPS(SMTP-over-SSL)协议服务开放的,这是SMTP协议基于SSL安全协议之上的一种变种协议,它继承了SSL安全协议的非对称加密的高度安全可靠性,可防止邮件泄露。SMTPS和SMTP协议一样,也是用来发送邮件的,只是更安全些,防止邮件被黑客截取泄露,还可实现邮件发送者抗抵赖功能。防止发送者发送之后删除已发邮件,拒不承认发送过这样一份邮件。
发送至多人邮箱:
解决的办法就是使用465端口:465端口是为SMTPS(SMTP-over-SSL)协议服务开放的,这是SMTP协议基于SSL安全协议之上的一种变种协议,它继承了SSL安全协议的非对称加密的高度安全可靠性,可防止邮件泄露。SMTPS和SMTP协议一样,也是用来发送邮件的,只是更安全些,防止邮件被黑客截取泄露,还可实现邮件发送者抗抵赖功能。防止发送者发送之后删除已发邮件,拒不承认发送过这样一份邮件。
package com.diannuo.util.emailUtil; import java.security.Security; import java.util.Date; import java.util.Properties; 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; public class MailBySSL{ public static boolean sendMailBySSL() throws AddressException, MessagingException{ Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory"; // Get a Properties object Properties props = new Properties(); props.setProperty("mail.smtp.host", "smtp.163.com"); props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY); props.setProperty("mail.smtp.socketFactory.fallback", "false"); props.setProperty("mail.smtp.port", "465"); props.setProperty("mail.smtp.socketFactory.port", "465"); props.put("mail.smtp.auth", "true"); final String username = "506865679@163.com"; final String password = "xxxxxxx"; Session session = Session.getDefaultInstance(props, new Authenticator(){ protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); }}); // -- Create a new message -- Message msg = new MimeMessage(session); // -- Set the FROM and TO fields -- msg.setFrom(new InternetAddress("506865679@163.com")); msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse("506865679@qq.com",false)); msg.setSubject("你好,这是来自本地11111服务器"); msg.setText("来自测试邮件"); msg.setSentDate(new Date()); Transport.send(msg); System.out.println("Message sent."); return true; } }
发送至多人邮箱:
package com.diannuo.util.emailUtil; import java.io.IOException; import java.io.InputStream; 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; public class MailBySSL{ private static Properties p = new Properties(); private static InputStream is = MailBySSL.class .getResourceAsStream("/config.properties"); public static boolean sendMailBySSL(String sender,String text) throws AddressException, MessagingException, IOException{ Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory"; // Get a Properties object Properties props = new Properties(); props.setProperty("mail.smtp.host", "smtp.163.com"); props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY); props.setProperty("mail.smtp.socketFactory.fallback", "false"); props.setProperty("mail.smtp.port", "465"); props.setProperty("mail.smtp.socketFactory.port", "465"); props.put("mail.smtp.auth", "true"); p.load(is); final String username = p.getProperty("userName"); final String password = p.getProperty("password"); Session session = Session.getDefaultInstance(props, new Authenticator(){ protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); }}); // 创建邮件 Message msg = new MimeMessage(session); // 设置发件人和收件人 msg.setFrom(new InternetAddress(p.getProperty("userName"))); // 多个收件人地址 String[] add = p.getProperty("receiver").split(","); Address[] addArr = new InternetAddress[add.length]; for(int i=0;i<add.length;i++){ addArr[i] = new InternetAddress(add[i]); } msg.setRecipients(Message.RecipientType.TO, addArr); msg.setSubject(sender); // 标题 msg.setText(text);// 内容 msg.setSentDate(new Date()); Transport.send(msg); return true; } }
相关文章推荐
- JavaMail使用SSL的方式登录
- 使用JavaMail发送邮件,并使用465端口开启ssl加密传输,保证邮件安全
- JavaMail实现收发邮件(五)使用SSL实现加密传输
- JavaMail使用465端口发送SSL加密邮件
- com.microsoft.sqlserver.jdbc.SQLServerException: 驱动程序无法使用安全套接字层(SSL)加密与 SQL Server 建立安全连接。错误: RSA pr
- .NET中使用BouncyCastle动态库实现RSA方式加密方法
- Web.config加密——使用编程方式方式
- 谷歌下周起默认使用SSL加密搜索页面
- 搜狗浏览器使用google加密ssl搜索
- 如何通过HTTPS(SSL加密)方式访问webservice
- 转载:Linux下使用wpa_supplicant连接WPA加密方式的WIFI
- Apache mod_jk方式使用ajp13均衡集群整合Tomcat后的ssl配置
- 驱动程序无法通过使用安全套接字层(SSL)加密与 SQL Server 建立 安全连接。错误:“SQL Server 返回的响应不完整。连接已关闭
- 驱动程序无法通过使用安全套接字层(SSL)加密与SQL Server 建立安全连接
- 将代码加密方式之一使用静态链接库
- sqlite的使用方式和加密方式
- 使用ssl加密的IIS客户端证书访问配置
- 使用openssl进行ssl/tls加密传输会话测试
- 让Chrome使用https加密方式浏览指定网站
- 【IOS学习之路】关于DES加密 神州付中使用到的DES加密方式