【问题标题】:MailItem.Display(true|false) exception when used in WinForms在 WinForms 中使用 MailItem.Display(true|false) 异常
【发布时间】:2017-05-03 21:43:08
【问题描述】:

我正在创建一个 Outlook 加载项,它有一个子表单。表单上有一个按钮,如果用户单击它,我想通过它生成一个邮件项。我想在电子邮件中自动填充一些信息,然后让用户在闲暇时发送。

我的代码如下所示:

private void btnMailDocNotice_Click(object sender, EventArgs e)
    {
        string clientInfo = string.Empty;
        string matInfo = string.Empty;
        string author = string.Empty;
        string dType = string.Empty;
        string fLocation = string.Empty;
        string keyWords = string.Empty;
        string docName = string.Empty;

        clientInfo = this.mCboClient.Text + " " + lblClient;
        matInfo = this.mCboMatter.Text + " " + lblMatter;
        author = this.txtAuthor.Text;
        dType = this.mCboDocType.Text.ToUpper();
        fLocation = this.txtSavePath.Text;
        keyWords = this.txtKeyWords.Text;
        docName = this.txtDocName.Text;

        this.sendDocNotice = true;
        this.Hide();
        CreateMailItem(clientInfo, matInfo, author, dType, this.operatorCode.ToUpper(), fLocation, keyWords, docName);
        this.Show();
    }

private void CreateMailItem(string clientInfo, string matInfo, string author, string dType, string profiledBy, string fLocation, string keyWords, string docName)
    {
        this.DNoticeItem = (Outlook.MailItem)ThisAddIn.myApp.CreateItem(Outlook.OlItemType.olMailItem);
        this.DNoticeItem.Subject = "Document: " + docName;
        this.DNoticeItem.HTMLBody = "<span style=\"font-family:Calibri; font-size: 11pt;\">KeyWords: " + keyWords + "</span>";
        this.DNoticeItem.HTMLBody += "<br />Client: " + clientInfo;
        this.DNoticeItem.HTMLBody += "<br />Matter: " + matInfo;
        this.DNoticeItem.HTMLBody += "<br />Author: " + author;
        this.DNoticeItem.HTMLBody += "<br />Doc Type: " + dtClient;
        this.DNoticeItem.HTMLBody += "<br />Profiled by: " + profiledBy;
        this.DNoticeItem.HTMLBody += "<br />File://" + fLocation;
        this.DNoticeItem.Importance = Outlook.OlImportance.olImportanceNormal;
        this.DNoticeItem.Display(false);
    }

我遇到的问题是,它是否会在 mailitem.display 函数上引发异常,无论我使用 true 还是 false(做一些研究表明这决定了用户是否可以访问主 Outlook 窗口当邮件项打开时)。异常是“对话框已打开。关闭并重试”的 COM 异常。我尝试在创建邮件项的函数调用之前隐藏 WinForm,然后在函数退出后再次显示它,但它不起作用。我尝试了一个代码版本,在该版本中我使用 System.Diagnostics.Process.Start() 在将文件保存到磁盘后尝试打开文件,虽然它不会从加载项中触发异常,但 Outlook 会提示具有来自 ComException 的相同消息的消息框的用户。我什至尝试创建一个字段来查看是否应该起草文档通知电子邮件,并认为在 form.close() 调用之后让代码处理这个问题,认为关闭调用至少会处理之前的对话框锁定 Outlook,我仍然遇到同样的异常。

有没有办法实现我想要的?有没有人有什么建议?我现在有点卡住了,如果有人在这个问题上提供任何帮助/指针/建议,我将不胜感激。如果这是一个重复的问题,我深表歉意 - 我找不到这个问题的好答案。提前感谢您的宝贵时间。

【问题讨论】:

    标签: c# winforms email outlook outlook-addin


    【解决方案1】:

    首先,为什么不无模式地显示你自己的表格?

    其次(这很重要)不要使用如下代码

    this.DNoticeItem.HTMLBody += "<br />Client: " + clientInfo;
    

    每次运行这样的一行时,您都会检索 HTMLBody,向其中添加一些内容(使 HTML 格式错误),然后再次设置 HTMLBody 并强制 Outlook 理解您的(格式错误的)HTML。这(假设 Outlook 可以解析和修复您的 HTML)将导致返回的 HTML 与您设置的不同。

    使用常规字符串构建一次 HTML 正文,并且只设置一次 HTMLBody 属性。

    【讨论】:

    • 原谅我的无知 - 你能给我一个无模式显示表单的例子吗?我不熟悉这个词。感谢您提供有关 HTML 的提示,不知道回复:HTMLBody。我会构建,然后按照您的建议申请一次。
    • 提示提示 = new Prompt(mail, OutlookItemType.Sent); prompt.ShowDialog();
    • 用 Show() 替换 ShowDialog。
    • 我刚刚尝试过,仍然得到异常。此外,奇怪的是,选项卡按钮不再从控制切换到控制。在任何字段中按 Tab 保存多行文本框会导致窗口发出哔声。切换回 ShowDialog 至少可以恢复选项卡功能...
    • 您的表单是否创建为相应 Outlook 窗口的子窗口?
    猜你喜欢
    • 1970-01-01
    • 2011-04-01
    • 2017-07-22
    • 2018-09-07
    • 2011-01-16
    • 2021-12-25
    • 2016-12-15
    • 1970-01-01
    相关资源
    最近更新 更多