目前使用javamail发送邮件通常使用25端口,因为25端口是一个简单的邮件发送协议,因此常常会被滥用发送垃圾邮件,所以在一些服务器好比阿里云上会封禁该端口的使用.java
解决的办法就是使用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; } }