2010-11-23 43 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());