【问题标题】:failed to add an attachment in MimeMessage (rotativa) for sending Mail未能在 MimeMessage (rotativa) 中添加附件以发送邮件
【发布时间】:2020-10-27 16:25:39
【问题描述】:

我想通过电子邮件发送(查看页面)pdf。但是当我尝试添加附件时,我在“添加”下方发现错误。为此,我无法通过附加我的视图 pdf 成功发送邮件。 这是我的代码:

(Ordercontroller)

//other code


            var message = new MimeMessage();
            message.From.Add(new MailboxAddress("Test Project", "pt300@gmail.com"));
            message.To.Add(new MailboxAddress("psm", "p689@gmail.com"));
            message.Subject = "Hi,this is demo email";
            message.Body = new TextPart("plain")
            {
                Text = "Hello,My First Demo Mail it is.Thanks",
            };

            //add attach
            var aa = new ViewAsPdf("Cart")
            {
                FileName = "Invoice.pdf",          //actually, I don't know why this filename is 
                                                    // "Invoice". I found this system on a website.

                PageOrientation = Rotativa.AspNetCore.Options.Orientation.Portrait,
            };
         
            message.Attachments.Add(aa);

            //end attach
            using (var client = new SmtpClient())
            {
                client.Connect("smtp.gmail.com", 587, false);
                client.Authenticate("pt300@gmail.com", "MyPassword");

                client.Send(message);
                client.Disconnect(true);

            }

//other code

我尝试将这些控制器的视图转换成 pdf 并通过邮件发送:

(HomeController)

  public ActionResult Cart()
        {
            List<Product> products = HttpContext.Session.Get<List<Product>>("products");
            if (products == null)
            {
                products = new List<Product>();
            }
            return View(products);
        }

在 OrderController 中我发现了一个错误。

我将如何解决这个问题并通过邮件成功发送我的视图 pdf。请帮助。

【问题讨论】:

  • @SirRufo 这里的解决方案只使用了附件。但我想以 pdf 格式发送邮件 cart.cshtml。
  • 如果你考虑var aa = new ViewAsPdf("Cart")这一行,这里我要cart.cshtml pdf格式,然后通过邮件发送。但是在我发布的输出中“添加”时出现问题。

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


【解决方案1】:

Attachments 是一个 MimeMessage 属性,没有这样的方法Add strong>,所以这里出现了错误。要添加附件,您应该在之前创建一个BodyBuilder 对象,使用此类您将能够添加新附件(每个附件都是字节数组),使用 Attachments 属性及其 Add 方法,但始终与 BodyBuilder 相关,而不与 MimeMessage .

请看一下这里给出的答案,我猜这就是您要寻找的: .net Core Mailkit send attachement from array

或者在这里查看另一个示例: https://www.taithienbo.com/send-email-with-attachments-using-mailkit-for-net-core/

此外,您可以使用以下代码将 Rotativa PDF 作为字节数组获取:

var rotativaAction = new ViewAsPdf("Cart")
{
    FileName = "Invoice.pdf",   // You can change this file name to set whatever you want
    PageOrientation = Rotativa.AspNetCore.Options.Orientation.Portrait,
};

byte[] fileAttachmetByteArray = rotativaAction.BuildFile(ControllerContext);

一旦您使用 Rotativa 将 PDF 生成为字节数组,猜测您已将结果存储在名为 fileAttachmentByteArray 的变量中(如前面的代码示例中所述),您必须简单地发送此变量(字节数组)到BodyBuilderAdd方法中,作为第二个参数(第一个是附件文件名,即完全免费使用你喜欢的名字),然后设置Mime Message Body,使用 BodyBuilder您曾合作过。

所以,分步解释这个过程:

  1. 在第一行,您创建一个 BodyBuilder 变量,并使用您想要的文本作为电子邮件正文对其进行初始化。
  2. 然后,您调用 Add 方法来添加一个新附件,将您希望的文件名和您之前创建的 bye 数组发送给它
  3. 最后,将值分配给 Mime 消息的 Body 属性

你的代码应该是这样的:

var builder = new BodyBuilder { HtmlBody = "yourBodyMessage" };   // Change "yourBodyMessage" to the email body you desire
builder.Attachments.Add("filename", fileAttachmentByteArray);   // Once again, you can change this "filename" to set whatever you want
mimeMessage.Body = builder.ToMessageBody();   // Assuming mimeMessage is the same variable you provided in your code

【讨论】:

  • 现在我明白了,我必须按照你最后的代码添加附件。如果您更新代码以使用字节数组添加此附件,我将不胜感激。
  • 嗨@Shane Watson。也许我以错误的顺序发布了信息,我将编辑答案。请检查这是否是您所要求的流程是否清晰。谢谢!
猜你喜欢
  • 2019-04-30
  • 1970-01-01
  • 2013-12-14
  • 2017-04-08
  • 2018-02-02
  • 2015-09-24
  • 2018-12-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多