简体   繁体   中英

Amazon SES: Getting the real recipient of an inbound email

I am currently receiving emails from Amazon SES, which I store on Amazon S3. Then I have a program (written in Java) which retrieve emails from S3, parse them and do a specific action based on the recipient's name.

The issue

When an email is sent to 2 recipients (let's say A@mydomain.com and B@mydomain.com), Amazon SES receive 2 emails and store both of them in S3.

Both emails are identical (except for some custom headers set by my email client).

My Java program retrieve 2 emails from S3 but I can't determine for whom each email is intended , as both email addresses are present in the "TO" header of both emails.

What I found

  • Amazon SES store the MIME Message in S3

  • Based on the Internet Message Format RFC , MIME Message contains the full list of recipients, but not the actual recipient. I guess this information is not part of the Mime Message, but part of the protocol.

It means that:

  • Amazon SES must know the recipient
  • Once the mail is stored in S3, this information is lost.

Based on the fact that this analyze is correct (which might not be), I wanted to use Amazon Lambda expressions to retrieve the protocols information and store them as metadata of the email in S3. However, I cannot find a way to retrieve the protocols informations.

Any idea?

Does someone has any idea how to achieve this? It would be nice if I could get the recipient's email address of each email from a Lambda expression, but I didn't found a lot of references about this.

Maybe my interpretation of how this work (the email protocol / process) is completely wrong and someone could give me some explanation! :D

Some code to go with it

I found some code displaying the information we have if we do a lambda function at the SES level, but I didn't find the information I needed there.

https://gist.github.com/gonfva/b249f76893165bf5a8d1

The S3Object also contains some Metadata but they don't seem to be useful.

Bellow is the Java code I use to retrieve emails from Amazon.

package fr.novapost.delivery.emailer.listener.amazon;

import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.GetObjectRequest;
import com.amazonaws.services.s3.model.S3Object;
import com.amazonaws.services.s3.model.S3ObjectId;
import org.apache.commons.mail.util.MimeMessageParser;

import javax.mail.Address;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.MimeMessage;
import java.util.List;
import java.util.Properties;

public class Main {

public static void main(String[] args) {
    // Fill these variables with the proper values.
    String objectName = "";
    String bucketName = "";
    String accessKey = "";
    String secretAccessKey = "";

    BasicAWSCredentials awsCred = new BasicAWSCredentials(accessKey, secretAccessKey);
    AWSCredentialsProvider credentialProvider = new AWSStaticCredentialsProvider(awsCred);

    // Create S3 client
    AmazonS3 amazonS3 = AmazonS3ClientBuilder.standard().withCredentials(credentialProvider).withRegion(Regions.EU_WEST_1).build();

    // Retrieve object from S3
    GetObjectRequest request = new GetObjectRequest(new S3ObjectId(bucketName, objectName));
    S3Object s3Object = amazonS3.getObject(request);

    // Get content of the object (which is the MimeMessage) and create a MimeMessage from it.
    Session s = Session.getInstance(new Properties());
    try {
        MimeMessage mail = new MimeMessage(s, s3Object.getObjectContent());

        // Parse the mime message thanks to the org.apache.commons.mail.util.MimeMessageParser class.
        MimeMessageParser mimeParser = new MimeMessageParser(mail);
        mimeParser.parse();

        // Get the recipient's list.
        // It contains ALL the recipients.
        // I want to know for which specific recipient of this list this email was intended.
        List<Address> addresses = mimeParser.getTo();

        for (Address address : addresses) {
            System.out.println("recipient: " + address.toString());
        }
    } catch (MessagingException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
  }
}

The recipients as they may be suggested by To: and Cc: mail headers are irrelevant, as those are not actually used for delivery (and in the case of BCC, the recipient should definitely not appear there).

You should find that SES adds the actual envelope recipient (which is what you are looking for) in the first Received: header from the top.

Return-Path: <...>
Received: from x-x-x (x-x-x [x.x.x.x])
 by inbound-smtp.us-east-1.amazonaws.com with SMTP id xxxxxxxxxxxxxxxxxxxx
 for the-actual-recipient-is-this-address@example.com;
 Sat, 15 Jul 2017 05:07:18 +0000 (UTC)
X-SES-Spam-Verdict: PASS
X-SES-Virus-Verdict: PASS

Received: headers are prepended as mail is processed, so you they become less trustworthy as you work down -- but the top one comes from SES itself.

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