【发布时间】: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