邮件pdf使用Rotativa的附件

时间:2017-02-14 10:26:52

标签: asp.net-mvc rotativa

发送电子邮件和附件actaly有效。我的问题是我在尝试发送“生成的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;
    }

将这些名字编辑成英文。可能错过了一些。 谢谢你的耐心,对不好的英语感到抱歉。 最好的问候埃里克

1 个答案:

答案 0 :(得分:0)

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

public ActionResult tilbudspdfMailView(int? id, string filename)
    {
        Offer Offer= db.Offer.Find(id);

        return View("OfferPdf", Offer);
}