2017-06-22 49 views
0

我目前在Razor ASP.Net MVC工作。這裏我有一個HTML view,命名爲客戶付款,它是根據一些計算生成的,在RAZOR中顯示,並顯示在HTML dialog中。發送HTML視圖作爲電子郵件附件在Asp.Net MVC剃鬚刀

I want to email this View

我想給這個Html View作爲電子郵件附件。但在這裏,我感到困惑,我必須將HTML View轉換成的jQuery,然後發送給Controller一方,或者是否存在一些更有效的方法來執行此操作。

我還有coded發送簡單的電子郵件,沒有任何附件,在C#,但卡住了這一點。

最好的建議將不勝感激。 :-)

+1

[如何呈現PDF格式的ASP.NET MVC視圖]可能的副本(https://stackoverflow.com/questions/1324597/how-to-render-an-asp-net-mvc-view-in -pdf格式) –

回答

1

由於正常的郵件,你必須發送郵件的正文內容像下面

你的C#代碼。

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

發送上述體在電子郵件的正文

您Activation.txt文件中的代碼將是一個模板,如下面只是身體標記

<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;">[email protected]</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> 

您還可以動態地添加/替換要在電子郵件中發送的數據 在後端添加以下代碼

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

但是我們爲什麼要這樣做!相反,我們將視圖轉換爲'jQuery'中的'.pdf'並將其傳遞給Controller,以便將其作爲電子郵件附件發送。這不是簡單的方法! –

+1

如果我們發送了css樣式將不會應用於電子郵件正文中,則所有內容都應爲內嵌樣式。和更多的電子郵件正文應在身體標記,因爲您的視圖可能只有div標記,更多的所有電子郵件模板應保存在單獨的部分 –

+0

檢查我編輯的答案,如果你想動態添加任何東西 –

0

首先使用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 Follow this Link

希望它能幫助!

相關問題