如何生成视图的pdf,请使用asp.net将pdf附加到电子邮件

时间:2019-06-30 07:26:58

标签: asp.net email pdf rotativa

我需要发送一封附有电子邮件的pdf文件,但确实如此,但是pdf文件中只有html代码,没有jquery或bootstrap代码,我也不知道为什么。

我尝试使用局部视图,但是保存了表单信息,基于该信息生成了pdf,然后将所有pdf附加到单个按钮中,这给模型带来了错误(Model.get返回null )

这是我从数据库中存储信息的角度创建视图的代码,然后是pdf和email的逻辑

db.factura.Add(factura);
db.SaveChanges();

var pdf = new ViewAsPdf2("pdf", factura); //"pdf" is the view that contains the information to fill the pdf 
byte[] pdfByteArray = pdf.GetByte(ControllerContext);

string from = ConfigurationManager.AppSettings["FromEmailID"];
string to = ConfigurationManager.AppSettings["ToEmailID"];
MailMessage message = new MailMessage(from, to);
message.Subject = "test";
message.SubjectEncoding = Encoding.UTF8;
message.Body = "Esto es un test de generacion del pdf y el envio de un archivio adjunto en el email";
message.IsBodyHtml = true;

MemoryStream file = new MemoryStream(pdfByteArray);
file.Seek(0, SeekOrigin.Begin);
Attachment data = new Attachment(file, "RunTime_Attachment.pdf", "application/pdf");
ContentDisposition disposition = data.ContentDisposition; //mime o rotativa
disposition.CreationDate = System.DateTime.Now;
disposition.ModificationDate = System.DateTime.Now;
disposition.DispositionType = DispositionTypeNames.Attachment;
message.Attachments.Add(data);

SmtpClient SmtpMail = new SmtpClient(ConfigurationManager.AppSettings["SMTP"]);
SmtpMail.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["UserName"], ConfigurationManager.AppSettings["password"]);
SmtpMail.Port = 587;
SmtpMail.EnableSsl = true;
SmtpMail.Send(message);




return RedirectToAction("Index");

pdf和电子邮件都很好发送,但这是我收到的pdf https://drive.google.com/open?id=1czQn2U0aBDYGDl7-DWsZKnslO99FxSoE 我希望pdf具有jquery和bootstrap代码

0 个答案:

没有答案