如何使用Java mail API阅读退回电子邮件详细信息?

问题描述 投票:2回答:2

我正在使用Java mail API来读取我的Gmail ID上来自Amazon SES的退回电子邮件。

这是我从Amazon SES收到退回电子邮件的方式。

<email content start>

An error occurred while trying to deliver the mail to the following recipients:
[email protected]
Action: failed
Final-Recipient: rfc822; [email protected]
Diagnostic-Code: smtp; 550 5.1.1 user unknown
Status: 5.1.1



---------- Forwarded message ----------
From: [email protected]
To: [email protected]
Cc: 
Bcc: 
Date: Sun, 17 Dec 2017 15:27:30 +0000
Subject: [email protected]
[email protected]

<email content end>

我的问题是使用Java电子邮件API。我能够阅读以下内容:

An error occurred while trying to deliver the mail to the following recipients:
[email protected]

但是我无法在Java email api的帮助下阅读以下内容

Action: failed
Final-Recipient: rfc822; [email protected]
Diagnostic-Code: smtp; 550 5.1.1 user unknown
Status: 5.1.1

如何阅读电子邮件中的上述内容?

java javamail
2个回答
1
投票

诊断代码信息是消息内容的一部分,可以使用以下代码读取。

MimeMessage payload = (MimeMessage) message.getPayload();
    Multipart mp = (Multipart) payload.getContent();
    for (int i = 0; i < mp.getCount(); i++) {
                        BodyPart bodyPart = mp.getBodyPart(i);
                        StringWriter writer = new StringWriter();
                        IOUtils.copy(bodyPart.getInputStream(), writer);
                        System.out.println("Content inputstream: " +  writer.toString());


    }

0
投票

您正在查找的信息(操作,最终收件人,诊断代码,状态)在邮件的标题中设置,您可以使用

考虑到msg是消息对象:

  ... 
  final String[] diagnostics = msg.getHeader("Diagnostic-Code"); 

  for (String dx_code : diagnostics) {
     System.out.print(dx_code);
  }
  ...

第二个值(在示例diagnostics[1]中)将包含错误代码,指示它是否是硬反弹550(例如电子邮件地址不存在),或软反弹450(例如,收件箱箱已满)

© www.soinside.com 2019 - 2024. All rights reserved.