【问题标题】:Adding to Outlook Email Recipient using VSTO before sending email在发送电子邮件之前使用 VSTO 添加到 Outlook 电子邮件收件人
【发布时间】:2019-07-24 14:24:12
【问题描述】:

在 C# 中使用 Outlook 寻找有关 VSTO 功能区的一些建议或方向。

到目前为止,我已经构建了一个 Outlook 2010 功能区(使用 TabMail),该功能区打开一个 WinForms 窗口,允许我的用户通过 DataGridView 从 SQL 数据库中的自定义构建通讯簿中选择联系人。

用户基本上从 datagridview 中选择他们想要发送电子邮件的人,然后将其添加到 toLine 列表中。

Application app = new Microsoft.Office.Interop.Outlook.Application();
Mail item item = app.CreateItem((OlItemType.olMailItem));
item.To = toLine;
Item.Display();
This.close();

使用这种方法的缺点是用户必须在实际撰写电子邮件之前建立他们的收件人列表。

我现在正在尝试使用 TabMailNewMessage。这应该允许用户撰写他们的电子邮件,然后单击新邮件中的功能区图标并从那里添加到他们的收件人列表中。

我的图标在 TabMailNewMessage 中显示正常,我可以用它来打开 2nd Win Form [目前作为测试]。

我有点不确定如何将已打开的现有邮件项添加到收件人列表中。

目前我在 2nd Win Form 上只有一个按钮,谁能解释我如何单击该按钮并简单地将某人添加到 [这封已撰写的电子邮件] 的收件人列表中。 (我在按钮点击后没有任何代码,因为我不知道该怎么做)

我还需要确保它不发送电子邮件,而只是将用户添加到收件人列表中。

目前使用 Office 2010 和 VS 2013(使用 C#)。

希望我在这里有点意思。

谢谢

编辑: 不确定它是否简单

Application app = Globals.ThisAddIn.Application;
MailItem mi = (Outlook.MailItem)app.ActiveInspector().CurrentItem;
Mi.Recipients.Add(“joe@email.com”);
This.Close();

【问题讨论】:

    标签: c# email outlook vsto


    【解决方案1】:

    MailItem.Recipients.Add 方法允许在Recipients 集合中创建新收件人。收件人的名称可以是一个字符串,表示收件人的显示名称、别名或完整的 SMTP 电子邮件地址。

    using System.Runtime.InteropServices;
    // ...
    private bool AddRecipients(Outlook.MailItem mail)
    {
        bool retValue = false;
        Outlook.Recipients recipients = null;
        Outlook.Recipient recipientTo = null;
        Outlook.Recipient recipientCC = null;
        Outlook.Recipient recipientBCC = null;
        try
        {
            recipients = mail.Recipients;
            // first, we remove all the recipients of the e-mail
            while(recipients.Count != 0)
            {
                recipients.Remove(1);                    
            }
            // now we add new recipietns to the e-mail
            recipientTo = recipients.Add("Eugene Astafiev");
            recipientTo.Type = (int)Outlook.OlMailRecipientType.olTo;
            recipientCC = recipients.Add("Somebody");
            recipientCC.Type = (int)Outlook.OlMailRecipientType.olCC;
            recipientBCC = recipients.Add("eugene.astafiev@somedomain.com");
            recipientBCC.Type = (int)Outlook.OlMailRecipientType.olBCC;
            retValue = recipients.ResolveAll();
        }
        catch (Exception ex)
        {
            System.Windows.Forms.MessageBox.Show(ex.Message);
        }
        finally
        {
            if (recipientBCC != null) Marshal.ReleaseComObject(recipientBCC);
            if (recipientCC != null) Marshal.ReleaseComObject(recipientCC);
            if (recipientTo != null) Marshal.ReleaseComObject(recipientTo);
            if (recipients != null) Marshal.ReleaseComObject(recipients);
        }
        return retValue;
    }
    

    您可能会发现以下文章很有帮助:

    另外,我建议开发一个 Outlook 表单区域,它可以显示在同一窗口上,而不是使用独立窗口,请参阅 Create Outlook form regions 了解更多信息。

    【讨论】:

      【解决方案2】:

      是的,就像使用Application.ActiveInspector.CurrentItem 一样简单。但您可以做得更好 - 您的功能区按钮事件处理程序将 IRibbonControl 对象作为参数。将 IRibbonControl.Context 属性转换为 InspectorExplorer(取决于按钮所在的位置)。

      另外请记住,在 COM 插件中没有理由创建 Outlook.Application 对象的新实例(无论如何它都会受到安全提示的影响) - 使用 Globals.ThisAddIn.Application

      【讨论】:

      • 觉得它看起来太简单了,但我一定会研究你建议的 IRibbonControl。
      猜你喜欢
      • 2017-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-24
      • 1970-01-01
      相关资源
      最近更新 更多