IMAP邮箱
huifer opened this issue · 0 comments
huifer commented
package com.example.demo.entity;
import com.sun.mail.imap.IMAPFolder;
import com.sun.mail.imap.IMAPMessage;
import com.sun.mail.imap.IMAPStore;
import javax.mail.*;
import javax.mail.search.FlagTerm;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.Security;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* IMAP 协议
*/
public class ImapFetchMail {
public static void main(String[] args) throws Exception {
String host = "imap.qq.com";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
int port = 993;
String username = "qq邮箱地址";
String password = "填写密码";
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
Properties props = System.getProperties();
props.setProperty("mail.imap.socketFactory.class", SSL_FACTORY);
props.setProperty("mail.imap.socketFactory.port", "993");
props.setProperty("mail.store.protocol", "imap");
props.setProperty("mail.imap.host", host);
props.setProperty("mail.imap.port", "993");
props.setProperty("mail.imap.auth.login.disable", "true");
Session session = Session.getDefaultInstance(props, null);
session.setDebug(false);
IMAPFolder folder = null;
IMAPStore store = null;
String regex = "[a-zA-Z0-9_-]+@\\w+\\.[a-z]+(\\.[a-z]+)?";
Pattern p = Pattern.compile(regex);
IMAPFolder inbox = null;
try {
store = (IMAPStore) session.getStore("imap"); // 使用imap会话机制,连接服务器
store.connect(host, port, username, password);
Folder inbox1 = store.getFolder("INBOX");
inbox1.open(Folder.READ_WRITE);
FlagTerm ft =
new FlagTerm(new Flags(Flags.Flag.SEEN), false); //false代表未读,true代表已读
Message[] messages = inbox1.getMessages();
Message[] messages1 = messages;
Message[] search = inbox1.search(ft);
for (Message message : search) {
IMAPMessage imes = (IMAPMessage) message;
StringBuffer content = new StringBuffer(30);
getAllMultipart(message, content);
HashMap<String, String> res = new HashMap<>();
res.put("title", imes.getSubject());
res.put("messageId", imes.getMessageID());
res.put("context", content.toString());
res.put("send_time", sdf.format(imes.getSentDate()));
Matcher m = p.matcher(String.valueOf(imes.getFrom()[0]));
res.put("from", m.find() ? m.group() : "");
System.out.println(res);
}
System.out.println();
} catch (NoSuchProviderException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
} finally {
try {
if (folder != null) {
folder.close(false);
}
if (store != null) {
store.close();
}
} catch (MessagingException e) {
e.printStackTrace();
}
}
System.out.println("接收完毕!");
}
private static void getAllMultipart(Part part, StringBuffer sb) throws Exception {
String contentType = part.getContentType();
int index = contentType.indexOf("name");
boolean conName = false;
if (index != -1) {
conName = true;
}
//判断part类型
if (part.isMimeType("text/plain") && !conName) {
sb.append((String) part.getContent());
} else if (part.isMimeType("text/html") && !conName) {
sb.append((String) part.getContent());
} else if (part.isMimeType("multipart/*")) {
Multipart multipart = (Multipart) part.getContent();
int counts = multipart.getCount();
for (int i = 0; i < counts; i++) {
//递归获取数据
getAllMultipart(multipart.getBodyPart(i), sb);
//附件可能是截图或上传的(图片或其他数据)
if (multipart.getBodyPart(i).getDisposition() != null) {
//附件为截图
if (multipart.getBodyPart(i).isMimeType("image/*")) {
InputStream is = multipart.getBodyPart(i)
.getInputStream();
String name = multipart.getBodyPart(i).getFileName();
String fileName;
//截图图片
if (name.startsWith("=?")) {
fileName = name.substring(name.lastIndexOf(".") - 1, name.lastIndexOf("?="));
} else {
//上传图片
fileName = name;
}
FileOutputStream fos = new FileOutputStream("E:\\"
+ fileName);
int len = 0;
byte[] bys = new byte[1024];
while ((len = is.read(bys)) != -1) {
fos.write(bys, 0, len);
}
fos.close();
} else {
//其他附件
InputStream is = multipart.getBodyPart(i)
.getInputStream();
String name = multipart.getBodyPart(i).getFileName();
FileOutputStream fos = new FileOutputStream("E:\\"
+ name);
int len = 0;
byte[] bys = new byte[1024];
while ((len = is.read(bys)) != -1) {
fos.write(bys, 0, len);
}
fos.close();
}
}
}
} else if (part.isMimeType("message/rfc822")) {
getAllMultipart((Part) part.getContent(), sb);
}
}
}