在循环中向多个电子邮件地址发送时,附件流变成0字节。

问题描述 投票:0回答:1

我正在循环发送多个附件到电子邮件地址。附件正确地发送到列表中的第一个电子邮件地址,但当它循环向前发送列表中的其他电子邮件地址时,参数-模型中的附件流长度变为0,即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>();
    }
}

控制器代码。下面的Controller方法对邮件地址进行循环运行,发送邮件。

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("[email protected]");
            clientEmails.Add("[email protected]");

            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 protected]", 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;
        }
    }

谁能帮忙解决这个问题?

c# asp.net c#-4.0 smtp email-attachments
1个回答
1
投票

你必须克隆内容的 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);
            }
© www.soinside.com 2019 - 2024. All rights reserved.