简体   繁体   中英

How to get the response after sending an email using amazon SES SMTP interface?

The document says that after sending an email, no matter it was successful or not, SES will return a response to the sender, includeing message ID and an error if it wasn't sent successfully. 在此处输入图像描述 https://docs.aws.amazon.com/ses/latest/DeveloperGuide/sending-concepts-process.html

I am wondering how to get this response?

I am using SMTP and JavaMail to send emails, like: transport.sendMessage(msg, msg.getAllRecipients());

The sendMessage method doesn't return anything. So how can I get the response?

Thanks in advance!

Update:

In https://forums.aws.amazon.com/thread.jspa?messageID=363239 , it is said that

SMTP returns the message ID appended to the OK response to the DATA command. For example: 250 Ok 0000aaaaccccaaaacccc-ccccaacc-aaaa-cccc-aaaa-acccccaaaaae-000000

Could anyone teach me how to extract the message ID from the OK response?

At best you can register a TransportListener with Transport , this listener will be called with TransportEvent . This TransportListener is called every time Transport object emits an events like MESSAGE_DELIVERED , MESSAGE_NOT_DELIVERED , MESSAGE_PARTIALLY_DELIVERED .

You can do something like this -

// Create a transport.
Transport transport = session.getTransport();

//Register your event listener
//This TransportListener is called every time Transport object emits an events like `MESSAGE_DELIVERED`, `MESSAGE_NOT_DELIVERED`, `MESSAGE_PARTIALLY_DELIVERED`.
transport.addTransportListener(new TransportListener() {
    @Override
    public void messageDelivered(TransportEvent transportEvent) {
        System.out.println("From Message Delivered");
        System.out.println(transportEvent.getMessage());
    }

    @Override
    public void messageNotDelivered(TransportEvent transportEvent) {
        System.out.println("From Message Not Delivered");
        System.out.println(transportEvent.getMessage());
    }

    @Override
    public void messagePartiallyDelivered(TransportEvent transportEvent) {
        System.out.println("From Message Partially Delivered");
        System.out.println(transportEvent.getMessage());
    }
});

I think that You can use configuration set for it. It only requires adding such header X-SES-CONFIGURATION-SET with the value of the title of Your configuration set.

Here is full link: https://docs.aws.amazon.com/ses/latest/DeveloperGuide/send-using-smtp-java.html

Later, when You configure the configuration set, You can set it up to push the delivery, bounce, etc notifications on SNS. Which You can subscribe to in Your application.

Here is a link: https://docs.aws.amazon.com/ses/latest/DeveloperGuide/using-configuration-sets-in-email.html

And the link for the delivery object: https://docs.aws.amazon.com/ses/latest/DeveloperGuide/notification-contents.html#delivery-object

I fugured it out.

I should used getLastServerResponse() method of SMTPTransport class, such as

        String response = transport.getLastServerResponse();
        System.out.println("response: " + response);

The output will be like response: 250 Ok 0100017352b73695-a103f18d-f0a3-4a48-9d86-db1df264a3fe-000000

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