简体   繁体   中英

Get email content AWS SES

I am trying to get the content of an email I receive using SES. But the response I get is crazy

{
    "notificationType": "Received",
    "receipt": {
        "timestamp": "2015-09-11T20:32:33.936Z",
        "processingTimeMillis": 222,
        "recipients": [
            "recipient@example.com"
        ],
        "spamVerdict": {
            "status": "PASS"
        },
        "virusVerdict": {
            "status": "PASS"
        },
        "spfVerdict": {
            "status": "PASS"
        },
        "dkimVerdict": {
            "status": "PASS"
        },
        "action": {
            "type": "SNS",
            "topicArn": "arn:aws:sns:us-east-1:012345678912:example-topic"
        }
    },
    "mail": {
        "timestamp": "2015-09-11T20:32:33.936Z",
        "source": "61967230-7A45-4A9D-BEC9-87CBCF2211C9@example.com",
        "messageId": "d6iitobk75ur44p8kdnnp7g2n800",
        "destination": [
            "recipient@example.com"
        ],
        "headersTruncated": false,
        "headers": [
            {
                "name": "Return-Path",
                "value": "<0000014fbe1c09cf-7cb9f704-7531-4e53-89a1-5fa9744f5eb6-000000@amazonses.com>"
            },
            {
                "name": "Received",
                "value": "from a9-183.smtp-out.amazonses.com (a9-183.smtp-out.amazonses.com [54.240.9.183]) by inbound-smtp.us-east-1.amazonaws.com with SMTP id d6iitobk75ur44p8kdnnp7g2n800 for recipient@example.com; Fri, 11 Sep 2015 20:32:33 +0000 (UTC)"
            },
            {
                "name": "DKIM-Signature",
                "value": "v=1; a=rsa-sha256; q=dns/txt; c=relaxed/simple; s=ug7nbtf4gccmlpwj322ax3p6ow6yfsug; d=amazonses.com; t=1442003552; h=From:To:Subject:MIME-Version:Content-Type:Content-Transfer-Encoding:Date:Message-ID:Feedback-ID; bh=DWr3IOmYWoXCA9ARqGC/UaODfghffiwFNRIb2Mckyt4=; b=p4ukUDSFqhqiub+zPR0DW1kp7oJZakrzupr6LBe6sUuvqpBkig56UzUwc29rFbJF hlX3Ov7DeYVNoN38stqwsF8ivcajXpQsXRC1cW9z8x875J041rClAjV7EGbLmudVpPX 4hHst1XPyX5wmgdHIhmUuh8oZKpVqGi6bHGzzf7g="
            },
            {
                "name": "From",
                "value": "sender@example.com"
            },
            {
                "name": "To",
                "value": "recipient@example.com"
            },
            {
                "name": "Subject",
                "value": "Example subject"
            },
            {
                "name": "MIME-Version",
                "value": "1.0"
            },
            {
                "name": "Content-Type",
                "value": "text/plain; charset=UTF-8"
            },
            {
                "name": "Content-Transfer-Encoding",
                "value": "7bit"
            },
            {
                "name": "Date",
                "value": "Fri, 11 Sep 2015 20:32:32 +0000"
            },
            {
                "name": "Message-ID",
                "value": "<61967230-7A45-4A9D-BEC9-87CBCF2211C9@example.com>"
            },
            {
                "name": "X-SES-Outgoing",
                "value": "2015.09.11-54.240.9.183"
            },
            {
                "name": "Feedback-ID",
                "value": "1.us-east-1.Krv2FKpFdWV+KUYw3Qd6wcpPJ4Sv/pOPpEPSHn2u2o4=:AmazonSES"
            }
        ],
        "commonHeaders": {
            "returnPath": "0000014fbe1c09cf-7cb9f704-7531-4e53-89a1-5fa9744f5eb6-000000@amazonses.com",
            "from": [
                "sender@example.com"
            ],
            "date": "Fri, 11 Sep 2015 20:32:32 +0000",
            "to": [
                "recipient@example.com"
            ],
            "messageId": "<61967230-7A45-4A9D-BEC9-87CBCF2211C9@example.com>",
            "subject": "Example subject"
        }
    },
    "content": "Return-Path: <61967230-7A45-4A9D-BEC9-87CBCF2211C9@example.com>\r\nReceived: from a9-183.smtp-out.amazonses.com (a9-183.smtp-out.amazonses.com [54.240.9.183])\r\n by inbound-smtp.us-east-1.amazonaws.com with SMTP id d6iitobk75ur44p8kdnnp7g2n800\r\n for recipient@example.com;\r\n Fri, 11 Sep 2015 20:32:33 +0000 (UTC)\r\nDKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/simple;\r\n\ts=ug7nbtf4gccmlpwj322ax3p6ow6yfsug; d=amazonses.com; t=1442003552;\r\n\th=From:To:Subject:MIME-Version:Content-Type:Content-Transfer-Encoding:Date:Message-ID:Feedback-ID;\r\n\tbh=DWr3IOmYWoXCA9ARqGC/UaODfghffiwFNRIb2Mckyt4=;\r\n\tb=p4ukUDSFqhqiub+zPR0DW1kp7oJZakrzupr6LBe6sUuvqpBkig56UzUwc29rFbJF\r\n\thlX3Ov7DeYVNoN38stqwsF8ivcajXpQsXRC1cW9z8x875J041rClAjV7EGbLmudVpPX\r\n\t4hHst1XPyX5wmgdHIhmUuh8oZKpVqGi6bHGzzf7g=\r\nFrom: sender@example.com\r\nTo: recipient@example.com\r\nSubject: Example subject\r\nMIME-Version: 1.0\r\nContent-Type: text/plain; charset=UTF-8\r\nContent-Transfer-Encoding: 7bit\r\nDate: Fri, 11 Sep 2015 20:32:32 +0000\r\nMessage-ID: <61967230-7A45-4A9D-BEC9-87CBCF2211C9@example.com>\r\nX-SES-Outgoing: 2015.09.11-54.240.9.183\r\nFeedback-ID: 1.us-east-1.Krv2FKpFdWV+KUYw3Qd6wcpPJ4Sv/pOPpEPSHn2u2o4=:AmazonSES\r\n\r\nExample content\r\n"
}

I can easily get the subject, but I can't believe the message content is that hard to extract, the content is Example content and the only place I can find it is in the content key but it's very hard to parse

Yes it is !

As an example if you use gmail or another email client, go to view the raw data or source of your mail you'll see about the same thing (a bit different as here its a JSON representation) but emails' content are based on RFC2045

The Contents of Notifications for Amazon SES Email Receiving are documented and from the Top-Level JSON Object

content : String that contains the raw, unmodified email, which is typically in Multipurpose Internet Mail Extensions (MIME) format. For more information about MIME format, see RFC 2045.

Note This field is present only if the notification was triggered by an SNS action. Notifications triggered by all other actions do not contain this field.

This POJO might help:

public class SESMessageData {

public SESMessageData() {
}

private String notificationType;

private Receipt receipt;

private Mail mail;

public String getNotificationType() {
    return notificationType;
}

public void setNotificationType(String notificationType) {
    this.notificationType = notificationType;
}

public Receipt getReceipt() {
    return receipt;
}

public void setReceipt(Receipt receipt) {
    this.receipt = receipt;
}

public Mail getMail() {
    return mail;
}

public void setMail(Mail mail) {
    this.mail = mail;
}

public static class Receipt {

    private Action action;

    private Verdict dkimVerdict;

    private String processingTimeMillis;

    private Verdict spamVerdict;

    private Verdict spfVerdict;

    private String timestamp;

    private Verdict virusVerdict;

    private List<String> recipients;

    /**
     * @return A list of the recipient addresses for this delivery.
     */
    public List<String> getRecipients() {
        return recipients;
    }

    public void setRecipients(List<String> recipients) {
        this.recipients = recipients;
    }

    /**
     * @return Object that encapsulates information about the action that was executed.
     */
    public Action getAction() {
        return action;
    }

    public void setAction(Action action) {
        this.action = action;
    }

    /**
     * @return Object that indicates whether the DomainKeys Identified Mail (DKIM) check passed.
     */
    public Verdict getDkimVerdict() {
        return dkimVerdict;
    }

    public void setDkimVerdict(Verdict dkimVerdict) {
        this.dkimVerdict = dkimVerdict;
    }

    /**
     * @return String that specifies the period, in milliseconds, from the time Amazon SES received the message to the time it triggered the action.
     */
    public String getProcessingTimeMillis() {
        return processingTimeMillis;
    }

    public void setProcessingTimeMillis(String processingTimeMillis) {
        this.processingTimeMillis = processingTimeMillis;
    }

    /**
     * @return Object that indicates whether the message is spam.
     */
    public Verdict getSpamVerdict() {
        return spamVerdict;
    }

    public void setSpamVerdict(Verdict spamVerdict) {
        this.spamVerdict = spamVerdict;
    }

    /**
     * @return Object that indicates whether the Sender Policy Framework (SPF) check passed.
     */
    public Verdict getSpfVerdict() {
        return spfVerdict;
    }

    public void setSpfVerdict(Verdict spfVerdict) {
        this.spfVerdict = spfVerdict;
    }

    /**
     * @return String that specifies when the action was triggered, in ISO8601 format.
     */
    public String getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(String timestamp) {
        this.timestamp = timestamp;
    }

    /**
     * @return Object that indicates whether the message contains a virus.
     */
    public Verdict getVirusVerdict() {
        return virusVerdict;
    }

    public void setVirusVerdict(Verdict virusVerdict) {
        this.virusVerdict = virusVerdict;
    }

    /**
     * Object that encapsulates information about the action that was executed.
     */
    public class Action {
        private String type;
        private String topicArn;
        private String bucketName;
        private String objectKey;
        private String smtpReplyCode;
        private String statusCode;
        private String message;
        private String sender;
        private String functionArn;
        private String invocationType;
        private String organizationArn;

        /**
         * @return String that indicates the type of action that was executed. Possible values are S3, SNS, Bounce, Lambda, Stop, and WorkMail.
         */
        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        /**
         * @return String that contains the Amazon Resource Name (ARN) of the Amazon SNS topic to which the notification was published.
         */
        public String getTopicArn() {
            return topicArn;
        }

        public void setTopicArn(String topicArn) {
            this.topicArn = topicArn;
        }

        /**
         * @return String that contains the name of the Amazon S3 bucket to which the message was published. Present only for the S3 action type.
         */
        public String getBucketName() {
            return bucketName;
        }

        public void setBucketName(String bucketName) {
            this.bucketName = bucketName;
        }

        /**
         * @return String that contains a name that uniquely identifies the email in the Amazon S3 bucket. This is the same as the messageId in the mail object. Present only for the S3 action type.
         */
        public String getObjectKey() {
            return objectKey;
        }

        public void setObjectKey(String objectKey) {
            this.objectKey = objectKey;
        }

        /**
         * @return String that contains the SMTP reply code, as defined by RFC 5321. Present only for the bounce action type.
         */
        public String getSmtpReplyCode() {
            return smtpReplyCode;
        }

        public void setSmtpReplyCode(String smtpReplyCode) {
            this.smtpReplyCode = smtpReplyCode;
        }

        /**
         * @return String that contains the SMTP enhanced status code, as defined by RFC 3463. Present only for the bounce action type.
         */
        public String getStatusCode() {
            return statusCode;
        }

        public void setStatusCode(String statusCode) {
            this.statusCode = statusCode;
        }

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }

        public String getSender() {
            return sender;
        }

        public void setSender(String sender) {
            this.sender = sender;
        }

        public String getFunctionArn() {
            return functionArn;
        }

        public void setFunctionArn(String functionArn) {
            this.functionArn = functionArn;
        }

        public String getInvocationType() {
            return invocationType;
        }

        public void setInvocationType(String invocationType) {
            this.invocationType = invocationType;
        }

        public String getOrganizationArn() {
            return organizationArn;
        }

        public void setOrganizationArn(String organizationArn) {
            this.organizationArn = organizationArn;
        }

        @Override
        public String toString() {
            return "Action{" +
                   "type='" + type + '\'' +
                   ", topicArn='" + topicArn + '\'' +
                   ", bucketName='" + bucketName + '\'' +
                   ", objectKey='" + objectKey + '\'' +
                   ", smtpReplyCode='" + smtpReplyCode + '\'' +
                   ", statusCode='" + statusCode + '\'' +
                   ", message='" + message + '\'' +
                   ", sender='" + sender + '\'' +
                   ", functionArn='" + functionArn + '\'' +
                   ", invocationType='" + invocationType + '\'' +
                   ", organizationArn='" + organizationArn + '\'' +
                   '}';
        }
    }

    public class Verdict {
        private String status;

        public String getStatus() {
            return status;
        }

        public void setStatus(String status) {
            this.status = status;
        }

        @Override
        public String toString() {
            return "Verdict{" +
                   "status='" + status + '\'' +
                   '}';
        }
    }

    @Override
    public String toString() {
        return "Receipt{" +
               "action=" + action +
               ", dkimVerdict=" + dkimVerdict +
               ", processingTimeMillis='" + processingTimeMillis + '\'' +
               ", spamVerdict=" + spamVerdict +
               ", spfVerdict=" + spfVerdict +
               ", timestamp='" + timestamp + '\'' +
               ", virusVerdict=" + virusVerdict +
               ", recipients=" + recipients +
               '}';
    }
}

public static class Mail {

    private List<String> destination;

    private String messageId;

    private String source;

    private String timestamp;

    private List<Header> headers;

    private CommonHeader commonHeaders;

    private String headersTruncated;

    /**
     * @return A list of email addresses that are recipients of the email.
     */
    public List<String> getDestination() {
        return destination;
    }

    public void setDestination(List<String> destination) {
        this.destination = destination;
    }

    public String getMessageId() {
        return messageId;
    }

    public void setMessageId(String messageId) {
        this.messageId = messageId;
    }

    public String getSource() {
        return source;
    }

    public void setSource(String source) {
        this.source = source;
    }

    public String getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(String timestamp) {
        this.timestamp = timestamp;
    }

    public List<Header> getHeaders() {
        return headers;
    }

    public void setHeaders(List<Header> headers) {
        this.headers = headers;
    }

    public CommonHeader getCommonHeaders() {
        return commonHeaders;
    }

    public void setCommonHeaders(CommonHeader commonHeaders) {
        this.commonHeaders = commonHeaders;
    }

    public String getHeadersTruncated() {
        return headersTruncated;
    }

    public void setHeadersTruncated(String headersTruncated) {
        this.headersTruncated = headersTruncated;
    }

    public class Header {

        private String name;
        private String value;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }

        @Override
        public String toString() {
            return "Header{" +
                   "name='" + name + '\'' +
                   ", value='" + value + '\'' +
                   '}';
        }
    }

    public class CommonHeader {

        private String returnPath;

        private List<String> from;

        private List<String> to;

        private String messageId;

        private String subject;

        public String getReturnPath() {
            return returnPath;
        }

        public void setReturnPath(String returnPath) {
            this.returnPath = returnPath;
        }

        public List<String> getFrom() {
            return from;
        }

        public void setFrom(List<String> from) {
            this.from = from;
        }

        public List<String> getTo() {
            return to;
        }

        public void setTo(List<String> to) {
            this.to = to;
        }

        public String getMessageId() {
            return messageId;
        }

        public void setMessageId(String messageId) {
            this.messageId = messageId;
        }

        public String getSubject() {
            return subject;
        }

        public void setSubject(String subject) {
            this.subject = subject;
        }

        @Override
        public String toString() {
            return "CommonHeader{" +
                   "returnPath='" + returnPath + '\'' +
                   ", from=" + from +
                   ", to=" + to +
                   ", messageId='" + messageId + '\'' +
                   ", subject='" + subject + '\'' +
                   '}';
        }
    }

    @Override
    public String toString() {
        return "Mail{" +
               "destination=" + destination +
               ", messageId='" + messageId + '\'' +
               ", source='" + source + '\'' +
               ", timestamp='" + timestamp + '\'' +
               ", headers=" + headers +
               ", commonHeaders=" + commonHeaders +
               ", headersTruncated='" + headersTruncated + '\'' +
               '}';
    }
}

@Override
public String toString() {
    return "SESMessageData{" +
           "notificationType='" + notificationType + '\'' +
           ", receipt=" + receipt +
           ", mail=" + mail +
           '}';
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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