简体   繁体   English

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

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

I'm trying to send mails to my co workers, but after the 1st mail, the application crashes. 我正在尝试向同事发送邮件,但是在收到第一封邮件后,应用程序崩溃了。 I get this message 我收到此消息

COMException was unhandled by user code. 用户代码未处理COMException。 "The item has been moved or deleted." “该项目已被移动或删除。”

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

What Am I doing wrong? 我究竟做错了什么? Any suggestions? 有什么建议么?

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

You are creating the mail item outside of the loop. 您正在循环外创建mail item It needs to be created inside the loop because once you send OutlookMail it fails to exist anymore so you need a new mail object. 它需要在循环内创建,因为一旦发送OutlookMail它便不再存在,因此您需要一个新的邮件对象。

I think the error message “The item has been moved or deleted” is telling you the mail is already sent, so you should not change its "To" and send again. 我认为错误消息“该项目已被移动或删除”告诉您邮件已经发送,因此您不应更改其“收件人”并再次发送。 Please try to create the MailItem inside for-loop, or add all the address into mail item before sending the mail. 请尝试在for循环内创建MailItem,或在发送邮件之前将所有地址添加到邮件项目中。

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

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