【问题标题】:VSTO outlook add-in - Retrieve sender's name for replyVSTO Outlook 加载项 - 检索发件人姓名以进行回复
【发布时间】:2021-05-26 03:10:42
【问题描述】:

我正在完成如何使用功能区设计器创建您的第一个 Outlook 插件的演练:https://docs.microsoft.com/en-us/visualstudio/vsto/walkthrough-creating-your-first-vsto-add-in-for-outlook?view=vs-2019

我的目标是制作在问候语中包含发件人姓名的回复模板。

通过研究这个问题,我相信我需要MailItem.SenderEmailAddress 或MailItem.Sender,但是当我尝试将它添加到mailitem.htmlbody 时,它根本没有显示任何内容。

这是我的测试代码。

 private void Teser_button2_Click(object sender, RibbonControlEventArgs e)
        {
            {
                if (Globals.ThisAddIn.Application.ActiveExplorer() != null)
                {
                    MailItem mi = Globals.ThisAddIn.Application.ActiveExplorer().ActiveInlineResponse;
                    if (mi != null)
                    {

                        mi.HTMLBody = mi.SenderEmailAddress + @"<BODY style=font-size:11pt;font-family:Calibri></BODY>Hello ENTERNAMEHERE,
<br><
<br>Here is a brief summary on everything worked on:
<br>
<br>Thank you for your time,
<br>
" + mi.HTMLBody;
                        mi.BodyFormat = Outlook.OlBodyFormat.olFormatHTML;
                    }
                }
            }
        }

我尝试将mi.SenderEmailAddress 更改为mi.Sender,但仍然一无所获。

感谢任何想法!

更新

以下代码对我既适用于 Outlook Explorer,也适用于弹出窗口(它不是最好的,但很有效):

 private void Domain_setup_Click_1(object sender, RibbonControlEventArgs e)
        {
            if (Globals.ThisAddIn.Application.ActiveExplorer() != null)
            {
                MailItem mi = Globals.ThisAddIn.Application.ActiveExplorer().ActiveInlineResponse;
                Outlook.Selection selection = Globals.ThisAddIn.Application.ActiveExplorer().Selection;
                object item = selection[1];
                if (mi != null && item is Outlook.MailItem mailItem)
                {
                    string senderName = mailItem.SenderName;
                    mi.HTMLBody = @"<BODY style=font-size:11pt;font-family:Calibri></BODY>Hello " + senderName + @",
<br>
<br>Here is the information for the new domain/user account:
<br>
<br>Computer username: ENTERDOMAINORUSERNAMEHERE
<br>Computer password: ENTERPASSWORD
<br>
<br>Thank you for your time,
<br>
" + mi.HTMLBody;
                }
            }
            if (Globals.ThisAddIn.Application.ActiveInspector() != null)
            {
                MailItem mi = Globals.ThisAddIn.Application.ActiveInspector().CurrentItem;
                Outlook.Selection selection = Globals.ThisAddIn.Application.ActiveExplorer().Selection;
                object item = selection[1];
                if (mi != null && item is Outlook.MailItem mailItem)
                {
                    string senderName = mailItem.SenderName;
                    mi.HTMLBody = @"<BODY style=font-size:11pt;font-family:Calibri></BODY>Hello " + senderName + @",
<br>
<br>Here is the information for the new domain/user account:
<br>
<br>Computer username: ENTERDOMAINORUSERNAMEHERE
<br>Computer password: ENTERPASSWORD
<br>
<br>Thank you for your time,
<br>
" + mi.HTMLBody;
                }
            }
        }

【问题讨论】:

    标签: c# outlook vsto


    【解决方案1】:

    您访问的是回复中的发件人姓名,而不是正在回复的邮件。该名称应来自Globals.ThisAddIn.Application.ActiveExplorer().Selection 集合中的第一项。

    Outlook.Selection selection = Globals.ThisAddIn.Application.ActiveExplorer().Selection;
    if (selection.Count > 0)
    {
      object item = selection[1];
      if (item is Outlook.MailItem mailItem)
      {
        string senderName = mailItem.SenderName;
      }
    }
    

    此外,您不能也不应该连接两个 HTML 字符串 - 结果将 不是 是有效的 HTML 文档,两者必须合并(例如,您可以在 @ 末尾插入 HTML 987654323@ 标签在mi.HTMLBody)。

    【讨论】:

    • 我尝试使用testvare = Globals.ThisAddIn.Application.ActiveExplorer().sender;,但收到错误消息:type' does not contain a definition for 'name' and no accessible extension method 'name' accepting a first argument of type 'type' could be found (are you missing a using directive or an assembly reference?)
    • 我也尝试使用testvar = Globals.ThisAddIn.Application.ActiveExplorer.sender;,但我收到另一个错误说Application.ActiveExplorer is a method, which is not valid in the given context.
    • 这确实有效 - 但我应该更详细。你看,我只给出了一半的功能,因为我认为它不是进口的。你看..我实际上同时使用了ActiveExplorer() 和ActiveInspector(),这样我就可以从outlook explorer 或弹出窗口中进行回复。您的代码有效,但仅适用于 Outlook Explorer,这是有意义的,因为这是我唯一要求的。哈哈哈我试着看看ActiveInspector().selection 是不是一个东西,它看起来不像。我会将您的答案标记为正确,看看我是否可以从中学习。谢谢!
    • 如果您在检查员中有一个新项目,事情就会变得更加棘手 - 您不知道您正在处理的回复(ActiveInspector.CurrentItem)来自哪里。它可能是显示原始项目的另一个检查器,也可能来自Explorer 中选择的项目。要处理这两种情况,您需要捕获 Explorer.SelectionChange 和 Inspectors.NewInspector 事件并在原始项目上设置 Reply/ReplyAll/Forward 事件处理程序。当事件触发时,您将同时拥有旧项目和新项目(作为参数传递)。
    • 我编写了一些似乎可以工作的代码。我将它添加到 make 帖子中,因为我不能在这里制作多行 cmets
    【解决方案2】:

    与发件人相关的属性存在于收到的项目上。因此,您真正需要的是获取要回复的源项目。为避免不必要地搜索原始电子邮件,您可以使用多种方法在 Outlook 中检索所需信息:

    1. 您可以连接到 Outlook 项目的 Reply 事件,因此当用户选择回复电子邮件时,您将获得所有必需的信息并粘贴到邮件正文中。您可能会发现 Implement a wrapper for inspectors and track item-level events in each inspector 文章很有帮助。响应对象的实例作为参数传递,因此您可以使用从事件源中提取的与发送者相关的信息来修改消息正文。

    2. 如果该项目已保存并已发送/接收,您可以使用GetConversation 方法获取代表该项目所属会话的Conversation 对象。对话代表一个或多个文件夹和商店中的一个或多个项目。如果您将对话中的项目移动到Deleted Items 文件夹,然后使用GetChildren、GetRootItems 或GetTable 方法枚举对话,则该项目将不会包含在返回的对象中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-27
      • 2020-01-18
      • 2015-07-27
      • 2017-11-04
      • 2011-09-13
      • 1970-01-01
      • 2014-05-10
      • 2023-03-17
      相关资源
      最近更新 更多