基本信息
源码名称:分享,C#实现OutLook邮箱邮件检索
源码大小:0.54M
文件格式:.zip
开发语言:C#
更新时间:2017-04-12
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
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();
}
}
}