使用outlook MailItem in循环发送多封电子邮件

时间:2011-12-04 12:41:40

标签: c# outlook vsto

您好我正在开发一个Outlook附加组件,作为工作流程的一部分,它应该采用mailItem主体和主题,并且对于每个收件人,它应该根据收件人电子邮件更改邮件正文。

问题是它只发送第一封电子邮件,而Send();之后它不会将电子邮件发送给其他收件人

 Outlook.Application application = Globals.ThisAddIn.Application;
        Outlook.Inspector inspector = application.ActiveInspector();
        Outlook.MailItem myMailItem = (Outlook.MailItem)inspector.CurrentItem;
        myMailItem.Save();



        if (myMailItem != null)
        {

            myMailItem.Save();
            PorceesData(myMailItem);
        }

           ..
           ..
           ..
           ..
   private void ProcessData(MailItem oMailItem)
      {

Recipients recipients = oMailItem.Recipients;

string Body = oMailItem.Body;
string To = oMailItem.To;
string CC = oMailItem.CC;
string bcc = oMailItem.BCC;

foreach (Recipient r in recipients)
{
   if (r.Resolve() == true)
    {

        string msg = "Hello open the attached file (msg.html); 
        string address = r.Address;
        oMailItem.Body = msg;
         oMailItem.To = address;
         oMailItem.Subject = "my subject"

        foreach (Attachment t in oMailItem.Attachments)
         {
              t.Delete();
            }

          oMailItem.Attachments.Add(@"mydirectory");

           oMailItem.Send();
}

1 个答案:

答案 0 :(得分:2)

_MailItem.Send()关闭当前的检查员。这不在_MailItem.Send documentation中,而是实际的Outlook实现。你可能应该提出另一种方法。我建议为您要发送的每封邮件创建一个新的MailItem实例。

您可以使用...

创建新的MailItem
Outlook.MailItem eMail = (Outlook.MailItem)
Globals.ThisAddIn.Application.CreateItem(Outlook.OlItemType.olMailItem);
eMail.Subject = subject;
eMail.To = toEmail;
eMail.Body = body;
eMail.Importance = Outlook.OlImportance.olImportanceLow;
((Outlook._MailItem)eMail).Send();

发送给所有收件人后,您可以使用以下方法手动关闭当前检查器( Send()隐式调用此方法

((Outlook._MailItem)myMailItem).Close(Outlook.OlInspectorClose.olDiscard)