发送到循环中的多个电子邮件地址时,附件流变为 0 字节

Attachments stream turns to 0 Byte when sending to multiple email addresses in loop

我循环发送多个附件到电子邮件地址。附件已正确发送到列表中的第一个电子邮件地址,但当它向前循环列表中的其他电子邮件地址时,参数 - 模型中的附件流在长度 i 中变为 0。 e. 0 个字节。

查看模型代码:

public class BulkMailViewModel
{
    public string EmailTo { get; set; }

    public string EmailSubject { get; set; }

    public string EmailBody { get; set; }

    public List<HttpPostedFileBase> Attachments { get; set; }

    public BulkMailViewModel()
    {
        Attachments = new List<HttpPostedFileBase>();
    }
}

控制器代码:在控制器方法下方运行电子邮件地址循环以发送电子邮件。

public async Task<ActionResult> SendEmail(BulkMailViewModel model)
    {
        List<HttpPostedFileBase> attachments = new List<HttpPostedFileBase>();
        bool isEmailSentMain = true;
        if (model.Attachments != null && model.Attachments.Any())
        {
            foreach (var attachment in model.Attachments)
            {
                if (attachment != null)
                {
                    attachments.Add(attachment);
                }
            }
        }
        string emailContent = System.Uri.UnescapeDataString(model.EmailBody);
        try
        {
            List<string> clientEmails = new List<string>() { };

            clientEmails.Add("abc@abc.com");
            clientEmails.Add("pqr@pqr.com");

            foreach (var email in clientEmails)
            {
                try
                {
                    bool isEmailSent = false;
                    if (email != null && email != "")
                    {
                        isEmailSent = await SendBulkEmailAsync(email, model.EmailSubject, emailContent, attachments, true);

                        if (!isEmailSent)
                        {
                            isEmailSentMain = false;
                        }
                    }
                }
                catch (Exception e)
                {
                    isEmailSentMain = false;
                    return Json(new { status = isEmailSentMain, type = "continue", message = "Error while sending Email." });
                }
            }

        }

        catch (Exception e)
        {
            return Json(new { status = false, message = e.ToString()});
        }
    }

邮件方法代码:该方法实际发送邮件。

public async Task<bool> SendBulkEmailAsync(string emailTo, string emailSubject, string emailContent, List<HttpPostedFileBase> attachmentList, bool isBodyHtml = false)
    {
        List<HttpPostedFileBase> attachments = attachmentList;
        using (SmtpClient smtp = new SmtpClient()
        {
            //My SMTP Settings
        })

        using (var message = new MailAddress("email@abc.com", emailTo))
        {

            if (attachments != null)
            {
                foreach (var attach in attachments)
                {
                    string strFileName = System.IO.Path.GetFileName(attach.FileName);
                    attach.InputStream.Position = 0;
                    Attachment attachFile =
                    new Attachment(attach.InputStream, strFileName, attach.ContentType);
                    message.Attachments.Add(attachFile);
                }
            }
            message.Subject = emailSubject;
            message.Body = emailContent;
            message.IsBodyHtml = isBodyHtml;
            await smtp.SendMailAsync(message);

            return true;
        }
    }

有人可以帮忙吗?

您必须在本地流中克隆 attach.InputStream 的内容以避免它被 SendMailAsync 关闭:

            foreach (var attach in attachments)
            {
                string strFileName = System.IO.Path.GetFileName(attach.FileName);
                attach.InputStream.Position = 0;
                MemoryStream tempStream = new MemoryStream();
                attach.InputStream.CopyTo(tempStream);
                tempStream.Position = 0;
                Attachment attachFile =
                new Attachment(tempStream, strFileName, attach.ContentType);
                message.Attachments.Add(attachFile);
            }