I have configured my AWS SES
to store all incoming emails to an S3 bucket with Object key Prefix as Email. I have a Java application using with I am trying to read all objects in that bucket and then move them to another so that only the unread emails remain in the bucket. I use the following code:
public class FileReadImpl
{
private static final Logger logger = LoggerFactory.getLogger(FileReadImpl.class);
AmazonS3 s3;
public void init(String accessKey, String secretKey)
{
s3 = new AmazonS3Client(new BasicAWSCredentials(accessKey, secretKey));
}
public List<S3ObjectInputStream> readEmailsAndMoveToRead(String accessKeyId, String secretAccessKey, String incommingBucket, String processedBucket)
{
List<S3ObjectInputStream> s3ObjectInputStreamList = new ArrayList<S3ObjectInputStream>();
AWSCredentials credentials = new BasicAWSCredentials(accessKeyId, secretAccessKey);
AmazonS3 s3 = new AmazonS3Client(credentials);
ObjectListing listing = s3.listObjects(incommingBucket, "Email/");
List<S3ObjectSummary> summaries = listing.getObjectSummaries();
while (listing.isTruncated())
{
listing = s3.listNextBatchOfObjects (listing);
summaries.addAll (listing.getObjectSummaries());
}
for (S3ObjectSummary s3ObjectSummary : summaries)
{
String key = s3ObjectSummary.getKey();//getting the key of the item
S3Object object = s3.getObject(
new GetObjectRequest(incommingBucket, key));
S3ObjectInputStream inuptStream = object.getObjectContent();
s3ObjectInputStreamList.add(inuptStream);
if(!s3.doesBucketExist(processedBucket))
{
s3.createBucket(processedBucket);
}
s3.copyObject(incommingBucket, key, processedBucket, key);
s3.deleteObject(incommingBucket, key);
try
{
inuptStream.close();
}
catch (IOException e)
{
logger.error(e.toString());
}
}
return s3ObjectInputStreamList;
}
}
I have another service class that access the above class to get the list of emails and store them to my database. The code is as shown below:
public void getEmails()
{
FileReadImpl fileReadImpl = new FileReadImpl();
List<S3ObjectInputStream> s3ObjectInputStreamList = fileReadImpl.readEmailsAndMoveToRead("accessKeyId", "secretAccessKey", "incomingBucket", "processedBucket");
for (S3ObjectInputStream s3ObjectInputStream : s3ObjectInputStreamList)
{
//logic to save the email content as emails
}
}
I am not sure how to get the email content, the senders details, the cc details etc from the S3ObjectInputStream
object that I have. How do I process this object to get all the details I need.