微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

通过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循环中使用新对象.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐