简体   繁体   English

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

[英]How to read a bounce back email details with Java mail API?

I am using Java mail API to read the bounce back email from Amazon SES on my Gmail id.我正在使用 Java 邮件 API 在我的 Gmail id 上读取来自 Amazon SES 的退回电子邮件。

This is how I receive an bounce email from Amazon SES.这就是我从 Amazon SES 收到退回电子邮件的方式。

<email content start>

An error occurred while trying to deliver the mail to the following recipients:
bounce@simulator.amazonses.com
Action: failed
Final-Recipient: rfc822; bounce@simulator.amazonses.com
Diagnostic-Code: smtp; 550 5.1.1 user unknown
Status: 5.1.1



---------- Forwarded message ----------
From: fullstack.rahultokase@gmail.com
To: bounce@simulator.amazonses.com
Cc: 
Bcc: 
Date: Sun, 17 Dec 2017 15:27:30 +0000
Subject: bounce@simulator.amazonses.com
bounce@simulator.amazonses.com

<email content end>

My question is using Java email API.我的问题是使用 Java 电子邮件 API。 I am able to read the content up to:我能够阅读以下内容:

An error occurred while trying to deliver the mail to the following recipients:
bounce@simulator.amazonses.com

But I am not able to read the following content with the help of Java email api但是我无法在Java email api的帮助下阅读以下内容

Action: failed
Final-Recipient: rfc822; bounce@simulator.amazonses.com
Diagnostic-Code: smtp; 550 5.1.1 user unknown
Status: 5.1.1

How can I read the above content in the email?如何阅读邮件中的上述内容?

The diagnostic code information is a part of message content and can be read using the following code.诊断代码信息是消息内容的一部分,可以使用以下代码读取。

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());


    }

The information that you are looking for (Action, Final-Recipient, Diagnostic-Code, Status) are set in the headers of the message, you can get it with您要查找的信息(Action、Final-Recipient、Diagnostic-Code、Status)设置在消息的标题中,您可以通过

Considering that msg is the message object:考虑到 msg 是消息对象:

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

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

the second value (in the example diagnostics[1] ) will contain the error code that indicates if it is a hard bounce 550 (for instance the email address does not exist), or a soft bounce 450 (for example, the inbox bin is full)第二个值(在示例diagnostics[1] )将包含错误代码,指示它是硬退回 550(例如电子邮件地址不存在)还是软退回 450(例如,收件箱是满的)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM