Amazon SES reading emails stored in an s3 bucket using java from S3ObjectInputStream object

狂风中的少年 提交于 2019-12-06 14:01:31

You have to convert the inputstream to a MIME Message which can be done as follows:

for (S3ObjectSummary s3ObjectSummary : summaries) 
{
    String key = s3ObjectSummary.getKey();//getting the key of the item
    S3Object object = s3.getObject(
              new GetObjectRequest(incommingBucket, key));
    InputStream mailFileInputStream = object.getObjectContent();
    String bucketKey = object.getKey();
    MimeMessage message = getMimeMessageForRawEmailString(mailFileInputStream);//converting input stream to mime message
    object.close();
}

public MimeMessage getMimeMessageForRawEmailString(InputStream mailFileInputStream) throws Exception
{
    Properties props = new Properties();
    Session session = Session.getDefaultInstance(props, null);
    MimeMessage message = new MimeMessage(session, mailFileInputStream);
    return message;
}

Once you have the Mime message, use a parser to read the contents from that message:

MimeMessageParser parser = new MimeMessageParser(message);
String from = parser.getFrom();
String htmlContent = parser.parse().getHtmlContent();
System.out.println("Body: "+htmlContent);
String plain = parser.parse().getPlainContent();
System.out.println("plain: "+plain);

This way, you will be able to read all the contents of the email stored as an S3 object in an S3 bucket

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!