获取附件并保存在S3 Bucket MailKit IMAP中
我需要获取电子邮件中的附件进行保存,我按照需要完成了阅读电子邮件和保存的整个过程,但是当我尝试将附件保存在 AWS S3 存储桶中时,它会出现错误它尝试读取内容类型。 基本上我需要做的是。
- 获取附件。
- 为我的 S3 保存功能创建一个 FormFile 以保存附件(S3 保存功能已经运行)。 我在构建 FormFile 时遇到问题。
.
.
.
var attachmentsToS3 = new FormFileCollection();
foreach (var attachment in message.Attachments)
{
var part = attachment as MimePart;
var stream = new MemoryStream();
await part.Content.DecodeToAsync(stream);
var fileLength = stream.Length;
var formFile = new FormFile(stream, 0, fileLength, "file[]", attachment.ContentDisposition.FileName);
attachmentsToS3.Add(formFile);
}
await _atendimentoServices.SaveAnexoAtendimentoToS3( attachmentsToS3, idAcompanhamento, requestApi);
.
.
.
更新
根据布鲁诺的答案进行的更改现在正在运行。
var attachmentsToS3 = new FormFileCollection();
foreach (MimeEntity attachment in message.Attachments)
{
var memory = new MemoryStream();
if (attachment is MimePart part)
await part.Content.DecodeToAsync(memory);
else
await ((MessagePart)attachment).Message.WriteToAsync(memory);
var bytes = memory.ToArray();
var contentType = MimeTypes.GetMimeType(attachment.ContentType.MimeType);
var formFile = new FormFile(memory, 0, bytes.Length, "file[]", attachment.ContentDisposition.FileName)
{
Headers = new HeaderDictionary(),
ContentType = contentType
};
attachmentsToS3.Add(formFile);
}
await _atendimentoServices.SaveAnexoAtendimentoToS3(attachmentsToS3, idAcompanhamento, requestApi);```
I need to get the attachments that are in the email to save, I do the whole process of reading the email and saving as I need to, but when I try to get the attachments to save in the AWS S3 Bucket it gives an error when it tries to read the content type.
Basically what I need to do is.
- Get the attachment.
- Create a FormFile for my S3 save function to save the attachments (The S3 save function is already working).
I am having problem just to build the FormFile.
.
.
.
var attachmentsToS3 = new FormFileCollection();
foreach (var attachment in message.Attachments)
{
var part = attachment as MimePart;
var stream = new MemoryStream();
await part.Content.DecodeToAsync(stream);
var fileLength = stream.Length;
var formFile = new FormFile(stream, 0, fileLength, "file[]", attachment.ContentDisposition.FileName);
attachmentsToS3.Add(formFile);
}
await _atendimentoServices.SaveAnexoAtendimentoToS3( attachmentsToS3, idAcompanhamento, requestApi);
.
.
.
UPDATE
Changes made based on bruno's answer and it is now working.
var attachmentsToS3 = new FormFileCollection();
foreach (MimeEntity attachment in message.Attachments)
{
var memory = new MemoryStream();
if (attachment is MimePart part)
await part.Content.DecodeToAsync(memory);
else
await ((MessagePart)attachment).Message.WriteToAsync(memory);
var bytes = memory.ToArray();
var contentType = MimeTypes.GetMimeType(attachment.ContentType.MimeType);
var formFile = new FormFile(memory, 0, bytes.Length, "file[]", attachment.ContentDisposition.FileName)
{
Headers = new HeaderDictionary(),
ContentType = contentType
};
attachmentsToS3.Add(formFile);
}
await _atendimentoServices.SaveAnexoAtendimentoToS3(attachmentsToS3, idAcompanhamento, requestApi);```
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
下午好,加布里埃尔,在分析您的代码时,我注意到缺少某些代码部分。
首先,您需要像这样处理您的附件:
这样您就可以保证您的流将正确填充,无论是否为 mimeType。
缺少的另一部分是 FormFile 构造函数中的标题和内容类型,请尝试一下。
Good Afternoon Gabriel, Analysing you code I noticed there were missing some parts of code.
the first you need to trate your attachment to like this:
With that you'll garanted your stream will be populated correctly been a mimeType or not.
The other part was missing is the header and the content type at your FormFile constructor, try that.