Javascript 在Asp.NETMVC中以电子邮件附件形式发送HTML视图
我目前在ASP.NETMVC中工作。这里我有一个Javascript 在Asp.NETMVC中以电子邮件附件形式发送HTML视图,javascript,c#,jquery,asp.net-mvc,razor,Javascript,C#,Jquery,Asp.net Mvc,Razor,我目前在ASP.NETMVC中工作。这里我有一个HTML视图,名为客户付款,它是在RAZOR中根据一些计算生成的,并显示在HTML对话框中 我想将此Html视图作为电子邮件附件发送。但在这里我感到困惑,我必须将这个HTML视图转换成jQuery中的一些.PDF或.jpg,然后将其发送到控制器端,或者是否存在更有效的方法来实现这一点 我仍然在C#中编写了code来发送简单的电子邮件,没有任何附件,但在这一点上被卡住了 最好的建议将不胜感激。:-) 与普通邮件一样,您必须发送邮件正文内容,如下所示
HTML视图
,名为客户付款,它是在RAZOR
中根据一些计算生成的,并显示在HTML对话框
中
我想将此Html视图
作为电子邮件附件发送。但在这里我感到困惑,我必须将这个HTML视图
转换成jQuery
中的一些.PDF或.jpg
,然后将其发送到控制器
端,或者是否存在更有效的方法来实现这一点
我仍然在C#
中编写了code
来发送简单的电子邮件,没有任何附件,但在这一点上被卡住了
最好的建议将不胜感激。:-) 与普通邮件一样,您必须发送邮件正文内容,如下所示 你的c代码 在电子邮件正文中发送上述正文 你的Activation.txt文件代码将是一个模板,如下面的body标签所示
<body style="margin: 1% 10%;">
<table style="position:relative;background-color: whitesmoke;padding-bottom: 20px;text-align: center;width: 100%;">
<tbody class="">
<tr class="">
<td style="position:relative;top:20px;padding-left: 25px;padding-right: 25px;margin-right: auto;margin-left: auto;" width="400" align="center" bgcolor="whiteSmoke">
<img src="[HLINK]" height="70" width="150" alt="image cannot displayed ">
</td>
</tr>
<tr>
<td width="400" align="center" bgcolor="whiteSmoke"></td>
</tr>
<tr>
<td align="center" style="position: relative; top: 11px; padding-left: 25px; padding-right: 25px; margin-right: auto; margin-left: auto;" width="470" bgcolor="whitesmoke">
<h2 style="
color:#f44336;
font-family: WeblySleek UI,Segoe UI,Helvetica Neue,Arial,sans-serif">
Hey thanks for joining<h2 /> <h4 style="font-family: WeblySleek UI,Segoe UI,Helvetica Neue,Arial,sans-serif;">Welcome to Portal! Before you get started, please verify your email address below </h4>
</td>
</tr>
<tr bgcolor="whitesmoke" style="position:relative;top:9px;">
<td align="center" style="position:relative;top:-4px;padding-left: 25px;padding-right: 25px;margin-right: auto;margin-left: auto;">
<a style="position:relative;display: block;width:150px;height: 20px;text-align:center; text-decoration:none;background:#f44336;padding:10px;border-radius: 5px;color: white;font-family: WeblySleek UI,Segoe UI,Helvetica Neue,Arial,sans-serif;" href="[HLink]">Verify Email Address</a>
</td>
</tr>
<tr>
<td style="position:relative;top:3px;padding-left: 25px;padding-right: 25px;margin-right: auto;margin-left: auto;" bgcolor="whitesmoke" align="center" width="400">
<span style="padding-left:0.3cm;font-family: WeblySleek UI,Segoe UI,Helvetica Neue,Arial,sans-serif;">Thank you for visiting Us. It is the ultimate gift of choice.You can send to a friend or loved one with a selection of brands to pick from in various categories such as fashion, beauty, sports, food, entertainment etc. <span>
</td>
</tr>
<tr>
<td style="position:relative;padding-left: 25px;padding-right: 25px;margin-right: auto;margin-left: auto;" bgcolor="whitesmoke">
<h2 style="color:#f44336;font-family: WeblySleek UI,Segoe UI,Helvetica Neue,Arial,sans-serif;" align="center">Our Most Celebrated Brands</h2>
</tr>
<tr>
<td style="position:relative;top:-2px;padding-left: 25px;padding-right: 25px;margin-right: auto;margin-left: auto;" align="center" bgcolor="whitesmoke" width="400" style="padding:10px;">
<b style="font-family: WeblySleek UI,Segoe UI,Helvetica Neue,Arial,sans-serif;">Contact Us:</b> <img src="http://img/phone.png"> <span>987654321</span> <img src="http://img/email.jpg"> <span styl="font-family: WeblySleek UI,Segoe UI,Helvetica Neue,Arial,sans-serif;">help@help.com</span>
</td>
</tr>
<tr>
<td style="background-color:whitesmoke;position:relative;text-align: center;">
<span>
<a style="position:relative;padding:10px 30px;text-align:center; text-decoration:none;background:#4e69a2;border-radius: 5px;color: white;font-family: WeblySleek UI,Segoe UI,Helvetica Neue,Arial,sans-serif;" href="#">Facebook</a>
<a style="position:relative;padding:10px 30px;text-align:center;text-decoration:none;background:#c32f10;border-radius: 5px;color: white;font-family: WeblySleek UI,Segoe UI,Helvetica Neue,Arial,sans-serif;" href="#">Google</a>
</span>
</td>
</tr>
</tbody>
</table>
</body>
首先使用Rotativa将视图转换为Pdf,然后将其作为附件发送电子邮件 这是将视图转换为PDF的方法
public void SenRequestByEmail()
{
string ReqId="CT001";
try
{
using (var stream = new MemoryStream())
{
//methodto bind details to your model
var res = dealerService.BindReqToPrint(ReqId);
//_PrintActivationRequest is your View name here and res is the object of the model you are using at your view
ViewAsPdf pdf = new ViewAsPdf("_PrintActivationRequest", res) { FileName = "DeviceActivationRequest.pdf" };
//convert the pdf into array of bytes
byte[] bytes = pdf.BuildPdf(this.ControllerContext);
string fileName = string.Format("DeviceActivationRequest_{0}.pdf", DateTime.Now.ToString("dd_MMM_yy_hh:mm"));
SendDeviceActivationRequest(new MemoryStream(bytes), fileName );
}
}
catch (Exception ex)
{
}
}
方法发送电子邮件
public void SendDeviceActivationRequest(Stream ms, string fileName )
{
//Here write your code to send Email and attach the pdf file as shown in below code.
MailMessage mail = new MailMessage();
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment(ms,fileName , "application/pdf");
mail.Attachments.Add(attachment);
}
将视图转换为Pdf的步骤
希望有帮助 但我们为什么要这么做!相反,我们将视图转换为
jQuery
中的一些.pdf
,并将其传递给控制器,以便将其作为电子邮件附件发送。这不是一个简单的方法吗!css样式将不适用于电子邮件正文,如果我们发送,一切都应该在内联样式。更多的邮件正文应该放在body标签中,因为您的视图可能只有div标签,更多的邮件模板应该放在单独的部分中检查我编辑的答案,如果您想动态添加任何内容,请点击@Supraj。我采用了其他一些有效的方法来解决这个问题。我很快会在我的答案中上传它。非常感谢。可能是重复的
public void SenRequestByEmail()
{
string ReqId="CT001";
try
{
using (var stream = new MemoryStream())
{
//methodto bind details to your model
var res = dealerService.BindReqToPrint(ReqId);
//_PrintActivationRequest is your View name here and res is the object of the model you are using at your view
ViewAsPdf pdf = new ViewAsPdf("_PrintActivationRequest", res) { FileName = "DeviceActivationRequest.pdf" };
//convert the pdf into array of bytes
byte[] bytes = pdf.BuildPdf(this.ControllerContext);
string fileName = string.Format("DeviceActivationRequest_{0}.pdf", DateTime.Now.ToString("dd_MMM_yy_hh:mm"));
SendDeviceActivationRequest(new MemoryStream(bytes), fileName );
}
}
catch (Exception ex)
{
}
}
public void SendDeviceActivationRequest(Stream ms, string fileName )
{
//Here write your code to send Email and attach the pdf file as shown in below code.
MailMessage mail = new MailMessage();
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment(ms,fileName , "application/pdf");
mail.Attachments.Add(attachment);
}