问题
I've created a small Outlook add-on to add a link into the body of an email if the email subject contains a certain string. Currently, the link will only be added if the mail is (double-clicked) opened. Is there a way to add the link without the user opening the message first? Or is what I'm asking impossible? And if so, why?
void inspectors_NewInspector(Inspector Inspector)
{
Outlook.MailItem mailItem = Inspector.CurrentItem as Outlook.MailItem;
if (mailItem != null)
{
if (mailItem.Subject.StartsWith("ABCDE"))
{
mailItem.BodyFormat = OlBodyFormat.olFormatHTML;
mailItem.HTMLBody = "<html><body>Click <a href='www.google.com'>here</a>.<br><br></body></html>" + mailItem.HTMLBody;
mailItem.Save();
}
}
}
回答1:
I actually found the answer to this on my own and will post it below for anyone needing it in the future. I used THIS answer as a guide.
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
this.Application.NewMailEx += new Outlook.ApplicationEvents_11_NewMailExEventHandler(olApp_NewMail);
}
private void olApp_NewMail(String entryIDCollection)
{
Outlook.NameSpace outlookNS = this.Application.GetNamespace("MAPI");
Outlook.MAPIFolder mFolder = this.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
Outlook.MailItem mail;
try
{
mail = (Outlook.MailItem)outlookNS.GetItemFromID(entryIDCollection, Type.Missing);
if (mailItem.Subject.StartsWith("ABCDE"))
{
mailItem.BodyFormat = OlBodyFormat.olFormatHTML;
mailItem.HTMLBody = "<html><body>Click <a href='www.google.com'>here</a>.<br><br></body></html>" + mailItem.HTMLBody;
mailItem.Save();
}
}
catch
{}
}
来源:https://stackoverflow.com/questions/8481372/outlook-add-on-to-add-text-to-mail-body