我正在尝试构建一个字符串并且它正在给出错误,我正在分享我的代码,请指导我
public static bool Send(string Email, string ProcessID, string Name, string Part, string MFG, string TicketTotal)
{
StringBuilder sb = new StringBuilder();
string html = @"Dear, " + Name + "<br><br>" + "We are sending the price for your reference" + "<br><br>"
+ @"<table><thead style='background-color: #006DCC;color: white;padding:10px;width:700px;'>
<tr>
<th style='background-color:#006DCC;color: white;'>
Part No
</th>
<th>
Manufacturer
</th>
</tr>
</thead>" +
@"<tbody style='background-color: #EEEEEE;'>" +
for(int i=1;i<10;i++)
{
@"<tr>
<td style='background-color: #EEEEEE;'>" +
@"<span>" + Part + "</span>" +
"</td>" +
@"<td>" + "<span>" + MFG + "</span>" + "</td>" +
@"</tr>"
} +
@"</tbody></table>" +
@"<br><br>" + "Total Amount: " + TicketTotal + "<br>" + "Process ID: " + ProcessID + "<br><br>"
+ @"For more inquiry please visit <a href='#' target='_blank'> Contact</a>"
+ @"<br><br>" + "Best Regards" + "<br><br>" + "Address: Address here" + "<br>"
+ @"Tel: Number" + "<br>" + "Fax: number" + "<br>" + "<a href='#' target='_blank'>abc</a>";
}
我想使用循环在表格中添加十行,我搜索了互联网,但无法找到任何合适的解决方案。请指导我
答案 0 :(得分:4)
在您的评论中,您提到您正在发送基于HTML的电子邮件。我也是这样做的。我发现最简单的方法是将一个HTML文件作为您的资源,并在电子邮件的特定区域添加&#34;标记&#34;在他们中。例如,我选择%USERNAME%显然是用户名。然后我可以将资源用作字符串,并为我拥有的标记执行string.Replace
。这有两个目的。一个很容易准备好。还有两个很容易编辑和预览我的HTML。这是我的代码最终的样子。
public override string GenerateEmailBody()
{
var htmlBody = System.IO.File.ReadAllText("htmlEmail\\dliHTML.html");
htmlBody = htmlBody.Replace("%AUTHOR%", Author);
htmlBody = htmlBody.Replace("%REV%", Revision);
htmlBody = htmlBody.Replace("%REPO%", Repo);
htmlBody = htmlBody.Replace("%LOG%", Log);
htmlBody = htmlBody.Replace("%DATE%", System.DateTime.Now.ToString("MMMM-dd-yyyy HH:mm:ss"));
htmlBody = htmlBody.Replace("%ADDED%",GetUList("Added",Added));
htmlBody = htmlBody.Replace("%DELETED%",GetUList("Deleted",Deleted));
htmlBody = htmlBody.Replace("%MODIFIED%",GetUList("Modified",Modified));
return htmlBody;
}
你看我确实制作了ULList。这可能有更好的方法,但这种方法并不太可怕......
private string GetUList(string header, string[] list)
{
if (list==null) return string.Empty;
if (list.Length==0) return string.Empty;
var ulist = GetHeaderString(3, header);
ulist += "<UL>";
foreach (var element in list)
{
ulist += string.Format("<li>{0}</li>", element);
}
ulist += "</UL>";
return ulist;
}
修改强>
这里是&#34; Get header String&#34;我忘记添加的方法
private string GetHeaderString(int size, string message)
{
return string.Format("<H{0}>{1}</H{0}>" ,size, message);
}
答案 1 :(得分:1)
正如其他人所说,这是完全做错的错误方式。
但是要通过解释为什么来向你推进正确的方向,这不起作用,请参阅下文。
您正在尝试将循环连接到字符串。你不能这样做:
@"<tbody style='background-color: #EEEEEE;'>" + // this plus sign is the problem
for(int i=1;i<10;i++)
{
@"<tr> //etc.
您需要添加循环内的字符串:
string html = @"Dear, " + Name + "<br><br>" + "We are sending the price for your reference" + "<br><br>"
+ @"<table><thead style='background-color: #006DCC;color: white;padding:10px;width:700px;'>
<tr>
<th style='background-color:#006DCC;color: white;'>
Part No
</th>
<th>
Manufacturer
</th>
</tr>
</thead>" +
@"<tbody style='background-color: #EEEEEE;'>"; \\terminate the line.
//start the loop
for(int i=1;i<10;i++)
{
html += @"<tr>
<td style='background-color: #EEEEEE;'>" +
@"<span>" + Part + "</span>" +
"</td>" +
@"<td>" + "<span>" + MFG + "</span>" + "</td>" +
@"</tr>"; // not plus
}
html += @"</tbody></table>" +
@"<br><br>" + "Total Amount: " + TicketTotal + "<br>" + "Process ID: " + ProcessID + "<br><br>"
+ @"For more inquiry please visit <a href='#' target='_blank'> Contact</a>"
+ @"<br><br>" + "Best Regards" + "<br><br>" + "Address: Address here" + "<br>"
+ @"Tel: Number" + "<br>" + "Fax: number" + "<br>" + "<a href='#' target='_blank'>abc</a>";