1. import lombok.extern.slf4j.Slf4j;
  2. import org.springframework.beans.factory.annotation.Value;
  3.  
  4. import javax.mail.*;
  5. import javax.mail.internet.InternetAddress;
  6. import javax.mail.internet.MimeMessage;
  7. import javax.mail.internet.MimeMultipart;
  8. import javax.mail.internet.MimeUtility;
  9. import java.io.*;
  10. import java.text.SimpleDateFormat;
  11. import java.util.Date;
  12. import java.util.Properties;
  13.  
  14. /**
  15. * 使用POP3协议解析邮件帮助类
  16. *
  17. * @author bzs on 2018/6/28.
  18. */
  19. @Slf4j
  20. public class ParsingEmailUtil {
  21.  
  22. public static void main(String[] args) throws Exception{
  23. resceive("**********@163.com", "*****");
  24. }
  25.  
  26. /**
  27. * 获取邮箱信息
  28. *
  29. * @param emailAdress 需要解析的邮箱地址
  30. * @param password 邮箱的授权密码
  31. * @throws Exception
  32. */
  33. public static void resceive(String emailAdress, String password) throws Exception {
  34.  
  35. String port = "110"; // 端口号
  36. String servicePath = "pop.163.com"; // 服务器地址
  37.  
  38. // 准备连接服务器的会话信息
  39. Properties props = new Properties();
  40. props.setProperty("mail.store.protocol", "pop3"); // 使用pop3协议
  41. props.setProperty("mail.pop3.port", port); // 端口
  42. props.setProperty("mail.pop3.host", servicePath); // pop3服务器
  43.  
  44. // 创建Session实例对象
  45. Session session = Session.getInstance(props);
  46. Store store = session.getStore("pop3");
  47. store.connect(emailAdress, password); //163邮箱程序登录属于第三方登录所以这里的密码是163给的授权密码而并非普通的登录密码
  48.  
  49. // 获得收件箱
  50. Folder folder = store.getFolder("INBOX");
  51. /* Folder.READ_ONLY:只读权限
  52. * Folder.READ_WRITE:可读可写(可以修改邮件的状态)
  53. */
  54. folder.open(Folder.READ_WRITE); //打开收件箱
  55.  
  56. // // 由于POP3协议无法获知邮件的状态,所以getUnreadMessageCount得到的是收件箱的邮件总数
  57. // System.out.println("未读邮件数: " + folder.getUnreadMessageCount());
  58. //
  59. // // 由于POP3协议无法获知邮件的状态,所以下面得到的结果始终都是为0
  60. // System.out.println("删除邮件数: " + folder.getDeletedMessageCount());
  61. // System.out.println("新邮件: " + folder.getNewMessageCount());
  62.  
  63. // 获得收件箱中的邮件总数
  64. log.warn("邮件总数: {}", folder.getMessageCount());
  65.  
  66. // 得到收件箱中的所有邮件,并解析
  67. Message[] messages = folder.getMessages();
  68. //解析邮件
  69. parseMessage(messages);
  70.  
  71. //得到收件箱中的所有邮件并且删除邮件
  72. // deleteMessage(messages);
  73.  
  74. //释放资源
  75. folder.close(true);
  76. store.close();
  77. }
  78.  
  79. /**
  80. * 解析邮件
  81. *
  82. * @param messages 要解析的邮件列表
  83. */
  84. public static void parseMessage(Message... messages) throws MessagingException, IOException {
  85. if (messages == null || messages.length < 1)
  86. throw new MessagingException("未找到要解析的邮件!");
  87.  
  88. // 解析所有邮件
  89. for (int i = 0, count = messages.length; i < count; i++) {
  90. MimeMessage msg = (MimeMessage) messages[i];
  91. log.info("------------------解析第" + msg.getMessageNumber() + "封邮件-------------------- ");
  92. log.warn("主题: {}" , getSubject(msg));
  93. log.warn("发件人: {}" , getFrom(msg));
  94. log.warn("收件人:{}" , getReceiveAddress(msg, null));
  95. log.warn("发送时间:{}" , getSentDate(msg, null));
  96. log.warn("是否已读:{}" , isSeen(msg));
  97. log.warn("邮件优先级:{}" , getPriority(msg));
  98. log.warn("是否需要回执:{}" , isReplySign(msg));
  99. log.warn("邮件大小:{}" , msg.getSize() * 1024 + "kb");
  100. boolean isContainerAttachment = isContainAttachment(msg);
  101. log.warn("是否包含附件:{}" ,isContainerAttachment);
  102. if (isContainerAttachment) {
  103. saveAttachment(msg, "d:\\log\\" + msg.getSubject() + "_" + i + "_"); //保存附件
  104. }
  105. StringBuffer content = new StringBuffer(30);
  106. //解析邮件正文
  107. getMailTextContent(msg, content);
  108. log.warn("邮件正文:{}" , content);
  109. log.info("------------------第" + msg.getMessageNumber() + "封邮件解析结束-------------------- ");
  110. System.out.println();
  111. }
  112. }
  113.  
  114. /**
  115. * 删除邮件
  116. *
  117. * @param messages 要删除邮件列表
  118. */
  119. public static void deleteMessage(Message... messages) throws MessagingException, IOException {
  120. if (messages == null || messages.length < 1)
  121. throw new MessagingException("未找到要解析的邮件!");
  122.  
  123. // 解析所有邮件
  124. for (int i = 0, count = messages.length; i < count; i++) {
  125.  
  126. /**
  127. * 邮件删除
  128. */
  129. Message message = messages[i];
  130. String subject = message.getSubject();
  131. // set the DELETE flag to true
  132. message.setFlag(Flags.Flag.DELETED, true);
  133. System.out.println("Marked DELETE for message: " + subject);
  134.  
  135. }
  136. }
  137.  
  138. /**
  139. * 获得邮件主题
  140. *
  141. * @param msg 邮件内容
  142. * @return 解码后的邮件主题
  143. */
  144. public static String getSubject(MimeMessage msg) throws UnsupportedEncodingException, MessagingException {
  145. return MimeUtility.decodeText(msg.getSubject());
  146. }
  147.  
  148. /**
  149. * 获得邮件发件人
  150. *
  151. * @param msg 邮件内容
  152. * @return 姓名 <Email地址>
  153. * @throws MessagingException
  154. * @throws UnsupportedEncodingException
  155. */
  156. public static String getFrom(MimeMessage msg) throws MessagingException, UnsupportedEncodingException {
  157. String from = "";
  158. Address[] froms = msg.getFrom();
  159. if (froms.length < 1)
  160. throw new MessagingException("没有发件人!");
  161.  
  162. InternetAddress address = (InternetAddress) froms[0];
  163. String person = address.getPersonal();
  164. if (person != null) {
  165. person = MimeUtility.decodeText(person) + " ";
  166. } else {
  167. person = "";
  168. }
  169. from = person + "<" + address.getAddress() + ">";
  170.  
  171. return from;
  172. }
  173.  
  174. /**
  175. * 根据收件人类型,获取邮件收件人、抄送和密送地址。如果收件人类型为空,则获得所有的收件人
  176. * <p>Message.RecipientType.TO 收件人</p>
  177. * <p>Message.RecipientType.CC 抄送</p>
  178. * <p>Message.RecipientType.BCC 密送</p>
  179. *
  180. * @param msg 邮件内容
  181. * @param type 收件人类型
  182. * @return 收件人1 <邮件地址1>, 收件人2 <邮件地址2>, ...
  183. * @throws MessagingException
  184. */
  185. public static String getReceiveAddress(MimeMessage msg, Message.RecipientType type) throws MessagingException {
  186. StringBuffer receiveAddress = new StringBuffer();
  187. Address[] addresss = null;
  188. if (type == null) {
  189. addresss = msg.getAllRecipients();
  190. } else {
  191. addresss = msg.getRecipients(type);
  192. }
  193.  
  194. if (addresss == null || addresss.length < 1)
  195. throw new MessagingException("没有收件人!");
  196. for (Address address : addresss) {
  197. InternetAddress internetAddress = (InternetAddress) address;
  198. receiveAddress.append(internetAddress.toUnicodeString()).append(",");
  199. }
  200.  
  201. receiveAddress.deleteCharAt(receiveAddress.length() - 1); //删除最后一个逗号
  202.  
  203. return receiveAddress.toString();
  204. }
  205.  
  206. /**
  207. * 获得邮件发送时间
  208. *
  209. * @param msg 邮件内容
  210. * @return yyyy年mm月dd日 星期X HH:mm
  211. * @throws MessagingException
  212. */
  213. public static String getSentDate(MimeMessage msg, String pattern) throws MessagingException {
  214. Date receivedDate = msg.getSentDate();
  215. if (receivedDate == null)
  216. return "";
  217.  
  218. if (pattern == null || "".equals(pattern))
  219. pattern = "yyyy年MM月dd日 E HH:mm ";
  220.  
  221. return new SimpleDateFormat(pattern).format(receivedDate);
  222. }
  223.  
  224. /**
  225. * 判断邮件中是否包含附件
  226. *
  227. * @param part 邮件内容
  228. * @return 邮件中存在附件返回true,不存在返回false
  229. * @throws MessagingException
  230. * @throws IOException
  231. */
  232. public static boolean isContainAttachment(Part part) throws MessagingException, IOException {
  233. boolean flag = false;
  234. if (part.isMimeType("multipart/*")) {
  235. MimeMultipart multipart = (MimeMultipart) part.getContent();
  236. int partCount = multipart.getCount();
  237. for (int i = 0; i < partCount; i++) {
  238. BodyPart bodyPart = multipart.getBodyPart(i);
  239. String disp = bodyPart.getDisposition();
  240. if (disp != null && (disp.equalsIgnoreCase(Part.ATTACHMENT) || disp.equalsIgnoreCase(Part.INLINE))) {
  241. flag = true;
  242. } else if (bodyPart.isMimeType("multipart/*")) {
  243. flag = isContainAttachment(bodyPart);
  244. } else {
  245. String contentType = bodyPart.getContentType();
  246. if (contentType.indexOf("application") != -1) {
  247. flag = true;
  248. }
  249.  
  250. if (contentType.indexOf("name") != -1) {
  251. flag = true;
  252. }
  253. }
  254.  
  255. if (flag) break;
  256. }
  257. } else if (part.isMimeType("message/rfc822")) {
  258. flag = isContainAttachment((Part) part.getContent());
  259. }
  260. return flag;
  261. }
  262.  
  263. /**
  264. * 判断邮件是否已读
  265. *
  266. * @param msg 邮件内容
  267. * @return 如果邮件已读返回true, 否则返回false
  268. * @throws MessagingException
  269. */
  270. public static boolean isSeen(MimeMessage msg) throws MessagingException {
  271. return msg.getFlags().contains(Flags.Flag.SEEN);
  272. }
  273.  
  274. /**
  275. * 判断邮件是否需要阅读回执
  276. *
  277. * @param msg 邮件内容
  278. * @return 需要回执返回true, 否则返回false
  279. * @throws MessagingException
  280. */
  281. public static boolean isReplySign(MimeMessage msg) throws MessagingException {
  282. boolean replySign = false;
  283. String[] headers = msg.getHeader("Disposition-Notification-To");
  284. if (headers != null)
  285. replySign = true;
  286. return replySign;
  287. }
  288.  
  289. /**
  290. * 获得邮件的优先级
  291. *
  292. * @param msg 邮件内容
  293. * @return 1(High):紧急 3:普通(Normal) 5:低(Low)
  294. * @throws MessagingException
  295. */
  296. public static String getPriority(MimeMessage msg) throws MessagingException {
  297. String priority = "普通";
  298. String[] headers = msg.getHeader("X-Priority");
  299. if (headers != null) {
  300. String headerPriority = headers[0];
  301. if (headerPriority.indexOf("1") != -1 || headerPriority.indexOf("High") != -1)
  302. priority = "紧急";
  303. else if (headerPriority.indexOf("5") != -1 || headerPriority.indexOf("Low") != -1)
  304. priority = "低";
  305. else
  306. priority = "普通";
  307. }
  308. return priority;
  309. }
  310.  
  311. /**
  312. * 获得邮件文本内容
  313. *
  314. * @param part 邮件体
  315. * @param content 存储邮件文本内容的字符串
  316. * @throws MessagingException
  317. * @throws IOException
  318. */
  319. public static void getMailTextContent(Part part, StringBuffer content) throws MessagingException, IOException {
  320. //如果是文本类型的附件,通过getContent方法可以取到文本内容,但这不是我们需要的结果,所以在这里要做判断
  321. boolean isContainTextAttach = part.getContentType().indexOf("name") > 0;
  322. if (part.isMimeType("text/*") && !isContainTextAttach) {
  323. content.append(part.getContent().toString());
  324. } else if (part.isMimeType("message/rfc822")) {
  325. getMailTextContent((Part) part.getContent(), content);
  326. } else if (part.isMimeType("multipart/*")) {
  327. Multipart multipart = (Multipart) part.getContent();
  328. int partCount = multipart.getCount();
  329. for (int i = 0; i < partCount; i++) {
  330. BodyPart bodyPart = multipart.getBodyPart(i);
  331. getMailTextContent(bodyPart, content);
  332. }
  333. }
  334. }
  335.  
  336. /**
  337. * 保存附件
  338. *
  339. * @param part 邮件中多个组合体中的其中一个组合体
  340. * @param destDir 附件保存目录
  341. * @throws UnsupportedEncodingException
  342. * @throws MessagingException
  343. * @throws FileNotFoundException
  344. * @throws IOException
  345. */
  346. public static void saveAttachment(Part part, String destDir) throws MessagingException, IOException {
  347. if (part.isMimeType("multipart/*")) {
  348. Multipart multipart = (Multipart) part.getContent(); //复杂体邮件
  349. //复杂体邮件包含多个邮件体
  350. int partCount = multipart.getCount();
  351. for (int i = 0; i < partCount; i++) {
  352. //获得复杂体邮件中其中一个邮件体
  353. BodyPart bodyPart = multipart.getBodyPart(i);
  354. //某一个邮件体也有可能是由多个邮件体组成的复杂体
  355. String disp = bodyPart.getDisposition();
  356. if (disp != null && (disp.equalsIgnoreCase(Part.ATTACHMENT) || disp.equalsIgnoreCase(Part.INLINE))) {
  357. InputStream is = bodyPart.getInputStream();
  358. saveFile(is, destDir, decodeText(bodyPart.getFileName()));
  359. } else if (bodyPart.isMimeType("multipart/*")) {
  360. saveAttachment(bodyPart, destDir);
  361. } else {
  362. String contentType = bodyPart.getContentType();
  363. if (contentType.indexOf("name") != -1 || contentType.indexOf("application") != -1) {
  364. saveFile(bodyPart.getInputStream(), destDir, decodeText(bodyPart.getFileName()));
  365. }
  366. }
  367. }
  368. } else if (part.isMimeType("message/rfc822")) {
  369. saveAttachment((Part) part.getContent(), destDir);
  370. }
  371. }
  372.  
  373. /**
  374. * 读取输入流中的数据保存至指定目录
  375. *
  376. * @param is 输入流
  377. * @param fileName 文件名
  378. * @param destDir 文件存储目录
  379. * @throws FileNotFoundException
  380. * @throws IOException
  381. */
  382. private static void saveFile(InputStream is, String destDir, String fileName) throws FileNotFoundException, IOException {
  383. BufferedInputStream bis = new BufferedInputStream(is);
  384. BufferedOutputStream bos = new BufferedOutputStream(
  385. new FileOutputStream(new File(destDir + fileName)));
  386. int len = -1;
  387. while ((len = bis.read()) != -1) {
  388. bos.write(len);
  389. bos.flush();
  390. }
  391. bos.close();
  392. bis.close();
  393. }
  394.  
  395. /**
  396. * 文本解码
  397. *
  398. * @param encodeText 解码MimeUtility.encodeText(String text)方法编码后的文本
  399. * @return 解码后的文本
  400. * @throws UnsupportedEncodingException
  401. */
  402. public static String decodeText(String encodeText) throws UnsupportedEncodingException {
  403. if (encodeText == null || "".equals(encodeText)) {
  404. return "";
  405. } else {
  406. return MimeUtility.decodeText(encodeText);
  407. }
  408. }
  409.  
  410. }

  通过此方法可以解析出邮箱中邮件的详细信息。

java解析邮箱中的邮件信息的更多相关文章

  1. 日期格式化(类似QQ邮箱中的邮件列表显示日期)

    日期格式化(类似QQ邮箱中的邮件列表显示日期) public static string FormatDateDisplay(DateTime _datetime) { var ts = DateTi ...

  2. java解析xml实例——获取天气信息

    获取xml并解析其中的数据: package getweather.xml; import java.io.IOException; import java.util.HashMap; import ...

  3. Java 在Word中创建邮件合并模板并合并文本和图片

    Word里面的邮件合并功能是一种可以快速批量操作同类型数据的方式,常见的如数据填充.打印等.其中必不可少的步骤包括用于填充的模板文档.填充的数据源以及实现邮件合并的功能.下面,通过Java程序展示如何 ...

  4. java 解析webservice 中的soapheader

    //从MessageContet中获取头域中的值 public HeaderBean getBeanFromRequest(org.apache.axis2.context.MessageContex ...

  5. Linux中Postfix邮件认证配置(五)

    Postfix+Dovecot+Sasl工作原理 1.A用户使用MUA客户端借助smtp协议登陆smtpd服务器,需要先进行用户和密码认证,而SMTPD服务器端支持sasl认证,例如有一个sasl客户 ...

  6. Android将日志信息自动发送到指定的邮箱中 邮件的内容以附件形式发送

    今日整合了网上一些大神的例子(具体看了那些大神的?这个真不好意思我忘记了.下次再整合一定给大家补上,这次也只有默默的给那几个大神说声抱歉了.)做了一个“记录android项目中的日志信息,并将日志信息 ...

  7. Java解析OFFICE(word,excel,powerpoint)以及PDF的实现方案及开发中的点滴分享

    Java解析OFFICE(word,excel,powerpoint)以及PDF的实现方案及开发中的点滴分享 在此,先分享下写此文前的经历与感受,我所有的感觉浓缩到一个字,那就是:"坑&qu ...

  8. EBS中使用JAVA方式发送HTML格式邮件

    转自huan.gu专栏:http://blog.csdn.net/gh320/article/details/17174769 EBS中使用JAVA方式发送HTML格式邮件 一.开发工具:JDevel ...

  9. JAVA方法调用中的解析与分派

    JAVA方法调用中的解析与分派 本文算是<深入理解JVM>的读书笔记,参考书中的相关代码示例,从字节码指令角度看看解析与分派的区别. 方法调用,其实就是要回答一个问题:JVM在执行一个方法 ...

随机推荐

  1. 激光数据匹配(MATLAB Robotics System Toolbox)

    正态分布变换算法是一个配准算法,它应用于三维点的统计模型,使用标准最优化技术来确定两个点云间的最优的匹配,因为其在配准过程中不利用对应点的特征计算和匹配,所以时间比其他方法快.算法细节可以参考:NDT ...

  2. Java邻接表表示加权有向图,附dijkstra最短路径算法

    从A到B,有多条路线,要找出最短路线,应该用哪种数据结构来存储这些数据. 这不是显然的考查图论的相关知识了么, 1.图的两种表示方式: 邻接矩阵:二维数组搞定. 邻接表:Map<Vertext, ...

  3. spark的外排:AppendOnlyMap与ExternalAppendOnlyMap

    相信很多人和我一样, 在控制台中总是可以看到会打印出如下的语句: INFO ExternalAppendOnlyMap: Thread 94 spilling in-memory map of 63. ...

  4. Docker安装和常用命令

    Docker安装 Docker的安装可以参考 https://docs.docker.com/ 下面的 Get Docker / Docker CE / Linux, 需要关注的主要是CentOS和U ...

  5. Maven + SpringMVC项目集成Swagger

    Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 Web 服务.总体目标是使客户端和文件系统作为服务器以同样的速度来更新.文件的方法,参数和模型紧密集成到服 ...

  6. linux(ubuntu) 查看系统设备信息 命令

    时间:2012-08-02 00:12   ubuntu查看版本命令 方法一: 在终端中执行下列指令: cat /etc/issue 方法二: 使用 lsb_release 命令也可以查看 Ubunt ...

  7. ios实例开发精品文章推荐(7.22)

    UIView 基本方法 UIView的一些基本方法理解:loadView.viewDidLoad.viewDidUnload.viewWillAppear,viewWillDisappear init ...

  8. nfs的优化

    总结和测试了一下自己的经验: NFS中的rsize.wsize rsize.wsize对于NFS的效能有很大的影响.wsize和rsize设定了SERVER和CLIENT之间往来数据块的大小,这两个参 ...

  9. Linux下DIR,dirent,stat等结构体详解(转)

    最近在看Linux下文件操作相关章节,遇到了这么几个结构体,被搞的晕乎乎的,今日有空,仔细研究了一下,受益匪浅. 首先说说DIR这一结构体,以下为DIR结构体的定义: struct __dirstre ...

  10. 【ASP.NET】ASP.NET中权限验证使用OnAuthorization实现

    在项目开发中,通常我们都会涉及到用户登录才能访问的网页,比如购物网站,我们浏览商品,添加购物车(以前开发的时候在这里就需要登录用户,但是现在有了缓存的实现,这里可以将商品加入缓存,等到结账的时候再登录 ...