【问题标题】:Sending mail to multiple recipients via Interop library C#通过互操作库 C# 向多个收件人发送邮件
【发布时间】:2012-07-31 09:22:47
【问题描述】:

我正在开发一个具有邮件发送选项的桌面应用程序。我有以下代码,它仅适用于 1 个收件人:

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);
    }                
}

我在以以下模式添加多个收件人的粗体行出现错误: john.harper@abcd.com; adam.smith@abcd.com

它适用于 1 个地址,但是当我将多个地址分开时,它会引发 COM 异常 - Outlook 无法解析一个或多个名称。

希望你能帮我解决这个问题。

【问题讨论】:

  • 有什么理由不能使用System.Net.Mail
  • 我需要在邮件中标记和设置重要性级别,所以我选择了它,System.Net.Mail 提供这些吗?
  • 优先级,是的。标记,没有。 msdn.microsoft.com/en-us/library/…

标签: c# visual-studio-2010 office-interop outlook-2010


【解决方案1】:

您是否尝试将多个收件人添加到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 循环中使用新对象。

【讨论】:

    猜你喜欢
    • 2020-09-15
    • 2013-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-10
    • 2016-11-03
    相关资源
    最近更新 更多