获取附件并保存在S3 Bucket MailKit IMAP中

发布于 2025-01-20 17:34:33 字数 1633 浏览 2 评论 0原文

我需要获取电子邮件中的附件进行保存,我按照需要完成了阅读电子邮件和保存的整个过程,但是当我尝试将附件保存在 AWS S3 存储桶中时,它会出现错误它尝试读取内容类型。 基本上我需要做的是。

  1. 获取附件。
  2. 为我的 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.

  1. Get the attachment.
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

源来凯始玺欢你 2025-01-27 17:34:33

下午好,加布里​​埃尔,在分析您的代码时,我注意到缺少某些代码部分。

首先,您需要像这样处理您的附件:

if (attachment is MimePart part) await part.Content.DecodeToAsync(memory);
else await ((MessagePart)attachment).Message.WriteToAsync(memory);

这样您就可以保证您的流将正确填充,无论是否为 mimeType。

缺少的另一部分是 FormFile 构造函数中的标题和内容类型,请尝试一下。

var formFile = new FormFile(memory, 0, bytes.Length, "file[]", attachment.ContentDisposition.FileName)
                            {
                                Headers = new HeaderDictionary(),
                                ContentType = MimeTypes.GetMimeType(attachment.ContentType.MimeType);
                            };

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:

if (attachment is MimePart part) await part.Content.DecodeToAsync(memory);
else await ((MessagePart)attachment).Message.WriteToAsync(memory);

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.

var formFile = new FormFile(memory, 0, bytes.Length, "file[]", attachment.ContentDisposition.FileName)
                            {
                                Headers = new HeaderDictionary(),
                                ContentType = MimeTypes.GetMimeType(attachment.ContentType.MimeType);
                            };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文