c# - 内存流中的电子邮件附件出错,但仅在第一次发送后发生

标签 c# asp.net-mvc

我做了一个循环,将包含相同附件的电子邮件发送到不同的电子邮件:第一次发送没有问题,从第二次发送附件就损坏了:

    public Dictionary<MemoryStream, string> GetDocumentsForEmail(int idCorso)
    {
        string response = string.Empty;
        var dictionary = new Dictionary<MemoryStream, string>();

        var documents = (from d in db.Documents
                         where d.IDCorso == idCorso
                         select d).ToList();
        if (documents.Count > 0)
        {
            foreach (var doc in documents)
            {
                var file = new MyFileHelper().GetDownloadFile(idCorso, doc.IDFileType);
                if (file != null)
                {
                    dictionary.Add(file.Item1, file.Item2);
                }
            }
            return dictionary;
        }
        return null;
    }

在帮助文件中我有这个:

    public Tuple<MemoryStream, string> GetDownloadFile(int IDCorso, int fileType)
    {
        var document = (from d in db.Documents
                         where d.IDCorso == IDCorso && d.IDFileType == fileType
                         select d).FirstOrDefault();
        if (document != null)
        {
            string fileName = document.FileName.Trim() + document.FileExtension.Trim();
            byte[] fileBytes = document.FileContent;
            MemoryStream ms = new MemoryStream(fileBytes);
            return Tuple.Create(ms, fileName);
         }
        return null;
    }

这里用循环调用sendmail

    ...
    var listAttachment = new List<Dictionary<MemoryStream, string>>();
    listAttachment.Add(GetDocumentsForEmail(idCorso));
    foreach (var item in users)
    {
        listTo.Clear();
        var emailUser = GetEmailUser(item.ci.IDUser).ToString();
        listTo.Add(emailUser);
        mail.SendEmail(listTo, ..., listAttachment);
    }

在其他帮助文件中发送电子邮件

public class MyMailHelper
{
    public void SendEmail(List<string> mailTOList..., List<Dictionary<MemoryStream, string>> mailAttachment = null)
    {
       ...
       SmtpClient SmtpServer = new SmtpClient();
       MailMessage mail = new MailMessage(){...}
       ...
       if (mailAttachment != null)
       {
          foreach (var item in mailAttachment)
          {
             foreach (var key in item.Keys)
             {
                mail.Attachments.Add(new Attachment(key, item[key]));
             }
          }
        }
        SmtpServer.Send(mail);

一切正常,但只有第一次发送附件才可读,在附件损坏后!
感谢您的帮助

最佳答案

到达流末尾后,您将无法从中读取更多数据。 这就是为什么你应该在使用后将其设置为 0。

ms.Position=0;

关于c# - 内存流中的电子邮件附件出错,但仅在第一次发送后发生,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60623759/

相关文章:

c# - C# 4.0 中动态的意图是什么? "neat"有哪些用途?

c# - ASP.Net MVC : Calling a method from a view

c# - 无法从 Entity Framework 中的模型代码生成数据库

asp.net - 如何在开发过程中管理来自子域的静态内容服务

c# - 在 ASP.NET Core 中使用 Azure Active Directory B2C 在 Web API 中测试云身份验证 - 返回错误 "invalid_request"

c# - 如何在 DevExpress XtraGrid 中获取点击的单元格列

c# - 将用户控件添加到 Windows 窗体 C# 时出错

c# - Response.StatusCode 在 nunit 测试中为 null

php - Magento 与 nopCommerce

c# - System.Drawing.Color -state 有什么值(value)?