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

发布于 2024-12-19 13:52:18 字数 1303 浏览 1 评论 0原文

您好,我正在开发一个 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();
}

Hello I am developing an outlook Add-on, as part of the work flow it should take the mailItem body and subject, and for each recipient it should change the body of message according to recipient e-mail.

The problem is that it just sends the first e-mail and after Send(); it does not send the e-mail to other recipients

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野却迷人 2024-12-26 13:52:18

_MailItem.Send() 关闭当前检查器。这不在 中_MailItem.Send 文档,而是实际的 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)

_MailItem.Send() closes the current inspector. This isn't in the _MailItem.Send documentation, but is the actual Outlook implementation. You should probably come up with another approach. I'd suggest creating a new MailItem instance for each message you wish to send.

You can create a new MailItem using...

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

After sending to all recipients you can manually close the current inspector using the following (Send() implicitly calls this method)

((Outlook._MailItem)myMailItem).Close(Outlook.OlInspectorClose.olDiscard)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文