Amazon SES使用来自S3ObjectInputStream对象的java读取存储在s3存储桶中的电子邮件
我已将 AWS SES
配置为将所有传入的电子邮件存储到具有对象密钥前缀为电子邮件的S3存储桶中。我有一个Java应用程序使用我试图读取该存储桶中的所有对象,然后将它们移动到另一个,以便只有未读的电子邮件保留在存储桶中。我使用以下代码:
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
}
}
我不知道如何从 S3ObjectInputStream $获取电子邮件内容,发件人详细信息,cc详细信息等c $ c>我拥有的对象。如何处理此对象以获取我需要的所有详细信息。
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.
您必须将输入流转换为MIME消息可以按如下方式完成:
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;
}
获得Mime消息后,使用解析器从中读取内容消息:
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);
通过这种方式,您将能够读取存储为S3对象的电子邮件的所有内容一个S3桶
This way, you will be able to read all the contents of the email stored as an S3 object in an S3 bucket