JavaMail search mail, java development practice classic video

mb61ab431c11928 2022-01-26 13:58:36 阅读数:656

javamail search mail java development
  1. ????new?FromStringTerm(" zhaopin "),???

  2. ????new?SubjectTerm(" Latest position information "));??

  3. Message[]?messages?=?folder.search(orTerm);??

3、 The search sender does not contain “ zhaopin “ All mail

[java]? [view plain]( ) [copy]( )

  1. SearchTerm?notTerm?=?new?NotTerm(new?FromStringTerm(" zhaopin "));??

  2. Message[]?messages?=?folder.search(notTerm);??

4、 Search all emails received from Monday to today

[java]? [view plain]( ) [copy]( )

  1. Calendar?calendar?=?Calendar.getInstance();??

  2. calendar.set(Calendar.DAY_OF_WEEK,?calendar.get(Calendar.DAY_OF_WEEK?-?(Calendar.DAY_OF_WEEK?-?1))?-?1);??

  3. Date?mondayDate?=?calendar.getTime();??

  4. SearchTerm?comparisonTermGe?=?new?SentDateTerm(ComparisonTerm.GE,?mondayDate);??

  5. SearchTerm?comparisonTermLe?=?new?SentDateTerm(ComparisonTerm.LE,?new?Date());??

  6. SearchTerm?comparisonAndTerm?=?new?AndTerm(comparisonTermGe,?comparisonTermLe);??

  7. Message[]?messages?=?folder.search(comparisonAndTerm);??

5、 Search greater than or equal to 100KB All of your messages

[java]? [view plain]( ) [copy]( )

  1. int?mailSize?=?1024?*?100;??

  2. SearchTerm?intComparisonTerm?=?new?SizeTerm(IntegerComparisonTerm.GE,?mailSize);??

  3. Message[]?messages?=?folder.search(intComparisonTerm);??

ComparisonTerm Class is often used in date and number comparisons , It uses six constants EQ(=)、GE(>=)、GT(>)、LE(<=)、LT(<)、NE(!=) To represent six different comparison operations .

Complete code :

[java]? [view plain]( ) [copy]( )

  1. package?org.yangxin.study.jm;??

  2. import?java.io.BufferedReader;??

  3. import?java.io.InputStreamReader;??

  4. import?java.util.Calendar;??

  5. import?java.util.Date;??

  6. import?java.util.Properties;??

  7. import?javax.mail.Flags.Flag;??

  8. import?javax.mail.Folder;??

  9. import?javax.mail.Message;??

  10. import?javax.mail.Session;??

  11. import?javax.mail.Store;??

  12. import?javax.mail.URLName;??

  13. import?javax.mail.internet.MimeMessage;??

  14. import?javax.mail.internet.MimeUtility;??

  15. import?javax.mail.search.AndTerm;??

  16. import?javax.mail.search.BodyTerm;??

  17. import?javax.mail.search.ComparisonTerm;??

  18. import?javax.mail.search.FromStringTerm;??

  19. import?javax.mail.search.IntegerComparisonTerm;??

  20. import?javax.mail.search.NotTerm;??

  21. import?javax.mail.search.OrTerm;??

  22. import?javax.mail.search.SearchTerm;??

  23. import?javax.mail.search.SentDateTerm;??

  24. import?javax.mail.search.SizeTerm;??

  25. import?javax.mail.search.SubjectTerm;??

  26. /**?

  27. ?*? Search mail ?

  28. ?*/??

  29. public?class?SearchMailTest?{??

  30. ????public?static?void?main(String[]?args)?throws?Exception?{??

  31. ????????Properties?props?=?new?Properties();??

  32. ????????props.setProperty("mail.pop3.auth",?"true");??

  33. ????????Session?session?=?Session.getInstance(props);??

  34. ????????URLName?url?=?new?URLName("pop3",?"pop3.163.com",?110,?null,?"[email protected]",?"yX546900873");??

  35. ????????Store?store?=?session.getStore(url);??

  36. ????????store.connect();??

  37. ????????//? Get inbox ??

  38. ????????Folder?folder?=?store.getFolder("INBOX");??

  39. ????????//? Open inbox in read-write mode ??

  40. ????????folder.open(Folder.READ_WRITE);??

  41. ????????Message[]?messages?=?search(folder);??

  42. ????????System.out.println(" Inboxes are shared :"?+?folder.getMessageCount()?+?" Messages , Search for "?+?messages.length?+?" Eligible messages !");??

  43. ????????//? Resolve messages searched ??

  44. ????????POP3ReceiveMailTest.parseMessage(messages);???

  45. ????????//? Search all messages according to the criteria entered by the user , And prompt the user whether to delete ??

  46. ????????//searchDemo(folder);??

  47. ????????folder.close(true);??

  48. ????????store.close();??

  49. ????}??

  50. ????public?static?Message[]?search(Folder?folder)?throws?Exception?{??

  51. ????????//? Search for messages with gourmet topics ??

  52. ????????String?subject?=?"java train ";??

  53. ????????SearchTerm?subjectTerm?=?new?SubjectTerm(subject);??

  54. ????????//? Search sender includes Alipay mail. ??

  55. ????????SearchTerm?fromTerm?=?new?FromStringTerm(" Alipay ");??

  56. ????????//? Search message content contains " Recruitment " The mail ??

  57. ????????SearchTerm?bodyTerm?=?new?BodyTerm(" Recruitment ");??

  58. ????????//? The search sender does not contain “ zhaopin ” The mail ??

  59. ????????SearchTerm?notTerm?=?new?NotTerm(new?FromStringTerm(" zhaopin "));??

  60. ????????//? Search from “ zhaopin ”, And the content includes “Java The engineer “ The mail ??

  61. ????????SearchTerm?andTerm?=?new?AndTerm(??

  62. ????????????????new?FromStringTerm(" zhaopin "),??

  63. ????????????????new?BodyTerm("java The engineer "));??

  64. ????????//? Search from ” zhaopin “ Or subject contains ” Latest position information “ The mail ??

  65. ????????SearchTerm?orTerm?=?new?OrTerm(??

  66. ????????????????new?FromStringTerm(" zhaopin "),???

  67. ????????????????new?SubjectTerm(" Latest position information "));??

  68. ????????//? Search all emails received from Monday to today ??

  69. ????????Calendar?calendar?=?Calendar.getInstance();??

  70. ????????calendar.set(Calendar.DAY_OF_WEEK,?calendar.get(Calendar.DAY_OF_WEEK?-?(Calendar.DAY_OF_WEEK?-?1))?-?1);??

  71. ????????Date?mondayDate?=?calendar.getTime();??

  72. ????????SearchTerm?comparisonTermGe?=?new?SentDateTerm(ComparisonTerm.GE,?mondayDate);??

  73. ????????SearchTerm?comparisonTermLe?=?new?SentDateTerm(ComparisonTerm.LE,?new?Date());??

  74. ????????SearchTerm?comparisonAndTerm?=?new?AndTerm(comparisonTermGe,?comparisonTermLe);??

  75. ????????//? Search greater than or equal 100KB All of your messages ??

  76. ????????int?mailSize?=?1024?*?100;??

  77. ????????SearchTerm?intComparisonTerm?=?new?SizeTerm(IntegerComparisonTerm.GE,?mailSize);??

  78. ????????return?folder.search(intComparisonTerm);??

  79. ????}??

  80. ?}??

example : Based on the recipients entered by the user (email Address or name ) And subject as search criteria , And prompt the user whether to delete the searched mail ?

[java]? [view plain]( ) [copy]( )

  1. /**?

  2. ?????*? According to the recipient address entered by the user ( Include email Address and name ) And subject as search criteria , And prompt the user whether to delete the searched mail ?

  3. ?????*[email protected]?from? The recipient ?

  4. ?????*[email protected]?subject? The theme ?

  5. ?????*/??

  6. ????public?static?void?searchDemo(Folder?folder)?throws?Exception?{??

  7. ????????String?notifyMsg?=?" In the inbox "?+?folder.getMessageCount()?+?" Messages . Please select action :\n";??

  8. ????????notifyMsg?+=?"1、 Enter the recipient \n"?+?"2、 Enter a topic \n"?+?"3、 Begin your search \n"?+?"4、 sign out ";??

  9. ????????System.out.println(notifyMsg);??

  10. ????????String?from?=?null;??

  11. ????????String?subject?=?null;??

  12. ????????BufferedReader?reader?=?new?BufferedReader(new?InputStreamReader(System.in));??

  13. ????????String?oper?=?reader.readLine().trim();??

  14. ????????while(!(from?!=?null?&&?subject?!=?null))?{??

  15. ????????????if?("4".equals(oper))?{??

  16. ????????????????System.exit(0);??

  17. ????????????}?else?{??

summary

ad locum , Due to the interview MySQL More questions , Therefore, it is here to MySQL As an example, summarize and share . But you often have to learn more than that , There is also the use of some mainstream frameworks ,Spring Source code learning ,Mybatis The learning of source code and so on need to be mastered , I've also sorted out these knowledge points

 The real question of the interview

Spring Source notes

This article has been  CODING Open source project :【 A big factory Java Analysis of interview questions + Core summary learning notes + The latest explanation video + Actual project source code 】 Included

copyright:author[mb61ab431c11928],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/01/202201261358198479.html