본문 바로가기
aws

Java Mail API로 Amazon SES 반송 이메일 세부 정보 읽기

by moonsiri 2021. 4. 7.
728x90
반응형

메일을 발송했는데 발송 실패가 되어 "Delivery Status Notification (Failure)"라는 제목으로 발송 실패 메일이 왔습니다.

Java Mail API로 읽어온 메일 내용은 다음과 같습니다.

An error occurred while trying to deliver the mail to the following recipients:
moonsiri@gmail.com

 

정확히 무슨 이유로 메일 발송이 실패했는지 찾다가 직접 메일함에 들어가 확인해보니 내용이 더 있었습니다.

An error occurred while trying to deliver the mail to the following recipients:
moonsiri@gmail.com

Technical report:

Reporting-MTA: dns; a27-61.smtp-out.us-west-2.amazonses.com

Action: failed
Final-Recipient: rfc822; moonsiri@gmail.com
Diagnostic-Code: smtp; 550-5.1.1 The email account that you tried to reach does not exist. Please try
 550-5.1.1 double-checking the recipient's email address for typos or
 550-5.1.1 unnecessary spaces. Learn more at
 550 5.1.1  https://support.google.com/mail/?p=NoSuchUser v1si2030228pjk.72 - gsmtp
Status: 5.1.1

 

 

자바 메일 API로 메일을 읽어올 때 content-type이 "text/plain" 일 때만 메일 내용이라 판단했는데,

읽어오지 못한 부분의 content-type은 "message/delivery-status"로 InputStream으로 데이터를 가져왔습니다.

 

if (StringUtils.contains(message.getContentType(), "multipart/report")) {
	Multipart multiPart = (Multipart) message.getContent();

	String msgContent = "";
	for (int i = 0; i < multiPart.getCount(); i++) {
		BodyPart part = multiPart.getBodyPart(i);
		
		Object o = part.getContent();
		
		if (o instanceof String) {
			msgContent = String.valueOf(o);

		} else if (o instanceof InputStream) {
			StringWriter writer = new StringWriter();
			IOUtils.copy(part.getInputStream(), writer);
			if (StringUtils.isNotBlank(msgContent)) {
				msgContent += "\r\n\r\n";
			}
			msgContent += writer.toString();
		}

	}
}

 

 

[Reference]

www.javaer101.com/ko/article/6426254.html

728x90
반응형

댓글