Skip to content

Instantly share code, notes, and snippets.

@Forgo7ten
Created February 3, 2022 12:19
Show Gist options
  • Save Forgo7ten/69aece486c326d5b3d0901687c68f56d to your computer and use it in GitHub Desktop.
Save Forgo7ten/69aece486c326d5b3d0901687c68f56d to your computer and use it in GitHub Desktop.
Java Mail示例
/**
* @ClassName MyMail
* @Description 封装好的半成品MyMail
* @Author Palmer
* @Date 2021/11/27
**/
public class MyMail {
private String mUserAddr;
private String mPassword;
private IMAPStore mStore = null;
public MyMail(String mUserAddr, String mPassword) {
this.mUserAddr = mUserAddr;
this.mPassword = mPassword;
this.connect();
}
public String getmUserAddr() {
return mUserAddr;
}
public void setmUserAddr(String mUserAddr) {
this.mUserAddr = mUserAddr;
}
public String getmPassword() {
return mPassword;
}
public void setmPassword(String mPassword) {
this.mPassword = mPassword;
}
public IMAPStore getmStore() {
return mStore;
}
public void setmStore(IMAPStore mStore) {
this.mStore = mStore;
}
public Store connect() {
// 准备连接服务器的会话信息
Properties props = new Properties();
props.setProperty("mail.store.protocol", "imap");
props.setProperty("mail.imap.host", "imap.163.com");
props.setProperty("mail.imap.port", "143");
//带上IMAP ID信息,由key和value组成,例如name,version,vendor,support-email等。
Map<String, String> iam = new HashMap<String, String>() {
{
put("name", mUserAddr.split("@")[0]);
put("version", "1.0.0");
put("vendor", "my_java_client");
put("support-email", mUserAddr);
}
};
// 创建Session实例对象
Session session = Session.getInstance(props);
try {
// 创建IMAP协议的Store对象
mStore = (IMAPStore) session.getStore("imap");
// 连接邮件服务器
mStore.connect(mUserAddr, mPassword);
mStore.id(iam);
} catch (MessagingException e) {
e.printStackTrace();
}
return mStore;
}
public List<Message> getMailsInFolder(String folderName) {
List<Message> messageList = new ArrayList<>();
try {
Folder folder = this.mStore.getFolder(folderName);
// 以只读模式打开邮件
folder.open(Folder.READ_ONLY);
Message[] messages = folder.getMessages();
messageList.addAll(Arrays.asList(messages));
} catch (MessagingException e) {
e.printStackTrace();
}
return messageList;
}
}
/**
* @ClassName MyMailTest
* @Description 解析邮件并保存本地
* @Author Palmer
* @Date 2021/11/27
**/
public class MyMailTest {
public static final String XW_FILE = "校外邮寄地址整理.csv";
public static final String XN_FILE = "校内奖品整理.csv";
public static final String LOG_FILE = "log.txt";
public static List<Map<String, String>> xwInfos = new ArrayList<>();
public static List<Map<String, String>> xnInfos = new ArrayList<>();
public static StringBuffer logInfo = new StringBuffer();
public static void parseXwMail(String content, String subject, String fromEmail) {
content = content.replace("\r\n\r\n", "\n").replace("\r\n", "\n").replace("\r", "").replace("&nbsp;", " ");
String[] rows = content.split("\n");
String splitCh = "=";
String id = "";
String code = "";
String to = "";
String iphone = "";
String address = "";
try {
for (String row : rows) {
if (row.contains("获奖ID")) {
id = row.split(splitCh)[1].trim();
} else if (row.contains("在线验证码")) {
code = row.split(splitCh)[1].trim();
} else if (row.contains("收件人")) {
to = row.split(splitCh)[1].trim();
} else if (row.contains("手机号码")) {
iphone = row.split(splitCh)[1].trim();
} else if (row.contains("收货地址")) {
address = row.split(splitCh)[1].trim();
}
}
String mid = id;
String mcode = code;
String mto = to;
String miphone = iphone;
String maddress = address;
/*System.out.println("获奖ID = " + mid + "\n"
+ "验证码 = " + mcode + "\n"
+ "收件人 = " + mto + "\n"
+ "手机号码 = " + miphone + "\n"
+ "收货地址 = " + maddress);*/
HashMap<String, String> xwInfo = new HashMap<String, String>() {{
put("id", mid);
put("code", mcode);
put("to", mto);
put("iphone", miphone);
put("address", maddress);
put("from_email", fromEmail);
}};
xwInfos.add(xwInfo);
} catch (Exception e) {
logInfo.append(" --->> " + subject + " 解析出错!<<---\n");
}
}
private static void parseXnMail(String content, String subject, String fromEmail) {
content = content.replace("\r\n\r\n", "\n").replace("\r\n", "\n").replace("\r", "").replace("&nbsp;", " ");
String[] rows = content.split("\n");
int len = rows.length;
String splitCh = "=";
String tid = "";
String ttotal = "";
StringBuffer tmpPrize = new StringBuffer();
try {
for (String row : rows) {
if (row.contains("许愿池奖品")) {
String prize = row.split(splitCh)[1].split("金额")[0];
String money = row.split(splitCh)[1].split("金额")[1];
tmpPrize.append(prize.trim());
tmpPrize.append("(").append(money.trim()).append(") | ");
} else if (row.contains("奖品总计")) {
ttotal = row.split("总计")[1].trim();
} else if (row.contains("获奖ID")) {
tid = row.split(splitCh)[1].trim();
}
}
tmpPrize.delete(tmpPrize.length() - 3, tmpPrize.length());
String prize = tmpPrize.toString().trim().replace("\r", "").replace("&nbsp;", "");
String id = tid;
String total = ttotal;
/* System.out.println("获奖ID = " + id + "\n"
+ "奖品列表 = " + prize + "\n"
+ "总金额 = " + total);*/
HashMap<String, String> xnInfo = new HashMap<String, String>() {{
put("id", id);
put("prize", prize);
put("total", total);
put("from_email", fromEmail);
}};
xnInfos.add(xnInfo);
} catch (Exception e) {
logInfo.append(" --->> " + subject + " 解析出错!<<---\n");
}
}
public static void checkMail(Message messageMail, boolean xwFlag) {
IMAPMessage email = (IMAPMessage) messageMail;
try {
// 打印邮件标题
// System.out.println("subject: "+email.getSubject());
// 打印第一个发件人
// System.out.println("From: "+email.getFrom()[0].toString());
logInfo.append("标题:" + email.getSubject() + " 发件人:" + email.getFrom()[0].toString().split(" ")[1] + "\n");
MimeMultipart content = (MimeMultipart) email.getContent();
String textContent = content.getBodyPart(0).getContent().toString();
// System.out.println(textContent);
if (xwFlag) {
parseXwMail(textContent, email.getSubject(), email.getFrom()[0].toString().split(" <")[1].split(">")[0]);
} else {
parseXnMail(textContent, email.getSubject(), email.getFrom()[0].toString().split(" <")[1].split(">")[0]);
}
} catch (MessagingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String userEmail = "<邮箱>";
String password = "<授权码>";
List<Message> emails = null;
emails = new MyMail(userEmail, password).getMailsInFolder("校外奖品邮寄信息统计"); // 邮箱文件夹名称
logInfo.append("=====校外共" + emails.size() + "封邮件=====\n");
for (Message email : emails) {
checkMail(email, true);
}
saveXwInfo();
emails = new MyMail(userEmail, password).getMailsInFolder("校内奖品邮寄信息统计");
logInfo.append("=====校内共" + emails.size() + "封邮件=====\n");
for (Message email : emails) {
checkMail(email, false);
}
saveXnInfo();
logInfo.append("Done!\n");
printLog();
}
private static void printLog() {
System.out.println(logInfo.toString());
try {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(LOG_FILE)));
bw.write(logInfo.toString());
bw.flush();
bw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void saveXnInfo() {
try {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(XN_FILE), "GB2312"));
StringBuffer sb = new StringBuffer("获奖ID, 奖品列表, 总金额, 发信邮箱\n");
for (Map<String, String> info : xnInfos) {
sb.append(info.get("id") + ", ")
.append(info.get("prize") + ", ")
.append(info.get("total") + ", ")
.append(info.get("from_email") + "\n");
}
bw.write(sb.toString());
bw.flush();
bw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void saveXwInfo() {
try {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(XW_FILE), "GB2312"));
StringBuffer sb = new StringBuffer("获奖ID, 收件人, 手机号, 收件地址, 学信网验证码, 发信邮箱\n");
for (Map<String, String> info : xwInfos) {
sb.append(info.get("id") + ", ")
.append(info.get("to") + ", ")
.append(info.get("iphone") + ", ")
.append(info.get("address") + ", ")
.append(info.get("code") + ", ")
.append(info.get("from_email") + "\n");
}
bw.write(sb.toString());
bw.flush();
bw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.2</version>
</dependency>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment