<code>
此代碼用的jar文件:mail.jar(我這里用的是1.4.7 的版本);
如果jdk用的是1.8版本會出現SSL錯誤:這個問題是jdk導致的,jdk1.8里面有一個jce的包,安全性機制導致的訪問https會報錯,官網上有替代的jar包,如果替換掉就可以了.
這兩個jar包的下載地址:http://www.oracle.com/technetwork/java/javase/downloads/jce-7-download-432124.html
下載之后,把這個壓縮文件解壓,得到兩個jar包去覆蓋jdk安裝目錄下的jre\lib\security\下相同的jar包就能解決java8的郵件發送問題。
</code>
<!-- 發送郵件的maven -->
<!-- https://mvnrepository.com/artifact/javax.mail/mail -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
<!--發送郵件的gradle-->
// https://mvnrepository.com/artifact/javax.mail/mail
compile group: 'javax.mail', name: 'mail', version: '1.4.7'
import com.sun.mail.util.MailSSLSocketFactory;
import org.apache.log4j.Logger;
import java.security.GeneralSecurityException;
import java.util.Properties;
/**
* Created by Mcin on 2017/5/18.
*/
public class ExMailUtil {
private static final Logger logger = Logger.getLogger(ExMailUtil.class);
// 這是騰訊企業郵箱的 如果是其他郵箱 自行更換
static String MAIL_TRANSPORT_PROTOCOL = "smtp"; //郵箱協議
static String MAIL_SMTP_HOST = "smtp.exmail.qq.com"; //發件服務器地址
static String MAIL_SMTP_PORT = "465"; // 端口
static String MAIL_SMTP_AUTH = "true"; //使用smtp身份驗證
/**
* 郵箱配置
*/
public static Properties setTencentExEmail (){
Properties prop = new Properties();
//協議
prop.setProperty("mail.transport.protocol", MAIL_TRANSPORT_PROTOCOL);
//服務器
prop.setProperty("mail.smtp.host", MAIL_SMTP_HOST);
//端口
prop.setProperty("mail.smtp.port", MAIL_SMTP_PORT);
//使用smtp身份驗證
prop.setProperty("mail.smtp.auth", MAIL_SMTP_AUTH);
//開啟安全協議 使用SSL,企業郵箱必需!
MailSSLSocketFactory sf = null;
try {
sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
} catch (GeneralSecurityException e1) {
e1.printStackTrace();
}
prop.put("mail.smtp.ssl.enable", "true");
prop.put("mail.smtp.ssl.socketFactory", sf);
return prop;
}
}
import org.apache.log4j.Logger;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Date;
/**
* Created by mcin on 2017/5/18.
* 發送企業郵箱
*/
public class SendMail {
private static final Logger logger = Logger.getLogger(SendMail.class);
final static String TO_EMAIL_ADDRESS = "收件人賬號郵箱"; // 收件人賬號郵箱賬號
final static String USER_NAME = "要登陸的企業郵箱賬號"; // 要登陸的企業郵箱賬號
final static String PASS_WORD = "要登陸的企業郵箱密碼"; / /要登陸的企業郵箱密碼
final static String SUBJECT = "這是企業郵箱發送的主題"; // 郵件主題
final static String CONTENT = "這是企業郵箱發送的內容"; // 郵件內容
static long startTime , endTime; // 用于計算發送的時間耗時
public static void main(String[] args) throws Exception {
//獲取Session對象
Session session = Session
.getDefaultInstance(
ExMailUtil
.setTencentExEmail(),
new Authenticator() {
//此訪求返回用戶和密碼的對象
@Override
protected PasswordAuthentication getPasswordAuthentication() {
PasswordAuthentication pa = new PasswordAuthentication(USER_NAME, PASS_WORD);
return pa;
}
});
//設置session的調試模式,發布時取消
// session.setDebug(true);
/*
// 有循環的情況下,如果實現群發的功能 比如 收件人方可以顯示到多少個收件用戶的
MimeMessage mimeMessage = new MimeMessage(session);
mimeMessage.setFrom(new InternetAddress(userName,userName));*/
for (int i = 0; i <10 ; i++) {
// 有循環的情況下,實現單獨發送的功能 收件人方只顯示自己的郵箱
MimeMessage mimeMessage = new MimeMessage(session);
mimeMessage.setFrom(new InternetAddress(USER_NAME,USER_NAME));
mimeMessage.addRecipient(Message
.RecipientType
.TO,
new InternetAddress(
TO_EMAIL_ADDRESS
));
//設置主題
mimeMessage.setSubject(SUBJECT);
mimeMessage.setSentDate(new Date());
//設置內容
mimeMessage.setText(CONTENT);
mimeMessage.saveChanges();
logger.info("***開始發送第 "+(i+1)+" 個郵件***");
startTime = System.currentTimeMillis();
try {
//發送
Transport.send(mimeMessage);
endTime = System.currentTimeMillis();
logger.info("第 "+(i+1)+" 個發送成功***耗時:"
+(endTime - startTime)/1000+" 秒");
logger.info("-------------------------------------------------------------");
} catch (MessagingException e) {
logger.error(e.getMessage());
continue;
}
}
}
}