2013-10-29 42 views
-2

這是我正在嘗試使用的代碼。但它實際上並沒有按照我的意願迭代數組來填充正文。我想要做的是使用C#創建一個Outlook電子郵件,並填充接收者,郵件主題,然後根據數組中包含的內容生成電子郵件的正文。編輯 - 我移動了每個循環,試圖用數組中的每個元素填充正文,但是我得到了一個編譯錯誤,無法使用此代碼將int轉換爲字符串。Outlook主體的迭代陣列

 public static string GenerateEmail() 
{ 
    try 
    { 
     for (int q = eName.GetLowerBound(0); q <= eName.GetUpperBound(0); q++) 
     { 
      return Global.Variables.GlobalVariables.eName[q]; 
      Outlook.Application oApp = new Outlook.Application(); 
      Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem); 
      for (int q = eName.GetLowerBound(0); q <= eName.GetUpperBound(0); q++) 
      { 
       oMsg.HTMLBody = q; 
      } 
      oMsg.Subject = "Reports Are Ready"; 
      Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients; 
      Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add("[email protected]"); 
      oRecip.Resolve(); 
      oMsg.Save(); 
      oRecip = null; 
      oRecips = null; 
      oMsg = null; 
      oApp = null; 
     } 
    } 
    catch 
    { 
    } 
    return null; 
    }   
} 
+3

返回循環中的第一個語句,空catch ???你想做什麼 ? – Habib

+2

很多時候,一個好的舊步調試會話會節省您很多的時間和精力... –

+0

標題編輯,你有C#標記,它不需要在標題中。 –

回答

2

這個問題似乎是在for循環的第一行是一個return語句,這將導致功能的直接abbortion。

如果要填充消息正文而不是爲每個interation創建一條消息,請將實際電子郵件的聲明移至循環之外。然後在循環內附加內容到消息:

Outlook.Application oApp = new Outlook.Application(); 
Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem); 
string content = string.Empty; 

for (int q = eName.GetLowerBound(0); q <= eName.GetUpperBound(0); q++) 
{ 
    content += "..."; 
} 

oMsg.HTMLBody = content; 
// additional settings 
+0

刪除返回行(您建議的第一行代碼)現在爲數組的每個元素創建一封新電子郵件,而不是用每個元素填充1封電子郵件的正文。 – user2676140

+0

@ user2676140:我用一些代碼更新了我的答案。 – nuke