简体   繁体   English

在Asp.Net MVC Razor中将HTML视图作为电子邮件附件发送

[英]Send HTML view as an Email Attachment in Asp.Net MVC Razor

I'm currently working in Razor ASP.Net MVC . 我目前在Razor ASP.Net MVC Here I have a HTML view , named as Customer Payment , which is generated on the basis of some calculations, in RAZOR , and shown in a HTML dialog . 在这里,我有一个名为“ 客户付款”HTML view ,该HTML view是根据一些计算在RAZOR ,并显示在HTML dialog

我想通过电子邮件发送此视图

I want to send this Html View as an Email attachment . 我想将此Html View作为电子邮件附件发送 But here I'm getting confused rather I have to transfer this HTML View into some .PDF or .jpg in jQuery and then send it to Controller side, or is there some more efficient way exists to do this. 但是在这里,我感到困惑,我不得不将这个HTML View转换为jQuery .PDF or .jpg ,然后将其发送到Controller端,或者是否存在一些更有效的方法来做到这一点。

Still I have coded to send simple email, without any attachment, in C# ,but got stuck at this point. 我仍然使用C# coded了发送没有任何附件的简单电子邮件的coded ,但此刻陷入困境。

Best suggestions will be highly appreciated. 最好的建议将不胜感激。 :-) :-)

As normal mails, you have to send the body of the mail content like below 作为普通邮件,您必须发送如下邮件内容的正文

Your c# code. 您的C#代码。

string _FilePath = HttpContext.Server.MapPath("Activation.txt");
StreamReader sr = new StreamReader(_FilePath);
string body = sr.ReadToEnd();
sr.Close();
sr.Dispose();

send the above body in the body of an email 在电子邮件正文中发送上述正文

your Activation.txt file code will be a template like below just body tag 您的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>&nbsp;&nbsp;&nbsp;<img src="http://img/phone.png">&nbsp;&nbsp;<span>987654321</span>&nbsp;&nbsp;<img src="http://img/email.jpg">&nbsp;&nbsp;<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>

        &nbsp;&nbsp;
        <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>

You can also dynamically add/replace the data to be sent in email Add below code in backend 您还可以动态添加/替换要通过电子邮件发送的数据,在后端添加以下代码

  body = body.Replace("[HLink]", YOUR-DATA);

first Convert your view to Pdf using Rotativa and Then email it as an attachment. 首先使用Rotativa将视图转换为Pdf,然后通过电子邮件将其作为附件发送。

this is your method to Convert the view to 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)
        {
        }

    }

Method to send Email. 发送电子邮件的方法。

 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);
}

To convert your View to Pdf Follow this Link 要将视图转换为Pdf,请点击此链接

Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM