繁体   English   中英

无法通过C#从Outlook发送邮件

[英]Can't send mail from Outlook via C#

我正在尝试向同事发送邮件,但是在收到第一封邮件后,应用程序崩溃了。 我收到此消息

用户代码未处理COMException。 “该项目已被移动或删除。”

private void SendMail()
{
    var usersEmailAddresses = Factory.Users.List(); // .List() lists all the data from the Users table.

    Application OutlookApplication = new Application();
    MailItem OutlookMail = (MailItem)OutlookApplication.CreateItem(OlItemType.olMailItem);

    OutlookMail.Subject = @"TEST/Ushqimi i caktuar per sot";

    MailBody //region, here I assign the MailBody Text.

    for (int index = 0; index < usersEmailAddresses.Count; index++)
    {
        OutlookMail.To = usersEmailAddresses[index].Email;  //Here is where I get the exception, AFTER trying to assign the second email.

        if (usersEmailAddresses[index].RecieveEmail && !usersEmailAddresses[index].IsOnVacation)
        {
            ((_MailItem)OutlookMail).Send();
        }
    }
}

我究竟做错了什么? 有什么建议么?

MailItem OutlookMail = (MailItem)OutlookApplication.CreateItem(OlItemType.olMailItem);

您正在循环外创建mail item 它需要在循环内创建,因为一旦发送OutlookMail它便不再存在,因此您需要一个新的邮件对象。

我认为错误消息“该项目已被移动或删除”告诉您邮件已经发送,因此您不应更改其“收件人”并再次发送。 请尝试在for循环内创建MailItem,或在发送邮件之前将所有地址添加到邮件项目中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM