【发布时间】:2021-06-12 23:42:57
【问题描述】:
在单独的窗口中撰写新电子邮件时,我的 Outlook 加载项无法正常工作。但是,当您在主窗口中回复或转发它时,我的用户提示不会出现。似乎在某些时候代码没有捕捉到 Outlook 吐出的事件。
如果有人有任何想法,我将不胜感激!
using System;
using System.Collections.Generic;
using Outlook = Microsoft.Office.Interop.Outlook;
using System.Windows.Forms;
namespace AutoMarkings
{
public partial class ThisAddIn
{
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
this.Application.ItemSend += new Outlook.ApplicationEvents_11_ItemSendEventHandler(Application_ItemSend)
}
void Application_ItemSend(object Item, ref bool Cancel)
{
Outlook.Inspector inspector = Application.ActiveInspector();
if (inspector != null)
{
object item = inspector.CurrentItem;
Outlook.MailItem mail = Item as Outlook.MailItem;
string PR_SECURITY_FLAGS = @"http://schemas.microsoft.com/mapi/proptag/0x6E010003";
int sec = mail.PropertyAccessor.GetProperty(PR_SECURITY_FLAGS);
if (item is Outlook.MailItem)
{
DialogResult result = MessageBox.Show("SAMPLE MESSAGE", MessageBoxButtons.YesNoCancel);
if (result == DialogResult.Cancel)
{
Cancel = true;
}
else if (result == DialogResult.Yes)
{
var recips = mail.Recipients;
bool external = false;
for (int i = recips.Count; i >= 1; i--)
{
Outlook.Recipient recip = (Outlook.Recipient)recips[i];
Outlook.PropertyAccessor pa = recip.PropertyAccessor;
const string PR_SMTP_ADDRESS =
"http://schemas.microsoft.com/mapi/proptag/0x39FE001E";
string smtpAddress =
pa.GetProperty(PR_SMTP_ADDRESS).ToString();
if (!smtpAddress.Contains("@COMPANYDOMAIN"))
{
external = true;
break;
}
}
if (sec > 31)
{
sec = sec % 32;
}
if (external == true)
{
if ((sec == 0) || (sec == 2))
{
MessageBox.Show("This email is being sent to an external address. Please encrypt the message and send again.");
Cancel = true;
}
}
if (Cancel == false && mail != null)
{
var attachments = mail.Attachments;
string addToBody = "SAMPLE BODY";
List<string> list = new List<string>();
if (!mail.HTMLBody.Contains(addToBody)) mail.HTMLBody = (addToBody + "<br/>" + "<br/>" + mail.HTMLBody);
}
}
}
}
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e) { }
# region VSTO generated code /// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
}
# endregion
}
}
【问题讨论】:
标签: c# outlook outlook-addin