一、QQ郵箱準備
在QQ郵箱設置中,開啓POP3/SMTP服務,並生成授權碼
二、新建項目
1.新建項目並引入javax.mail的jar包,這裏給出maven的配置
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
2.貼出項目代碼
package mail;
import java.security.GeneralSecurityException;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
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;
/**
* 發送郵件的測試程序
*
* @author lwq
*
*/
public class MailTest {
public static void main(String[] args) throws MessagingException {
// 配置發送郵件的環境屬性
final Properties props = new Properties();
/*
* 可用的屬性: mail.store.protocol / mail.transport.protocol / mail.host /
* mail.user / mail.from
*/
// 表示SMTP發送郵件,需要進行身份驗證
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtp.qq.com");
props.put("mail.smtp.port", "465");
// 發送郵件協議名稱
props.setProperty("mail.transport.protocol", "smtp");
//創建ssl安全鏈接
MailSSLSocketFactory sf;
try {
sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
props.put("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.ssl.socketFactory", sf);
} catch (GeneralSecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 發件人的賬號
props.put("mail.user", "******@qq.com");
// 訪問SMTP服務時需要提供的密碼
props.put("mail.password", "授權碼-16 ");
// 構建授權信息,用於進行SMTP進行身份驗證
Authenticator authenticator = new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
// 用戶名、密碼
String userName = props.getProperty("mail.user");
String password = props.getProperty("mail.password");
return new PasswordAuthentication(userName, password);
}
};
// 使用環境屬性和授權信息,創建郵件會話
Session mailSession = Session.getInstance(props, authenticator);
// 創建郵件消息
MimeMessage message = new MimeMessage(mailSession);
// 設置發件人
InternetAddress form = new InternetAddress(
props.getProperty("mail.user"));
message.setFrom(form);
// 設置收件人
InternetAddress to = new InternetAddress("*********@qq.com");
message.setRecipient(RecipientType.TO, to);
// 設置抄送
InternetAddress cc = new InternetAddress("[email protected]");
message.setRecipient(RecipientType.CC, cc);
// 設置密送,其他的收件人不能看到密送的郵件地址
InternetAddress bcc = new InternetAddress("[email protected]");
message.setRecipient(RecipientType.CC, bcc);
// 設置郵件標題
message.setSubject("測試");
// 設置郵件的內容體
message.setContent("9999999999", "text/html;charset=UTF-8");
// 發送郵件
Transport.send(message);
}
}
三、注意的問題
1.代碼中的郵箱密碼並不是QQ密碼,而是授權碼
2.之前參考的網易郵箱代碼,總是出現javax.mail.AuthenticationFailedException: 220 Ready to start TLS的錯誤,需要在代碼中加入ssl安全鏈接的代碼
3.騰訊給出了兩個的端口號,建議使用465端口
4.jdk版本問題,總是拋出異常Could not connect to SMTP host: smtp.qq.com, port: 465;
nested exception is: javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
而後在網上查找到了簡書上的一篇資料:http://www.jianshu.com/p/5ba3bde60f21
大抵是由於jdk1.8裏面有一個jce包,由於安全性的問題訪問https時會報錯,我們在官網上找到可替代的包替換掉即可,或者把jdk換成1.7版本的。下載地址:
http://www.oracle.com/technetwork/java/javase/downloads/jce-7-download-432124.html
解壓完后里面有兩個jar包,我們在jdk目錄下按照 jdk\jre\lib\security路徑,將裏面原有的兩個包替換掉即可。