简体   繁体   English

通过Outlook加载项编辑电子邮件正文中的文本

[英]Editing text in body of email via Outlook add-in

The following is an add-in for MS Outlook. 以下是MS Outlook的加载项。 One can scan the body of an e-mail and if there is a certain (specific word or pattern) word present, then a MessageBox appears. 可以扫描电子邮件的正文,如果存在某个(特定单词或样式)单词,则会出现一个MessageBox。 However, I am wondering if it is possible to change the way a word appears or to edit the text in the body of email, without MessageBox whatsoever. 但是,我想知道是否可以在没有MessageBox的情况下更改单词的显示方式或编辑电子邮件正文中的文本。 For example, a word (such as the name of the company) can be converted into an hyperlink (ie Google to www.google.com or Microsoft to www.microsoft.com) and the user who reads emails on Outlook always sees the hyperlink instead of the word itself. 例如,一个单词(例如公司名称)可以转换为超链接(即Google到www.google.com或Microsoft到www.microsoft.com),并且在Outlook上阅读电子邮件的用户始终会看到超链接而不是单词本身。

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;
using System.Drawing;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Text.RegularExpressions;


namespace FirstOutlookAddIn
{
    public partial class ThisAddIn
    {
        public static string[] data = new string[10];
        public static Stopwatch timer = new Stopwatch();
        Outlook.NameSpace outlookNameSpace;
        Outlook.MAPIFolder inbox;
        Outlook.Items items;

        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {

            timer = Stopwatch.StartNew(); ReadMail();
            outlookNameSpace = this.Application.GetNamespace("MAPI");
            inbox = outlookNameSpace.GetDefaultFolder(
                    Microsoft.Office.Interop.Outlook.
                    OlDefaultFolders.olFolderInbox);
            items = inbox.Items;

            items.ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(ReadSingleMail); // Modified method to run for single email

        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
            // Hinweis: Outlook löst dieses Ereignis nicht mehr aus. Wenn Code vorhanden ist, der 
            // ausgeführt werden muss, wenn Outlook geschlossen wird, informieren Sie sich unter http://go.microsoft.com/fwlink/?LinkId=506785
        }

        static void ReadSingleMail(dynamic item)
        {
            string bodyText; // Email body
            string mydocpath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); //Path to My Documents

            if (item != null)
            {
                bodyText = item.Body;
            }
            else
            {
                return; // If no e-mail body, exit function.
            }
        }

        static void ReadMail()
        {
            //Set up OCR
            string bodyText;
            string mydocpath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

            //Get unread emails from Inbox
            Microsoft.Office.Interop.Outlook.Application app = null;
            Microsoft.Office.Interop.Outlook._NameSpace ns = null;
            Microsoft.Office.Interop.Outlook.MAPIFolder inboxFolder = null;
            app = new Microsoft.Office.Interop.Outlook.Application();
            ns = app.GetNamespace("MAPI");
            inboxFolder = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
            Outlook.Items unreadItems = inboxFolder.Items.Restrict("[Unread]=true");
            int max_runs;
            //Go through each Unread email
            if (unreadItems.Count > 10) { max_runs = 10; }
            else max_runs = unreadItems.Count;

            for (int counter = 1; counter <= max_runs; counter++)
            {
                //Reinitialize Data array
                for (int index = 0; index <= 8; index++)
                {
                    data[index] = "";
                }
                dynamic item = unreadItems[counter];
                bodyText = item.Body;
                Match match = Regex.Match(bodyText, "Insert searched pattern here");

                if (match.Success)
                {
                    MessageBox.Show(match.Value);
                    match = match.NextMatch();
                }

            }            
        }

        #region Von VSTO generierter Code

        /// <summary>
        /// Erforderliche Methode für die Designerunterstützung.
        /// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }

        #endregion
    }
}

Creating the function ReadMail() as below replaces text, as I want it. 如下创建函数ReadMail()可以替换文本。

static void ReadMail(){
    Microsoft.Office.Interop.Outlook.Application app = null;
    Microsoft.Office.Interop.Outlook._NameSpace ns = null;
    Microsoft.Office.Interop.Outlook.MAPIFolder inboxFolder = null;
    app = new Microsoft.Office.Interop.Outlook.Application();
    ns = app.GetNamespace("MAPI");
    inboxFolder = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
    Outlook.Items Items = inboxFolder.Items.Restrict("[LastModificationTime] > '01/1/2003'");

    foreach (var item in Items){
        var mail = (Outlook.MailItem)item;
        mail.Body = mail.Body.Replace("Text to be replaced", "Replacing text");
        mail.Save();
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 email 正文格式从 Outlook VSTO 加载项更改为 HTML? - How to change email body format to HTML from an Outlook VSTO add-in? 通过C#开发的Outlook加载项将评论附加到电子邮件 - Attach Comments to Email via Outlook Add-In developed via C# Outlook加载项-用于电子邮件跟踪的自定义标题 - Outlook Add-In - Custom Headers for Email Tracking Outlook外接程序,在电子邮件和回复电子邮件之间建立关系 - outlook add-in,build a relation between an email and the reply email Outlook 互操作添加正文 - Outlook Interop add body text 是否为VSTO Outlook加载项修改MailItem.Body? - Modifying MailItem.Body for VSTO Outlook Add-in? 是否可以捕获发送电子邮件状态的Outlook加载项C# - is it possible capture sending email status outlook add-in c# 创建一个Outlook加载项以将电子邮件发送到Sharepoint中的文档库 - Creating an Outlook add-in that sends email to document library in Sharepoint Outlook加载项:检测何时选择任何抄送或收件人电子邮件地址 - Outlook Add-In: Detect when any of the CC or TO email addresses are selected Outlook加载项:通过检查器移动电子邮件时的事件 - Outlook Add-In: event when email is moved through inspector
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM