从smtp服务器mvc 5成功发送后,电子邮件附件无法打开

时间:2019-02-14 09:54:59

标签: jquery ajax asp.net-mvc-5 smtpclient

在formdata中使用Ajax向控制器发送文件后,我正在发送带有附件的电子邮件。电子邮件发送成功,但是当我打开该电子邮件以在我的电子邮件帐户中查看该电子邮件时,该电子邮件已附加但未打开,显示错误味精

显示此图片有问题

List < HttpPostedFileBase > attachments = new List<HttpPostedFileBase>();
HttpPostedFileBase frontimage = null;
foreach(string fileName in Request.Files)
{
    frontimage = Request.Files["frontimage"];
}

attachments.Add(frontimage);
using(MailMessage mm = new MailMessage("example@gmail.com", "example2@hotmail.com"))
{
    mm.Subject = "Test Email";
    mm.Body = "test";

    foreach(HttpPostedFileBase attachment in attachments)
    {
        if (attachment != null) {
            string fileName = Path.GetFileName(attachment.FileName);
            mm.Attachments.Add(new Attachment(attachment.InputStream, fileName));
        }
    }
    mm.IsBodyHtml = false;
    SmtpClient smtp = new SmtpClient();
    smtp.Host = "smtp.gmail.com";
    smtp.EnableSsl = true;
    NetworkCredential NetworkCred = new NetworkCredential("username", "password");
    smtp.UseDefaultCredentials = true;
    smtp.Credentials = NetworkCred;
    smtp.Port = 587;
    smtp.Send(mm);

}

1 个答案:

答案 0 :(得分:0)

通过添加attachment.InputStream.Position = 0来解决。