【发布时间】:2016-03-22 15:55:46
【问题描述】:
我想通过电子邮件发送一些车辆信息。 我的所有代码都在工作,但间隔信息是个问题。每辆车都有一个清单,然后该清单会通过电子邮件发送。所以我遍历列表并获取缺陷和评论。
foreach (var item in chkList.CheckItems)
{
if (item.Defect == true)
{
defect += string.Format("{0,-40} {1}\n", item.ItemTitle, item.Comment);
}
}
if (hasDefect == true)
{
Utils.ChecklistSendMail("Checklist", ToAddresses.Split(';'),
"Vehicle Reg: " + reg + "\n" +
"Checklist No: " + chkList.CheckListNo + "\n"+
"Date: " + ChecklistDate.ToShortDateString() + "\n" +
"Defects: Comments: " + "\n" +
defect);
}
电子邮件如下所示:
Vehicle Reg: XLZ 8194
Checklist No: 0
Date: 22/03/2016
Defects: Comments:
Vehicle Secure comment1
Brakes comment2
我希望缺陷和 cmets 显示如下:
Defects: Comments:
Vehicle Secure comment1
Brakes comment2
由于Vehicle Secure 比Brakes 长,评论被推得更远。但是,无论第一个单词有多长,有没有办法将字符串固定在某个位置?
编辑
代码现在看起来像这样:
string defect = "";
string comment = "";
string aheading = "Defects:";
string bheading = "Comments:";
foreach (var item in chkList.CheckItems)
{
if (item.Defect == true)
{
defect += item.ItemTitle;
comment += item.Comment;
}
}
string result = aheading.PadRight(20, ' ') + bheading.PadRight(20, ' ') + Environment.NewLine +
defect.PadRight(20, ' ') + comment.PadRight(20, ' ') + Environment.NewLine;
但输出看起来像这样:
Defects: Comments:
Vehicle SecureBrakestest1test2
【问题讨论】:
-
您可以使用标签:
"\t",但在您的电子邮件中使用 html 会更好。 -
我认为以 html 格式发送电子邮件是最好的解决方案
-
@user1666620 我将如何在这里使用 html?
-
@user123456789 当然。您的邮件正文可以是纯 HTML
-
@user123456789 stackoverflow.com/questions/8628683/…