用Java解析MIME发送方(RFC 822)

用Java解析MIME发送方(RFC 822),java,mime,jakarta-mail,rfc822,mime4j,Java,Mime,Jakarta Mail,Rfc822,Mime4j,MIME消息发送者以以下格式显示: "John Doe" <johndoe@gmail.com> <johndoe@gmail.com> 这让我觉得我用错了这门课。尽管如此,如果我以这种方式解析,我确实可以访问getFrom()方法,该方法返回一个 使用mime4j很容易做到这一点: case T_FIELD: // field means header if(token.getName() == "from") { // get raw str

MIME消息发送者以以下格式显示:

"John Doe" <johndoe@gmail.com>
<johndoe@gmail.com>
这让我觉得我用错了这门课。尽管如此,如果我以这种方式解析,我确实可以访问
getFrom()
方法,该方法返回一个

使用mime4j很容易做到这一点:

case T_FIELD: // field means header
    if(token.getName() == "from") {
        // get raw string as above - unparsed

因此,使用mime4j或使用java、javax等实用程序,应该可以提取“a@b.com“这是地址的一部分,但我还没有在javax或mime4j中找到一个负责此操作的类。

我认为您需要javax.mail中的InternetAddress类:

最低工作示例:

import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;

public class JavaMailExample {

    public static void main(String[] args) throws AddressException {
        String fullemail = "\"John Doe\" <johndoe@gmail.com>";
        InternetAddress addr = new InternetAddress(fullemail);
        System.out.println(addr.getPersonal()); // John Doe
        System.out.println(addr.getAddress());  // johndoe@gmail.com
   }
}
import javax.mail.internet.AddressException;
导入javax.mail.internet.InternetAddress;
公共类JavaMailExample{
公共静态void main(字符串[]args)引发AddressException{
字符串fullemail=“\”John Doe\”;
InternetAddress addr=新的InternetAddress(完整电子邮件);
System.out.println(addr.getPersonal());//John Doe
System.out.println(addr.getAddress());//johndoe@gmail.com
}
}

是的,谢谢,从这里收到。为了使这是一个好的答案,虽然你应该包括如何使用它-我投票,因为这有助于我,但它接近于一个链接唯一的答案。
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;

public class JavaMailExample {

    public static void main(String[] args) throws AddressException {
        String fullemail = "\"John Doe\" <johndoe@gmail.com>";
        InternetAddress addr = new InternetAddress(fullemail);
        System.out.println(addr.getPersonal()); // John Doe
        System.out.println(addr.getAddress());  // johndoe@gmail.com
   }
}