我的 Java 方法无法使用 Java 邮件变量作为参数

My Java method is not working with JavaMail variables as arguments

我正在 Java 中创建一个程序,用于在用户收到有关密码重置的电子邮件时提醒他们。我已经给自己发送了一封电子邮件,其中包含文本 "Your password has been reset" 并创建了一个分析电子邮件的方法:

    private static boolean passwordResetFinder(String email) {
    boolean passReset = false;

    if (email.matches(".*password.*") && (email.matches(".*changed.*") || email.matches(".*reset.*"))) {
        passReset = true;
    }

我这样调用方法:

String email = new String();
...
open connection to inbox using JavaMail
...

Object content = emailReader.getContent();
email = content.toString();

if(passwordResetFinder(email)) {
   System.out.println("Password Alert");
}

这是行不通的。但是,如果我输入以下内容:

if (passwordResetFinder("Your password has been reset")

email = content.toString();    
email = "Your password has been reset";    
    if(passwordResetFinder(email))

有效。这是为什么?

也许 emailReader.getContent() 返回的对象的 toString() 没有为您提供邮件包含的文本的字符串表示形式。如果在读取时它是 Multipart 的子类,则可能是这种情况 多部分邮件或 Stream(如果消息的类型未知)。

Return the content as a Java object. The type of the returned object is of course dependent on the content itself. For example, the object returned for "text/plain" content is usually a String object. The object returned for a "multipart" content is always a Multipart subclass. For content-types that are unknown to the DataHandler system, an input stream is returned as the content

https://javamail.java.net/nonav/docs/api/javax/mail/Part.html#getContent%28%29