Outlook Add-on to Add Text to Mail Body

北城以北 提交于 2019-12-11 05:23:32

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!