无法按主题找到消息

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

我在网上找答案,但什么也找不到。我的问题是:
在我的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中添加此依赖项

<!-- https://mvnrepository.com/artifact/com.sun.mail/javax.mail -->
<dependency>
    <groupId>com.sun.mail</groupId>
    <artifactId>javax.mail</artifactId>
    <version>1.6.2</version>
</dependency>

使用这个方法

private boolean sendEmail(String messege, String subject, String email, String from,int otp,String password) {
    //Variable for gmail
    String host="smtp.gmail.com";

    //get the system properties
    Properties properties = System.getProperties();
    System.out.println("PROPERTIES "+properties);

    //setting important information to properties object

    //host set
    properties.put("mail.smtp.host", host);
    properties.put("mail.smtp.port","465");
    properties.put("mail.smtp.ssl.enable","true");
    properties.put("mail.smtp.auth","true");

    //Step 1: to get the session object..
    Session session=Session.getInstance(properties, new Authenticator() {
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {              
            return new PasswordAuthentication(from, password);
        }

    });

    session.setDebug(true);

    //Step 2 : compose the message [text,multi media]
    MimeMessage m = new MimeMessage(session);

    try {

    //from email
    m.setFrom(from);

    //adding recipient to message
    m.addRecipient(Message.RecipientType.TO, new InternetAddress(email));

    //adding subject to message
    m.setSubject(subject);

    //adding text to message
    m.setText(messege);

    //send 

    //Step 3 : send the message using Transport class
    Transport.send(m);

    System.out.println("Sent success...................");

    }catch (Exception e) {

        e.printStackTrace();
        return false;
    }

    return true;
}

您还阅读了javamail api文档

相关问题