在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#语言基础 → 分享,C#实现OutLook邮箱邮件检索

分享,C#实现OutLook邮箱邮件检索

C#语言基础

下载此实例
  • 开发语言:C#
  • 实例大小:0.54M
  • 下载次数:44
  • 浏览次数:1245
  • 发布时间:2017-04-12
  • 实例类别:C#语言基础
  • 发 布 人:xiaz
  • 文件格式:.zip
  • 所需积分:2
 相关标签: 邮件 C# OutLook 邮箱 c

实例介绍

【实例简介】C#实现OutLook邮箱邮件检索
【实例截图】
【核心代码】

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using Outlook = Microsoft.Office.Interop.Outlook;
using Webus.Documents;
using Webus.Index;
using Webus.Search;
using System.Threading.Tasks;
using System.Threading;
using Webus.Helpers;
using System.IO;
using System.Text.RegularExpressions;
using System.Diagnostics;

namespace OutlookSearcher
{
    public partial class frmOutlookSearcher : Form
    {
        IIndexer IndexAccessor = null;
        bool isClosing = false;
        int InboxIndx = 1, SentboxIndex = 1;
        string inbox_index_file = AppDomain.CurrentDomain.BaseDirectory   "inbox_index.txt";
        string sentbox_index_file = AppDomain.CurrentDomain.BaseDirectory   "sentbox_index.txt";
        Task IndexingTask;
        Outlook.Application OutlookApp;
        Outlook.NameSpace OutlookNS;
        Outlook.MAPIFolder Inbox;
        Outlook.MAPIFolder Sentbox;


        public frmOutlookSearcher()
        {
            InitializeComponent();
            UpdateUI  = new UpdateUIEventHandler(frmOutlookSearcher_UpdateUI);
        }

        void frmOutlookSearcher_UpdateUI(object sender, string msg)
        {
            try
            {
                lbStatusText.Text = msg;
            }
            catch { };
        }

        void InitOutlookApp()
        {
            if (OutlookApp == null)
            {
                OutlookApp = new Outlook.Application();
                OutlookNS = OutlookApp.GetNamespace("MAPI");
                Inbox = OutlookNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
                Sentbox = OutlookNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail);
            }
        }

        private void frmOutlookSearcher_Load(object sender, EventArgs e)
        {
            try
            {
                this.InitOutlookApp();
                this.IndexAccessor = new IndexManager(new MailAnalyzer());
                this.IndexAccessor.MaxIndexSize = int.MaxValue;
                this.IndexAccessor.MinIndexSize = int.MaxValue;
                this.IndexAccessor.MergeFactor = int.MaxValue;
                if (File.Exists(inbox_index_file))
                {
                    InboxIndx = int.Parse(StringHelper.LoadString(inbox_index_file));
                }
                if (File.Exists(sentbox_index_file))
                {
                    SentboxIndex = int.Parse(StringHelper.LoadString(sentbox_index_file));
                }
                IndexingTask = Task.Factory.StartNew(this.IndexProc);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ExceptionHelper.ToString(ex));
            }
        }

        public delegate void UpdateUIEventHandler(object sender, string msg);
        public event UpdateUIEventHandler UpdateUI;
        private void IndexProc()
        {
            IndexAccessor.OpenOrNew(AppDomain.CurrentDomain.BaseDirectory   @"Index");
            while (isClosing == false)
            {
                try
                {
                    this.InitOutlookApp();
                    for (; InboxIndx <= Inbox.Items.Count; InboxIndx  )
                    {
                        try
                        {
                            this.InitOutlookApp();
                            if (isClosing)
                            {
                                return;
                            }
                            var item = Inbox.Items[InboxIndx];
                            if (item is Outlook.MailItem)
                            {
                                Outlook.MailItem mailItem = item as Outlook.MailItem;
                                var mailInfo = new MailInfo()
                                {
                                    EntryId = string.IsNullOrEmpty(mailItem.EntryID) ? string.Empty : mailItem.EntryID,
                                    From = string.IsNullOrEmpty(mailItem.SenderEmailAddress) ? string.Empty : mailItem.SenderEmailAddress,
                                    ConversationId = string.IsNullOrEmpty(mailItem.ConversationID) ? string.Empty : mailItem.ConversationID,
                                    Subject = string.IsNullOrEmpty(mailItem.Subject) ? string.Empty : mailItem.Subject,
                                    Body = string.IsNullOrEmpty(mailItem.HTMLBody) ? string.Empty : mailItem.HTMLBody,
                                    Folder = string.IsNullOrEmpty(Inbox.Name) ? string.Empty : Inbox.Name,
                                    To = string.IsNullOrEmpty(mailItem.To) ? string.Empty : mailItem.To
                                };
                                IndexAccessor.Add(mailInfo.ToDoc());
                                if (UpdateUI != null)
                                {
                                    UpdateUI(Inbox, string.Format("收件箱: {0}/{1}", InboxIndx, Inbox.Items.Count));
                                }
                            }
                        }
                        catch (Exception ex1)
                        {
                            if (UpdateUI != null)
                            {
                                UpdateUI(ex1, ex1.Message);
                            }
                            OutlookApp = null;
                        }
                    }
                    for (; SentboxIndex <= Sentbox.Items.Count; SentboxIndex  )
                    {
                        try
                        {
                            this.InitOutlookApp();
                            if (isClosing)
                            {
                                return;
                            }
                            var item = Sentbox.Items[SentboxIndex];
                            if (item is Outlook.MailItem)
                            {
                                Outlook.MailItem mailItem = item as Outlook.MailItem;
                                var mailInfo = new MailInfo()
                                {
                                    EntryId = string.IsNullOrEmpty(mailItem.EntryID) ? string.Empty : mailItem.EntryID,
                                    From = string.IsNullOrEmpty(mailItem.SenderEmailAddress) ? string.Empty : mailItem.SenderEmailAddress,
                                    ConversationId = string.IsNullOrEmpty(mailItem.ConversationID) ? string.Empty : mailItem.ConversationID,
                                    Subject = string.IsNullOrEmpty(mailItem.Subject) ? string.Empty : mailItem.Subject,
                                    Body = string.IsNullOrEmpty(mailItem.HTMLBody) ? string.Empty : mailItem.HTMLBody,
                                    Folder = string.IsNullOrEmpty(Sentbox.Name) ? string.Empty : Sentbox.Name,
                                    To = string.IsNullOrEmpty(mailItem.To) ? string.Empty : mailItem.To
                                };
                                IndexAccessor.Add(mailInfo.ToDoc());
                                if (UpdateUI != null)
                                {
                                    UpdateUI(Sentbox, string.Format("已发邮件: {0}/{1}", SentboxIndex, Sentbox.Items.Count));
                                }
                            }
                        }
                        catch (Exception ex2)
                        {
                            if (UpdateUI != null)
                            {
                                UpdateUI(ex2, ex2.Message);
                            }
                            OutlookApp = null;
                        }

                    }
                }
                catch (Exception ex)
                {
                    if (UpdateUI != null)
                    {
                        UpdateUI(ex, ex.Message);
                    }
                    OutlookApp = null;
                }
                Thread.Sleep(3000);
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                Regex re = new Regex("\\w \\s*[^\\\"] \\s*\\\"[^\\\"] \\\"");
                ISearcher searcher = new IndexSearcher(this.IndexAccessor);
                Hits hits = null;
                if (re.IsMatch(txtKeyword.Text))
                {
                    hits = searcher.Search(txtKeyword.Text, new MailAnalyzer());
                }
                else
                {
                    hits = searcher.Search(string.Format("Subject=\"{0}\" | Body=\"{0}\"", txtKeyword.Text.ToLower()), new MailAnalyzer());
                }
                List<MailInfo> result = new List<MailInfo>();
                foreach (HitDoc hit in hits)
                {
                    result.Add(new MailInfo(hit.GetDoc()));
                }
                dgvMails.DataSource = result;
                lbStatusText.Text = string.Format("找到{0}封邮件", result.Count);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ExceptionHelper.ToString(ex));
            }
        }

        private void dgvMails_SelectionChanged(object sender, EventArgs e)
        {
            try
            {
                if (dgvMails.SelectedRows.Count > 0)
                {
                    wbMail.DocumentText = dgvMails.SelectedRows[0].Cells["Body"].Value.ToString();
                    txtMailSubject.Text = dgvMails.SelectedRows[0].Cells[1].Value.ToString();
                    btnOpenInOutlook.Tag = dgvMails.SelectedRows[0].Cells["EntryId"].Value.ToString();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ExceptionHelper.ToString(ex));
            }

        }

        private void frmOutlookSearcher_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (isClosing == false)
            {
                e.Cancel = true;
                this.ShowInTaskbar = false;
                this.Hide();
            }
        }

        private void btnHelp_Click(object sender, EventArgs e)
        {
            try
            {
                wbMail.Navigate("http://www.cnblogs.com/iamzyf/p/3209710.html");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ExceptionHelper.ToString(ex));
            }

        }

        private void btnAbout_Click(object sender, EventArgs e)
        {
            try
            {
                Process.Start("http://www.gdtsearch.com");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ExceptionHelper.ToString(ex));
            }
        }

        private void btnOpenInOutlook_Click(object sender, EventArgs e)
        {
            try
            {
                var item = OutlookNS.GetItemFromID(btnOpenInOutlook.Tag.ToString());
                if (item is Outlook.MailItem)
                {
                    (item as Outlook.MailItem).Display();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ExceptionHelper.ToString(ex));
            }
        }

        private void btnContactUs_Click(object sender, EventArgs e)
        {
            MessageBox.Show("QQ: 171567278\nEmail: yinfan.zou@icloud.com");
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            try
            {
                isClosing = true;
                if (this.IndexAccessor != null)
                {
                    Task.WaitAll(IndexingTask);
                    this.IndexAccessor.Close();
                    StringHelper.WriteString(inbox_index_file, InboxIndx.ToString());
                    StringHelper.WriteString(sentbox_index_file, SentboxIndex.ToString());
                }
                Application.Exit();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ExceptionHelper.ToString(ex));
            }
        }

        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            this.ShowInTaskbar = true;
            this.Show();
        }

        private void btnDownloadWebus_ButtonClick(object sender, EventArgs e)
        {
            try
            {
                Process.Start("http://www.cnblogs.com/iamzyf/archive/2008/01/02/1023327.html");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ExceptionHelper.ToString(ex));
            }
        }

        private void btnLatestWEBUS_Click(object sender, EventArgs e)
        {
            try
            {
                Process.Start("http://www.gdtsearch.com/downloads/WEBUS2.0_latest.zip");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ExceptionHelper.ToString(ex));
            }
        }

        private void btnDownloadCodeSearch_ButtonClick(object sender, EventArgs e)
        {
            try
            {
                Process.Start("http://www.cnblogs.com/iamzyf/p/3199434.html");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ExceptionHelper.ToString(ex));
            }
        }
    }

    public class MailInfo
    {
        public string EntryId { get; set; }
        public string Folder { get; set; }
        public string From { get; set; }
        public string Subject { get; set; }
        public string ConversationId { get; set; }
        public string Body { get; set; }
        public string To { get; set; }
        public Document ToDoc()
        {
            var doc = new Document();
            doc.Fields.Add(new Field("EntryId", this.EntryId, Webus.Documents.FieldAttributes.None));
            doc.Fields.Add(new Field("Folder", this.Folder, Webus.Documents.FieldAttributes.Index));
            doc.Fields.Add(new Field("From", this.From, Webus.Documents.FieldAttributes.Index));
            doc.Fields.Add(new Field("Subject", this.Subject, Webus.Documents.FieldAttributes.AnalyseIndex));
            doc.Fields.Add(new Field("ConversationId", this.ConversationId, Webus.Documents.FieldAttributes.Index));
            doc.Fields.Add(new Field("Body", this.Body, Webus.Documents.FieldAttributes.AnalyseIndex));
            doc.Fields.Add(new Field("To", this.To, Webus.Documents.FieldAttributes.Index));
            return doc;
        }
        public MailInfo()
        {

        }
        public MailInfo(Document doc)
        {
            this.EntryId = doc.GetField("EntryId").Value.ToString();
            this.Folder = doc.GetField("Folder").Value.ToString();
            this.From = doc.GetField("From").Value.ToString();
            this.Subject = doc.GetField("Subject").Value.ToString();
            this.ConversationId = doc.GetField("ConversationId").Value.ToString();
            this.Body = doc.GetField("Body").Value.ToString();
            this.To = doc.GetField("To").Value.ToString();
        }
    }
}

标签: 邮件 C# OutLook 邮箱 c

实例下载地址

分享,C#实现OutLook邮箱邮件检索

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警