【问题标题】:Mail pdf Attachment using Rotativa使用 Rotativa 发送 pdf 附件
【发布时间】:2017-02-14 10:26:51
【问题描述】:

发送电子邮件和附件确实有效。我的问题是我在尝试发送“生成的 pdf”时收到此错误

Rotativa.dll 中出现“System.Exception”类型的异常,但未在用户代码中处理

附加信息:错误:加载页面失败http://localhost:49224/Offer/OfferPdf/4(有时它可以通过 --load-error-handling ignore 忽略此错误)

控制器中的邮件测试:

 public ActionResult MailTest()
    {



        MailMessage msg = new MailMessage();
        msg.To.Add(new MailAddress(CoEmail));
        msg.From = new MailAddress(MailFrom, UserName);
        msg.Subject = "Offer";
        msg.Body = "This is a Test";
        MemoryStream stream = new MemoryStream(OffersPdfMail (4, "Offer"));
        Attachment att1 = new Attachment(stream, "Offer.pdf", "application/pdf");
        msg.Attachments.Add(att1);
        msg.IsBodyHtml = true;
        msg.BodyEncoding = System.Text.Encoding.UTF8;
        msg.SubjectEncoding = System.Text.Encoding.Default;

        SmtpClient client = new SmtpClient();
        client.UseDefaultCredentials = false;
        client.Credentials = new System.Net.NetworkCredential(User, Pass);
        client.Port = 587; // 
        client.Host = "smtp.office365.com";
        client.DeliveryMethod = SmtpDeliveryMethod.Network;
        client.EnableSsl = true;
        try
        {
            client.Send(msg);
            return RedirectToAction("index");

        }
        catch (Exception ex)
        {
            return HttpNotFound();
        }

    }

字节[]:

 public Byte[] OfferPdfMail(int? id, string filename)
    {
  var mailpdft = new ActionAsPdf("OfferPdf/4")
        {
            FileName = "Offer",
            PageSize = Rotativa.Options.Size.A4,






            PageWidth = 210,

            PageHeight = 297

        };
        Byte[] PdfData = mailpdft.BuildPdf(ControllerContext);
        return PdfData;

最后是 ViewasPdf:

      public ActionResult OfferPdf (int? id, string filename)
    {
        string footer = "test" ;
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }




        var pdf = new ViewAsPdf("TilbudsPdf") {
            FileName = filename,
            PageSize = Rotativa.Options.Size.A4,

            PageOrientation = Rotativa.Options.Orientation.Portrait,

            PageMargins = new Rotativa.Options.Margins(12, 12, 12, 12),// it’s in millimeters

            PageWidth = 210,

            PageHeight = 297,
            CustomSwitches = footer };

        return pdf;
    }

将名称编辑为英文。可能错过了一些。 感谢您的耐心等待,抱歉英语不好。 最好的问候埃里克

【问题讨论】:

    标签: asp.net-mvc rotativa


    【解决方案1】:

    我找到了一个解决方案,这是因为我愚蠢地试图制作一个 pdf 的 pdf。所以我做了一个像这样的新 ActionResult 方法:

    public ActionResult tilbudspdfMailView(int? id, string filename)
        {
            Offer Offer= db.Offer.Find(id);
    
            return View("OfferPdf", Offer);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-22
      • 2020-08-05
      • 1970-01-01
      • 1970-01-01
      • 2011-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多