2010-11-23 52 views
2

我有一个称为消息的MailMessage对象。c#MailMessage对象问题

我想创建message.Body但我只有一个IList来填充它。

在身体我想以下几点:

"The following files could not be converted-" + IList<string> testlist 

??????

回答

3
StringBuilder builder = new StringBuilder("The following files could not be converted-\n"); 
foreach(string s in testlist) 
    builder.AppendFormat("{0}\n", s); 

string body = builder.ToString(); 
+0

我该如何在testlist之后添加一个字符串呢? – user380432 2010-11-23 22:03:18

+0

另外我需要用逗号分开的列表是可能的吗? – user380432 2010-11-23 22:03:44

+1

如果你只想要一个CSV字符串,为什么不把你的列表转换成一个CSV字符串。例如:string.Join(“,”,new List (strings).ToArray()); – Zachary 2010-11-23 22:14:06

-1

这是徒手和未经考验的,但你的想法...

var sb = new StringBuilder(); 
sb.Append("The following files could not be converted:\n"); 
testlist.ForEach(s => sb.Append(string.Format("{0}\n", s))); 
0
var body = string.Format("The following files could not be converted-{0}.", string.Join(", ", testlist.ToArray())); 

您可以通过使用“\ n”,而不是”玩的布局, “ 例如。

0

我会将您的IList转换为CSV字符串值。 string.Join()函数应该对此有所帮助。

下面的代码是我头顶的代码,所以它可能不起作用,但希望它能给你提供想法。

IList<string> x = new List<string>(); 
x.Add("file1"); 
x.Add("file2"); 

string message = "The following files could not be converted: " + string.Join(", ", x.ToArray());