目前使用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; } }