使用Mysql数据将HTML电子邮件发送到应用程序内的表中 [英] Send HTML email with Mysql data into a table from within application
问题描述
我有一个应用程序用于发送包含整个表格的电子邮件,但我不知道如何在那里获取动态数据。我确信我必须使用for循环,但我不知道如何将它写出来以获得数据行。
I have an application that i'm using to send an email that contains an entire table and i don't know how to get that dynamic data there. I'M SURE i have to use a for loop but i don't know how to word it to get the rows of data.
这是我到目前为止的内容:
Here is what i have so far:
msg.To.Add(selected);//put in custom account info
msg.Subject = "Message Subject";
msg.IsBodyHtml = true;
msg.Body = "<html>"+
"<body>"+
"<center>"+
"<h2>Your entire inventory:</h2>"+
"<table>"+
"<th>Product</th>"+
//for each loop
"<th>Quantity</th>"+
//for each loop
"</body>"+
"</html>";
让我知道您是否需要其他代码。
Let me know if you need any other code.
推荐答案
就个人而言,我会避免使用字符串连接来构建HTML。它不仅可以让你的代码笨拙,而且在编码输出时也会冒着意外的错误。一种替代方法是使用LINQ to XML来构建HTML。代码如下所示:
Personally, I would avoid building HTML using string concatenation. Not only can it make your code awkward, but you also risk accidental mistakes in encoding your output. One alternative is to use LINQ to XML to build your HTML. The code would look something like this:
var html = new XDocument(
new XElement("html",
new XElement("body",
new XElement("h2", "Your entire inventory:"),
new XElement("table",
new XElement("tr",
new XElement("th", "Product"),
from item in orders
select new XElement("td", item.ProductName)),
new XElement("tr",
new XElement("th", "Quantity"),
from item in orders
select new XElement("td", item.Quantity))))));
msg.Body = html.ToString();
这篇关于使用Mysql数据将HTML电子邮件发送到应用程序内的表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!