我已将AWS SES
配置为将所有传入的电子邮件存储到S3存储桶,其中对象键前缀为电子邮件。我有一个Java应用程序使用我试图读取该存储桶中的所有对象,然后将它们移动到另一个,以便只有未读的电子邮件保留在存储桶中。我使用以下代码:
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;
}
}
我有另一个服务类访问上面的类来获取电子邮件列表并将它们存储到我的数据库中。代码如下所示:
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详细信息等。如何处理此对象以获取我需要的所有详细信息。
答案 0 :(得分:3)
您必须将输入流转换为MIME消息,可以按如下方式完成:
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消息后,使用解析器从该消息中读取内容:
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对象的电子邮件的所有内容