【问题标题】:Send Email with attached Pdf problem in asp.net mvc 5在 asp.net mvc 5 中发送带有附件 Pdf 问题的电子邮件
【发布时间】:2018-11-11 04:21:47
【问题描述】:

我正在尝试发送附有 pdf 的电子邮件,我的 pdf 是字节数组。当我尝试发送邮件(没有 pdf)时,它会显示

消息 = SMTP 服务器需要安全连接或客户端未通过身份验证。服务器响应为:5.5.1 需要身份验证。了解更多信息,请访问

我的代码是`string senderEmail = System.Configuration.ConfigurationManager.AppSettings["SenderEmail"].ToString(); string senderPassword = System.Configuration.ConfigurationManager.AppSettings["SenderPassword"].ToString();

            SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
            client.EnableSsl = true;
            client.Timeout = 100000;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.UseDefaultCredentials = false;
            client.Credentials = new NetworkCredential(senderEmail, senderPassword);

            MailMessage mailMassege = new MailMessage(senderEmail, toEmail, subject, body);
            mailMassege.IsBodyHtml = true;
            mailMassege.BodyEncoding = Encoding.UTF8;


            client.Send(mailMassege);`

我的 pdf 在字节数组中 byte[] applicationPDFData = actionResult.BuildPdf(ControllerContext); System.IO.File.WriteAllBytes(filePath + "/hello.pdf", applicationPDFData);

我想用这个 pdf 发送邮件。 提前谢谢

【问题讨论】:

标签: c# asp.net asp.net-mvc smtp


【解决方案1】:

您必须创建一个附件并将其附加到邮件附件列表中。 这是一个例子:

byte[] applicationPDFData = actionResult.BuildPdf(ControllerContext);
Attachment attPDF = new Attachment(new MemoryStream(applicationPDFData), name);

EmailMessage emailMessage = new EmailMessage();
emailMessage.To.Add( new EmailRecipient( toEmail ) );
emailMessage.Subject = subject;
emailMessage.Body = body;
emailMessage.Attachments.Add( attPDF );

应届毕业生 gy

【讨论】:

    猜你喜欢
    • 2011-10-09
    • 1970-01-01
    • 2018-06-13
    • 1970-01-01
    • 2020-11-20
    • 2014-02-28
    • 1970-01-01
    • 2014-05-29
    相关资源
    最近更新 更多