无法按主题找到消息

hi3rlvi2  于 2021-07-12  发布在  Java
关注(0)|答案(1)|浏览(310)

我在网上找答案,但什么也找不到。我的问题是:
在我的java项目中,我使用java邮件api来读取gmail收件箱中的电子邮件。我正在使用“folder.search”方法搜索邮件。问题是它找不到具有特定字符组合的消息。
主题为的收件箱邮件示例: Welcome to a C'ompany 搜索结果
(“searchkey”)//结果状态: "Welcome to a C'ompany" //no results "Welcome to a" //message found "C'ompany" //message found "C'" //message found " C'" //message found "a C'" //no results " a C'" //no results 它看起来类似于以下语法的组合: space + letter + space + letter + apostrophe 处理得不太好。
我试着查看调试日志( session.setDebug(true) )但不存在错误消息。不返回结果的搜索日志示例: A4 SEARCH SUBJECT "Welcome to a C'ompany" 1:2 * SEARCH A4 OK SEARCH completed (Success) 我在两个不同版本的javamail上进行了测试,但是结果是一样的
com.sun.mail <artifactId>javax.mail</artifactId> 1.6.2 <groupId>javax.mail</groupId> mail <version>1.5.0-b01</version> 有人知道为什么会这样吗?这些库中是否有错误?

nbnkbykc

nbnkbykc1#

在pom.xml中添加此依赖项

  1. <!-- https://mvnrepository.com/artifact/com.sun.mail/javax.mail -->
  2. <dependency>
  3. <groupId>com.sun.mail</groupId>
  4. <artifactId>javax.mail</artifactId>
  5. <version>1.6.2</version>
  6. </dependency>

使用这个方法

  1. private boolean sendEmail(String messege, String subject, String email, String from,int otp,String password) {
  2. //Variable for gmail
  3. String host="smtp.gmail.com";
  4. //get the system properties
  5. Properties properties = System.getProperties();
  6. System.out.println("PROPERTIES "+properties);
  7. //setting important information to properties object
  8. //host set
  9. properties.put("mail.smtp.host", host);
  10. properties.put("mail.smtp.port","465");
  11. properties.put("mail.smtp.ssl.enable","true");
  12. properties.put("mail.smtp.auth","true");
  13. //Step 1: to get the session object..
  14. Session session=Session.getInstance(properties, new Authenticator() {
  15. @Override
  16. protected PasswordAuthentication getPasswordAuthentication() {
  17. return new PasswordAuthentication(from, password);
  18. }
  19. });
  20. session.setDebug(true);
  21. //Step 2 : compose the message [text,multi media]
  22. MimeMessage m = new MimeMessage(session);
  23. try {
  24. //from email
  25. m.setFrom(from);
  26. //adding recipient to message
  27. m.addRecipient(Message.RecipientType.TO, new InternetAddress(email));
  28. //adding subject to message
  29. m.setSubject(subject);
  30. //adding text to message
  31. m.setText(messege);
  32. //send
  33. //Step 3 : send the message using Transport class
  34. Transport.send(m);
  35. System.out.println("Sent success...................");
  36. }catch (Exception e) {
  37. e.printStackTrace();
  38. return false;
  39. }
  40. return true;
  41. }

您还阅读了javamail api文档

展开查看全部

相关问题