2012-08-02 58 views
4

我正在開發一個具有郵件發送選項的桌面應用程序。我有以下的代碼到和它的作品完美只有1個收件人:通過Interop庫向多個收件人發送郵件C#

DialogResult status; 
status = MessageBox.Show("Some message", "Info", MessageBoxButtons.OKCancel, MessageBoxIcon.Information); 
if (status == DialogResult.OK) 
{ 
    try 
    { 
     // Create the Outlook application. 
     Outlook.Application oApp = new Outlook.Application(); 
     // Create a new mail item. 
     Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem); 

     // Set HTMLBody. 
     //add the body of the email 
     oMsg.HTMLBody = "<html>" + 
       "<body>" + 
       "some html text" + 
       "</body>" + 
       "</html>"; 

     int iPosition = (int)oMsg.Body.Length + 1; 
     //Subject line 
     oMsg.Subject = txt_mailKonu.Text; 
     oMsg.Importance = Outlook.OlImportance.olImportanceHigh; 
     // Recipient 
     Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients;  
     //Following line causes the problem 
     Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add(senderForm.getRecipientList().ToString()); 
     oRecip.Resolve(); 
     //oRecip.Resolve(); 
     // Send. 
     oMsg.Send(); 
     // Clean up. 
     oRecip = null; 
     oRecips = null; 
     oMsg = null; 
     oApp = null; 
     MessageBox.Show("Successful", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information); 
    } 
    catch (Exception) 
    { 
     MessageBox.Show("Failed", "Eror", MessageBoxButtons.OK, MessageBoxIcon.Error); 
    }     
} 

我得到錯誤的粗線在那裏我在下面的模式添加多個收件人: [email protected]。 COM; [email protected]

它適用於1個地址,但當我得到多個地址分開時拋出COM異常 - Outlook無法解析一個或多個名稱。

希望你能幫助我。

+1

是否有任何理由不能使用'System.Net.Mail'? – mgnoonan 2012-08-02 12:06:03

+0

我需要標記和設置郵件的重要性級別,所以我選擇它,System.Net.Mail提供這些嗎? – neocorp 2012-08-02 13:45:06

+0

優先,是的。標記,不。 http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.aspx – mgnoonan 2012-08-02 14:36:22

回答

2

您是否嘗試將多個收件人添加到oMsg.Recipients

// I assume that senderForm.getRecipientList() returns List<String> 
foreach(String recipient in senderForm.getRecipientList()) 
{ 
    Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add(recipient); 
    oRecip.Resolve(); 
} 

如果需要的話,你可能會爆炸senderForm.getRecipientList().ToString()

String [] rcpts = senderForm.getRecipientList().ToString().Split(new string[] { "; " }, StringSplitOptions.None); 

foreach循環使用的新對象。

+1

非常感謝你解決它! – neocorp 2012-08-03 07:06:14

相關問題