通过Interop库C#向多个收件人发送邮件

我正在开发一个具有邮件发送选项的桌面应用程序.我有以下代码,它只适用于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",MessageBoxButtons.OK,MessageBoxIcon.information);
    }
    catch (Exception)
    {
        MessageBox.Show("Failed","Eror",MessageBoxIcon.Error);
    }                
}

我在粗线处得到错误,我在以下模式中添加多个收件人:
john.harper@abcd.com; adam.smith@abcd.com

它适用于1个地址,但是当我将多个地址分开时,它会抛出COM异常 – Outlook无法解析一个或多个名称.

希望你能帮助我.

解决方法

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

相关文章

目录简介使用JS互操作使用ClipLazor库创建项目使用方法简单测...
目录简介快速入门安装 NuGet 包实体类User数据库类DbFactory...
本文实现一个简单的配置类,原理比较简单,适用于一些小型项...
C#中Description特性主要用于枚举和属性,方法比较简单,记录...
[TOC] # 原理简介 本文参考[C#/WPF/WinForm/程序实现软件开机...
目录简介获取 HTML 文档解析 HTML 文档测试补充:使用 CSS 选...