gpt4 book ai didi

c# - 为电子邮件附件使用多个内存流

转载 作者:太空宇宙 更新时间:2023-11-03 13:18:46 26 4
gpt4 key购买 nike

我正在尝试使用多个内存中流构建一个 EmailMessage。但是,当我发送电子邮件时,出现以下错误:

"One of the streams has already been used and can't be reset to the origin"

据我所知,我认为问题可能是当我尝试执行以下操作时消息正在丢失内存流的上下文:

    foreach (var att in attachments)
{
doc = fetchDocumentByteArray();
using (MemoryStream ms = new MemoryStream(doc))
{
mailToSend.AddAttachment(new Attachment(ms, att.Filename));
}
}
mailToSend.Send();

我也试过设置 ms.Position = 0在 AddAttachment() 之前,但这不起作用。

在四处寻找像我这样的场景后,我发现了使用 List<MemoryStream> 的建议。 - 但我不确定我将如何实现它,或者它是否是正确的方法?

最佳答案

当您使用“using”键时,会在内部调用 Dispose 方法。它将使 MemoryStreams 被释放。

删除内部循环中的 using 并创建一个 try/finally 子句以在发送电子邮件后处理内存流。

关于c# - 为电子邮件附件使用多个内存流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25081225/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com