2017-07-14 41 views
0

我正在使用“System.Net.Mail”在我的代码中发送电子邮件。它允许电子邮件正文以HTML格式发送。我需要使用我的模型和bootstrap中的数据自定义和装饰我的电子邮件。使用HTML和模型从MVC定制电子邮件页面

所以,我在这里尝试创建HTML页面并尝试将其与Model中的数据集成,然后将HTML页面作为字符串传递给电子邮件对象的“主体”。

这是否有意义或有任何其他方式可以做到这一点?我想构建一个HTML表格,它根据模型中的数据动态地获取行。

+0

这会有帮助吗? https://stackoverflow.com/a/33124638/1658906 http://mehdi.me/generating-html-emails-with-razorengine-introduction/ http://mehdi.me/generating-html-emails-with-razorengine -basic-generating-your-first-email/ – juunas

+0

这个库被证明是过时的:(没用的。 – Ponni

回答

0

我一直使用这个工具http://aboutcode.net/postal。试一下,它会为你节省很多编码。

+0

你有没有一个可行的例子?不能得到这个工作! – Ponni

+0

通过网站,很容易指南。 –

0

下面是一个例子。我甚至包含了一些CSS来展示你如何设计你的电子邮件。 “项目”是我循环创建网格的模型。

CSS支持在大多数电子邮件客户端中受到限制,因此您可能无法使Bootstrap正常工作,但可以尝试。如果没有,您仍然可以完成类似Bootstrap的响应式设计。下面的例子保持中心和100%的宽度,因为页面调整大小。

var projects = db.Projects.Select(p => new { p.Name, p.Description }); 

var grid = new StringBuilder(); 

foreach (var project in projects) 
{ 
    grid.Append([email protected]"<tr'> 
      <td>{project.Name}</td> 
      <td>{project.Description}</td> 
     </tr>"); 
} 

var body = [email protected]" 
    <style> 
     body {{ color: #444 }} 
     h2 {{ margin-top: 0; margin-bottom: 0 }} 
     .container {{ text-align: center }} 
     table {{ margin-left: auto; margin-right: auto; font-size: 20; width: 100% }} 
     .heading {{ font-weight: bold; background-color: #f1f4ff }} 
     td {{ padding-top: 8px; padding-bottom: 8px; padding-left: 16px; padding-right: 16px }} 
    </style> 

    <div class='container'> 
     <h2>Hello from Mail</h2> 

     <table class='grid' cellpadding='0' cellspacing='0'> 
      <tr class='heading'> 
       <td>Project Name</td> 
       <td>Project Description</td> 
      </tr> 
      {grid} 
     </table> 
    </div> 
"; 

client.Send(mail);