在Outlook帐户中收到电子邮件时,我想将附件保存到AWS存储桶中。
我尝试使用具有HttpPostedFileBase
属性的InputStream
。
我尝试如下
//successfully saving 'HttpPostedFileBase' files
using (IAmazonS3 s3client = new AmazonS3Client(_awsAccessKey, _awsSecretKey, RegionEndpoint.USEast1)) {
PutObjectRequest putObjectRequest = new PutObjectRequest {
BucketName = _bucketName,
CannedACL = S3CannedACL.PublicRead,
Key = string.Format("uploads/" + AssignedTaskId.ToString() + "/" + Path.GetFileName(postedFile.FileName)),
InputStream = postedFile.InputStream
};
s3client.PutObject(putObjectRequest);
}
//but cannot get from Outlook.MailItem attachments
Outlook.MailItem newEmail = null;
foreach (object collectionItem in inBoxItems) {
newEmail = collectionItem as Outlook.MailItem;
}
using (IAmazonS3 s3client = new AmazonS3Client(_awsAccessKey, _awsSecretKey, RegionEndpoint.USEast1)) {
PutObjectRequest putObjectRequest = new PutObjectRequest {
BucketName = _bucketName,
CannedACL = S3CannedACL.PublicRead,
Key = string.Format("uploads/{0}", newEmail.Attachments[i].FileName),
InputStream = newEmail.Attachments[i].InputStream
};
s3client.PutObject(putObjectRequest);
}
}
遇到错误:
“附件”不包含“ InputStream”的定义,找不到可访问的扩展方法“ InputStream”,该类型接受第一个类型为“附件”的自变量(您是否缺少using指令或程序集引用?)>
答案 0 :(得分:0)
Outlook对象模型没有为此提供任何属性。相反,您可以访问一个低级属性:
const string PR_ATTACH_DATA_BIN = "http://schemas.microsoft.com/mapi/proptag/0x37010102";
Outlook.Attachment attachment = mail.Attachments[0];
// Retrieve the attachment as a byte array
var attachmentData = attachment.PropertyAccessor.GetProperty(PR_ATTACH_DATA_BIN);
另一种选择是将附件(请参见Attachment.SaveAsFile
)保存到文件,然后以字节流的形式打开文件。