一、前期准備
准備兩個qq郵箱號,本例使用的是自己的兩個郵箱
syc582535560@foxmail.com 2212444549@qq.com
下載javax.mail.jar jar包
如使用maven,在pom.xml里添加
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.1</version>
</dependency>
郵件的發送方要開啟POP3 和SMTP服務--即發送qq郵件的賬號要開啟POP3 和SMTP服務
登陸qq郵箱—點擊設置—點擊—賬戶—找到:POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服務 —點擊開啟
開啟后會得到一個授權碼! –注意:這個一定要記住,一會用到
建立測試代碼
public class Mail {
private static MimeMessage message;
public static void main(String[] args) throws MessagingException {
// Security.addProvider(new Provider());
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
//QQ郵箱服務器
String smtpHost="smtp.qq.com";
//郵箱用戶名,即QQ賬號(自定義)
final String username = "syc582535560@foxmail.com";
//郵箱授權碼(自定義)
final String password = "hafgyzlgfdzkbcdh";
//要發送到的郵箱(自定義)
String to = "2212444549@qq.com";
//自己的郵箱(自定義)
String from = "syc582535560@foxmail.com";
Transport transport;
Properties props = new Properties();
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.setProperty("mail.smtp.auth", "true");
props.put("mail.smtp.host",smtpHost);
props.put("mail.smtp.username", username);
props.put("mail.smtp.password", password);
Session session = Session.getDefaultInstance(props, new Authenticator() {
//身份認證
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
InternetAddress[] addresses = {new InternetAddress(to)};
message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO,addresses);
message.setSubject("singhead數據庫缺失的號碼段");//發送標題(自定義)
message.setSentDate(new Date());
message.setText("singhead數據庫缺失的號碼段");//發送內容(自定義)
transport = session.getTransport("smtp");
transport.connect(smtpHost, username, password);
transport.send(message);
System.out.println("email has been sent");
}
}
寫好測試代碼之后,點擊運行即可、
需下載mail.jar包
獲取16位SMTP命令步驟
默認是關閉的 ,將他開啟獲取16位SMTP命令
點擊進行驗證獲取16位SMTP命令
package com.zking.test;
import java.util.Properties;
import javax.mail.Authenticator;
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;
import javax.mail.internet.MimeMessage.RecipientType;
public class Yte {
public static void main(String[] args) throws AddressException,MessagingException {
// 創建Properties 類用於記錄郵箱的一些屬性
Properties props = new Properties();
// 表示SMTP發送郵件,必須進行身份驗證
props.put("mail.smtp.auth", "true");
//此處填寫SMTP服務器
props.put("mail.smtp.host", "smtp.qq.com");
//端口號,QQ郵箱端口587
props.put("mail.smtp.port", "587");
// 此處填寫,寫信人的賬號
props.put("mail.user", "xxxxxxxxxx@qq.com");
// 此處填寫16位STMP口令
props.put("mail.password", "xxxxxxxx");
// 構建授權信息,用於進行SMTP進行身份驗證
Authenticator authenticator = new Authenticator() {
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("xxxxxxxxxx@qq.com");
message.setRecipient(RecipientType.TO, to);
// 設置郵件標題
message.setSubject("標題");
// 設置郵件的內容體
message.setContent("內容", "text/html;charset=UTF-8");
// 最后當然就是發送郵件啦
Transport.send(message);
}
}