一、配置
smtp協定可以用來發送郵件,IMAP協定可以用來讀取郵件。QQ郵箱設定中開啟POP3協定。發送郵件javaMail-發送郵件
二、代碼實作
參看了一篇幾年前的部落格,拿過來進行了一些改造,隻是一個基版,一些郵箱的東西什麼可以放到配置檔案中,因暫時沒有需求,暫時這樣,記錄一下,用到在進行更新改造。(POP3是雙向的,如果你要進行删除操作,郵件伺服器上也會删除郵件)java代碼網上部落格很多了,這兩天拿來研究一下,做個筆記
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
/**
* 使用POP3協定解析郵件幫助類
*
* @author bzs on 2018/6/28.
*/
@Slf4j
public class ParsingEmailUtil {
public static void main(String[] args) throws Exception{
resceive("**********@163.com", "*****");
}
/**
* 擷取郵箱資訊
*
* @param emailAdress 需要解析的郵箱位址
* @param password 郵箱的授權密碼
* @throws Exception
*/
public static void resceive(String emailAdress, String password) throws Exception {
String port = "110"; // 端口号
String servicePath = "pop.163.com"; // 伺服器位址
// 準備連接配接伺服器的會話資訊
Properties props = new Properties();
props.setProperty("mail.store.protocol", "pop3"); // 使用pop3協定
props.setProperty("mail.pop3.port", port); // 端口
props.setProperty("mail.pop3.host", servicePath); // pop3伺服器
// 建立Session執行個體對象
Session session = Session.getInstance(props);
Store store = session.getStore("pop3");
store.connect(emailAdress, password); //163郵箱程式登入屬于第三方登入是以這裡的密碼是163給的授權密碼而并非普通的登入密碼
// 獲得收件箱
Folder folder = store.getFolder("INBOX");
/* Folder.READ_ONLY:隻讀權限
* Folder.READ_WRITE:可讀可寫(可以修改郵件的狀态)
*/
folder.open(Folder.READ_WRITE); //打開收件箱
// // 由于POP3協定無法獲知郵件的狀态,是以getUnreadMessageCount得到的是收件箱的郵件總數
// System.out.println("未讀郵件數: " + folder.getUnreadMessageCount());
//
// // 由于POP3協定無法獲知郵件的狀态,是以下面得到的結果始終都是為0
// System.out.println("删除郵件數: " + folder.getDeletedMessageCount());
// System.out.println("新郵件: " + folder.getNewMessageCount());
// 獲得收件箱中的郵件總數
log.warn("郵件總數: {}", folder.getMessageCount());
// 得到收件箱中的所有郵件,并解析
Message[] messages = folder.getMessages();
//解析郵件
parseMessage(messages);
//得到收件箱中的所有郵件并且删除郵件
// deleteMessage(messages);
//釋放資源
folder.close(true);
store.close();
}
/**
* 解析郵件
*
* @param messages 要解析的郵件清單
*/
public static void parseMessage(Message... messages) throws MessagingException, IOException {
if (messages == null || messages.length < 1)
throw new MessagingException("未找到要解析的郵件!");
// 解析所有郵件
for (int i = 0, count = messages.length; i < count; i++) {
MimeMessage msg = (MimeMessage) messages[i];
log.info("------------------解析第" + msg.getMessageNumber() + "封郵件-------------------- ");
log.warn("主題: {}" , getSubject(msg));
log.warn("發件人: {}" , getFrom(msg));
log.warn("收件人:{}" , getReceiveAddress(msg, null));
log.warn("發送時間:{}" , getSentDate(msg, null));
log.warn("是否已讀:{}" , isSeen(msg));
log.warn("郵件優先級:{}" , getPriority(msg));
log.warn("是否需要回執:{}" , isReplySign(msg));
log.warn("郵件大小:{}" , msg.getSize() * 1024 + "kb");
boolean isContainerAttachment = isContainAttachment(msg);
log.warn("是否包含附件:{}" ,isContainerAttachment);
if (isContainerAttachment) {
saveAttachment(msg, "d:\\log\\" + msg.getSubject() + "_" + i + "_"); //儲存附件
}
StringBuffer content = new StringBuffer(30);
//解析郵件正文
getMailTextContent(msg, content);
log.warn("郵件正文:{}" , content);
log.info("------------------第" + msg.getMessageNumber() + "封郵件解析結束-------------------- ");
System.out.println();
}
}
/**
* 删除郵件
*
* @param messages 要删除郵件清單
*/
public static void deleteMessage(Message... messages) throws MessagingException, IOException {
if (messages == null || messages.length < 1)
throw new MessagingException("未找到要解析的郵件!");
// 解析所有郵件
for (int i = 0, count = messages.length; i < count; i++) {
/**
* 郵件删除
*/
Message message = messages[i];
String subject = message.getSubject();
// set the DELETE flag to true
message.setFlag(Flags.Flag.DELETED, true);
System.out.println("Marked DELETE for message: " + subject);
}
}
/**
* 獲得郵件主題
*
* @param msg 郵件内容
* @return 解碼後的郵件主題
*/
public static String getSubject(MimeMessage msg) throws UnsupportedEncodingException, MessagingException {
return MimeUtility.decodeText(msg.getSubject());
}
/**
* 獲得郵件發件人
*
* @param msg 郵件内容
* @return 姓名 <Email位址>
* @throws MessagingException
* @throws UnsupportedEncodingException
*/
public static String getFrom(MimeMessage msg) throws MessagingException, UnsupportedEncodingException {
String from = "";
Address[] froms = msg.getFrom();
if (froms.length < 1)
throw new MessagingException("沒有發件人!");
InternetAddress address = (InternetAddress) froms[0];
String person = address.getPersonal();
if (person != null) {
person = MimeUtility.decodeText(person) + " ";
} else {
person = "";
}
from = person + "<" + address.getAddress() + ">";
return from;
}
/**
* 根據收件人類型,擷取郵件收件人、抄送和密送位址。如果收件人類型為空,則獲得所有的收件人
* <p>Message.RecipientType.TO 收件人</p>
* <p>Message.RecipientType.CC 抄送</p>
* <p>Message.RecipientType.BCC 密送</p>
*
* @param msg 郵件内容
* @param type 收件人類型
* @return 收件人1 <郵件位址1>, 收件人2 <郵件位址2>, ...
* @throws MessagingException
*/
public static String getReceiveAddress(MimeMessage msg, Message.RecipientType type) throws MessagingException {
StringBuffer receiveAddress = new StringBuffer();
Address[] addresss = null;
if (type == null) {
addresss = msg.getAllRecipients();
} else {
addresss = msg.getRecipients(type);
}
if (addresss == null || addresss.length < 1)
throw new MessagingException("沒有收件人!");
for (Address address : addresss) {
InternetAddress internetAddress = (InternetAddress) address;
receiveAddress.append(internetAddress.toUnicodeString()).append(",");
}
receiveAddress.deleteCharAt(receiveAddress.length() - 1); //删除最後一個逗号
return receiveAddress.toString();
}
/**
* 獲得郵件發送時間
*
* @param msg 郵件内容
* @return yyyy年mm月dd日 星期X HH:mm
* @throws MessagingException
*/
public static String getSentDate(MimeMessage msg, String pattern) throws MessagingException {
Date receivedDate = msg.getSentDate();
if (receivedDate == null)
return "";
if (pattern == null || "".equals(pattern))
pattern = "yyyy年MM月dd日 E HH:mm ";
return new SimpleDateFormat(pattern).format(receivedDate);
}
/**
* 判斷郵件中是否包含附件
*
* @param part 郵件内容
* @return 郵件中存在附件傳回true,不存在傳回false
* @throws MessagingException
* @throws IOException
*/
public static boolean isContainAttachment(Part part) throws MessagingException, IOException {
boolean flag = false;
if (part.isMimeType("multipart/*")) {
MimeMultipart multipart = (MimeMultipart) part.getContent();
int partCount = multipart.getCount();
for (int i = 0; i < partCount; i++) {
BodyPart bodyPart = multipart.getBodyPart(i);
String disp = bodyPart.getDisposition();
if (disp != null && (disp.equalsIgnoreCase(Part.ATTACHMENT) || disp.equalsIgnoreCase(Part.INLINE))) {
flag = true;
} else if (bodyPart.isMimeType("multipart/*")) {
flag = isContainAttachment(bodyPart);
} else {
String contentType = bodyPart.getContentType();
if (contentType.indexOf("application") != -1) {
flag = true;
}
if (contentType.indexOf("name") != -1) {
flag = true;
}
}
if (flag) break;
}
} else if (part.isMimeType("message/rfc822")) {
flag = isContainAttachment((Part) part.getContent());
}
return flag;
}
/**
* 判斷郵件是否已讀
*
* @param msg 郵件内容
* @return 如果郵件已讀傳回true, 否則傳回false
* @throws MessagingException
*/
public static boolean isSeen(MimeMessage msg) throws MessagingException {
return msg.getFlags().contains(Flags.Flag.SEEN);
}
/**
* 判斷郵件是否需要閱讀回執
*
* @param msg 郵件内容
* @return 需要回執傳回true, 否則傳回false
* @throws MessagingException
*/
public static boolean isReplySign(MimeMessage msg) throws MessagingException {
boolean replySign = false;
String[] headers = msg.getHeader("Disposition-Notification-To");
if (headers != null)
replySign = true;
return replySign;
}
/**
* 獲得郵件的優先級
*
* @param msg 郵件内容
* @return 1(High):緊急 3:普通(Normal) 5:低(Low)
* @throws MessagingException
*/
public static String getPriority(MimeMessage msg) throws MessagingException {
String priority = "普通";
String[] headers = msg.getHeader("X-Priority");
if (headers != null) {
String headerPriority = headers[0];
if (headerPriority.indexOf("1") != -1 || headerPriority.indexOf("High") != -1)
priority = "緊急";
else if (headerPriority.indexOf("5") != -1 || headerPriority.indexOf("Low") != -1)
priority = "低";
else
priority = "普通";
}
return priority;
}
/**
* 獲得郵件文本内容
*
* @param part 郵件體
* @param content 存儲郵件文本内容的字元串
* @throws MessagingException
* @throws IOException
*/
public static void getMailTextContent(Part part, StringBuffer content) throws MessagingException, IOException {
//如果是文本類型的附件,通過getContent方法可以取到文本内容,但這不是我們需要的結果,是以在這裡要做判斷
boolean isContainTextAttach = part.getContentType().indexOf("name") > 0;
if (part.isMimeType("text/*") && !isContainTextAttach) {
content.append(part.getContent().toString());
} else if (part.isMimeType("message/rfc822")) {
getMailTextContent((Part) part.getContent(), content);
} else if (part.isMimeType("multipart/*")) {
Multipart multipart = (Multipart) part.getContent();
int partCount = multipart.getCount();
for (int i = 0; i < partCount; i++) {
BodyPart bodyPart = multipart.getBodyPart(i);
getMailTextContent(bodyPart, content);
}
}
}
/**
* 儲存附件
*
* @param part 郵件中多個組合體中的其中一個組合體
* @param destDir 附件儲存目錄
* @throws UnsupportedEncodingException
* @throws MessagingException
* @throws FileNotFoundException
* @throws IOException
*/
public static void saveAttachment(Part part, String destDir) throws MessagingException, IOException {
if (part.isMimeType("multipart/*")) {
Multipart multipart = (Multipart) part.getContent(); //複雜體郵件
//複雜體郵件包含多個郵件體
int partCount = multipart.getCount();
for (int i = 0; i < partCount; i++) {
//獲得複雜體郵件中其中一個郵件體
BodyPart bodyPart = multipart.getBodyPart(i);
//某一個郵件體也有可能是由多個郵件體組成的複雜體
String disp = bodyPart.getDisposition();
if (disp != null && (disp.equalsIgnoreCase(Part.ATTACHMENT) || disp.equalsIgnoreCase(Part.INLINE))) {
InputStream is = bodyPart.getInputStream();
saveFile(is, destDir, decodeText(bodyPart.getFileName()));
} else if (bodyPart.isMimeType("multipart/*")) {
saveAttachment(bodyPart, destDir);
} else {
String contentType = bodyPart.getContentType();
if (contentType.indexOf("name") != -1 || contentType.indexOf("application") != -1) {
saveFile(bodyPart.getInputStream(), destDir, decodeText(bodyPart.getFileName()));
}
}
}
} else if (part.isMimeType("message/rfc822")) {
saveAttachment((Part) part.getContent(), destDir);
}
}
/**
* 讀取輸入流中的資料儲存至指定目錄
*
* @param is 輸入流
* @param fileName 檔案名
* @param destDir 檔案存儲目錄
* @throws FileNotFoundException
* @throws IOException
*/
private static void saveFile(InputStream is, String destDir, String fileName) throws FileNotFoundException, IOException {
BufferedInputStream bis = new BufferedInputStream(is);
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(new File(destDir + fileName)));
int len = -1;
while ((len = bis.read()) != -1) {
bos.write(len);
bos.flush();
}
bos.close();
bis.close();
}
/**
* 文本解碼
*
* @param encodeText 解碼MimeUtility.encodeText(String text)方法編碼後的文本
* @return 解碼後的文本
* @throws UnsupportedEncodingException
*/
public static String decodeText(String encodeText) throws UnsupportedEncodingException {
if (encodeText == null || "".equals(encodeText)) {
return "";
} else {
return MimeUtility.decodeText(encodeText);
}
}
}
public void readMail(){
try {
// 1. 設定連接配接資訊, 生成一個 Session
Properties props = new Properties();
// 用空的 Properties也行這裡
props.setProperty("mail.smtp.host", "smtp.qq.com");
props.setProperty("mail.smtp.auth", "true");
// 解決郵件下載下傳慢的問題,因為我郵件下載下傳速度很快,是以沒有驗證這段代碼
//props.setProperty("mail.imap.partialfetch", "false");
//props.setProperty("mail.imaps.partialfetch", "false");
Session session = Session.getDefaultInstance(props);
// 2. 擷取 Store 并連接配接到伺服器
// 設定連接配接相關資訊,file 可以直接指定郵箱的某個檔案夾
// 郵件在郵件伺服器上是在每個使用者名下,設定不同的檔案,收件箱預設為“INBOX”
// URLName urlname = new URLName("pop3","pop.qq.com",110,"INBOX","[email protected]","你的授權碼");
URLName urlname = new URLName("pop3","pop.qq.com",110,null,"[email protected]","授權碼");
Store store = session.getStore(urlname);
store.connect();
// 預設父目錄
Folder folder = store.getDefaultFolder();
if (folder == null) {
System.out.println("伺服器不可用");
return;
}
String defaultFolderName = folder.getName();
System.out.println("預設信箱名:" + defaultFolderName);
// 預設目錄清單
Folder[] folders = folder.list();
System.out.println("預設目錄下的子目錄數: " + folders.length);
System.out.println("-----根目錄下的包含的檔案名-----");
for(int i = 0; i < folders.length; i++) {
System.out.println("【目錄下的檔案名】:【" + folders[i].getFullName() + "】");
}
System.out.println("---------------");
// 擷取收件箱
Folder popFolder = folder.getFolder("INBOX");
// 可讀郵件,可以删郵件的模式打開目錄
popFolder.open(Folder.HOLDS_FOLDERS);
// 4. 列出來收件箱 下所有郵件
Message[] messages = popFolder.getMessages();
// 取出來郵件數
int msgCount = popFolder.getMessageCount();
System.out.println("共有郵件: " + msgCount + "封");
// FetchProfile fProfile = new FetchProfile();// 選擇郵件的下載下傳模式,
// 根據網速選擇不同的模式
// fProfile.add(FetchProfile.Item.ENVELOPE);
// folder.fetch(messages, fProfile);// 選擇性的下載下傳郵件
// 5. 循環處理每個郵件并實作郵件轉為新聞的功能
for (int i = 0; i < msgCount; i++) {
// 單個郵件
System.out.println("第" + i +"封郵件開始");
mailReceiver(messages[i]);
System.out.println("第" + i +"封郵件結束");
//郵件讀取用來校驗
messages[i].writeTo(new FileOutputStream("D:/pop3MailReceiver"+ i +".eml"));
}
// 7. 關閉 Folder 會真正删除郵件, false 不删除
popFolder.close(false);
// 8. 關閉 store, 斷開網絡連接配接
store.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 解析郵件
*/
private void mailReceiver(Message msg)throws Exception{
System.out.println("【郵件ID】:【" + msg.getMessageNumber() + "】");
System.out.println("【郵件主題】:【" + MimeUtility.decodeText(msg.getSubject()) + "】");
System.out.println("【郵件發送時間】:【" + this.getSentDate(msg) + "】");
// 發件人資訊
Address[] froms = msg.getFrom();
if(froms != null) {
InternetAddress addr = (InternetAddress)froms[0];
System.out.println("【發件人位址】:【" + addr.getAddress() + "】");
System.out.println("【發件人顯示名】:【" + addr.getPersonal() + "】");
}
// System.out.println("【收件人】:【" + msg.getRecipients(Message.RecipientType.TO) + "】");
System.out.println("【是否需要回複】:【" + this.needReplyState(msg) + "】");
System.out.println("【是否需要回複】:【" + this.isNew(msg) + "】");
// getContent() 是擷取包裹内容, Part相當于外包裝
Object o = msg.getContent();
if(o instanceof Multipart) {
Multipart multipart = (Multipart) o ;
reMultipart(multipart);
} else if (o instanceof Part){
Part part = (Part) o;
rePart(part);
} else {
System.out.println("【郵件内容類型】:【" + msg.getContentType() + "】");
System.out.println("【内容】:【" + msg.getContent() + "】");
}
}
/**
* @param part 解析内容
* @throws Exception
*/
private void rePart(Part part) throws MessagingException, IOException {
if (part.getDisposition() != null) {
String strFileNmae = MimeUtility.decodeText(part.getFileName());
//MimeUtility.decodeText解決附件名亂碼問題
System.out.println("【發現附件】:【 " + MimeUtility.decodeText(part.getFileName()) + "】");
System.out.println("【内容類型】:【 " + MimeUtility.decodeText(part.getContentType()) + "】");
System.out.println("【附件内容】:【" + part.getContent() + "】");
InputStream in = part.getInputStream();
// 打開附件的輸入流
// 讀取附件位元組并存儲到檔案中
java.io.FileOutputStream out = new FileOutputStream(strFileNmae);
int data;
while((data = in.read()) != -1) {
out.write(data);
}
in.close();
out.close();
} /*else {
if(part.getContentType().startsWith("text/plain")) {
System.out.println("文本内容:" + part.getContent());
} else {
//System.out.println("HTML内容:" + part.getContent());
}
}*/
}
/**
* @param multipart // 接卸包裹(含所有郵件内容(包裹+正文+附件))
* @throws Exception
*/
private void reMultipart(Multipart multipart) throws Exception {
//System.out.println("郵件共有" + multipart.getCount() + "部分組成");
// 依次處理各個部分
for (int j = 0, n = multipart.getCount(); j < n; j++) {
//System.out.println("處理第" + j + "部分");
Part part = multipart.getBodyPart(j);//解包, 取出 MultiPart的各個部分, 每部分可能是郵件内容,
// 也可能是另一個小包裹(MultipPart)
// 判斷此包裹内容是不是一個小包裹, 一般這一部分是 正文 Content-Type: multipart/alternative
if (part.getContent() instanceof Multipart) {
Multipart p = (Multipart) part.getContent();// 轉成小包裹
//遞歸疊代
reMultipart(p);
} else {
rePart(part);
}
}
}
/**
*擷取郵件日期
*/
private String getSentDate(Message message){
String emailSendDate = "郵件發送的時間出錯";
try {
Date date = message.getSentDate();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
emailSendDate = format.format(date);
} catch (MessagingException e) {
e.printStackTrace();
}
return emailSendDate;
}
public String needReplyState(Message message) throws MessagingException {
String replyFlag = "此郵件不需要回複";
String [] needReply = message.getHeader("Disposition-Notification-To");
if (null != needReply){
replyFlag = "郵件需要回複";
}
return replyFlag;
}
public String isNew(Message message) throws MessagingException {
Flags flags = message.getFlags();
Flags.Flag[] flag = flags.getSystemFlags();
for (int i = 0; i<flag.length;i++){
if (Flags.Flag.SEEN == flag[i]){
return "郵件未讀";
}
}
return "郵件已讀";
}
/**
* @param args
*/
public static void main(String[] args) {
MailUtil mailUtil = new MailUtil();
mailUtil.readMail();
}