在项目中,我们需要监听邮件服务器,看是否有新的邮件进入。下面的代码可以帮助我们监听新邮件,并对已有的邮件进行查找:

using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Outlook; namespace EmailReceiver
{
public class EmailAnalyzer
{
static ApplicationClass outlookApp = new Microsoft.Office.Interop.Outlook.ApplicationClass();
static NameSpace ns; static EmailAnalyzer()
{
ns = outlookApp.GetNamespace("MAPI");
} public static void Start()
{
AnalyzeNewItem();
} public static List<MailItem> GetMailItemsWithInFolders(Folders folders, string folderName)
{
List<MailItem> allmails = new List<MailItem>();
if (folders != null)
{
int foldersCount = folders.Count; for (int i = 1; i <= foldersCount; i++)
{
var subFolder = folders[i];
if (!subFolder.Name.Equals(folderName, StringComparison.InvariantCultureIgnoreCase))
{
continue;
} foreach (var item in subFolder.Items)
{
if (item is MailItem)
{
var mail = item as MailItem;
allmails.Add(mail);
}
}
}
}
return allmails;
} public static List<Folders> GetUnderlyingFolders(Folders inputFolder)
{
List<Folders> allFolders = new List<Folders>();
if (inputFolder != null)
{
int foldersCount = inputFolder.Count;
if (foldersCount > 0)
{
for (int i = 1; i <= foldersCount; i++)
{
var subFolder = inputFolder[i];
allFolders.AddRange(GetUnderlyingFolders(subFolder.Folders));
}
}
else
{
allFolders.Add(inputFolder);
}
}
return allFolders;
} public static List<Folders> GetUnderlyingFolders(Folders inputFolder, string folderName)
{
List<Folders> allFolders = new List<Folders>();
if (inputFolder != null)
{
int foldersCount = inputFolder.Count;
if (foldersCount > 0)
{
for (int i = 1; i <= foldersCount; i++)
{
var subFolder = inputFolder[i];
if (subFolder.Name.Equals(folderName, StringComparison.InvariantCultureIgnoreCase))
{
allFolders.AddRange(GetUnderlyingFolders(subFolder.Folders));
}
}
}
else
{
allFolders.Add(inputFolder);
}
}
return allFolders;
} public static List<MailItem> GetUnderlyingItems(string folderName = "")
{
var inbox = ns.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
return GetUnderlyingItems(inbox.Folders, !string.IsNullOrEmpty(folderName) ? null : inbox.Items, folderName);
} private static List<MailItem> GetUnderlyingItems(Folders inputFolder, Items items, string folderName = "")
{
List<MailItem> allItems = new List<MailItem>();
if (inputFolder != null)
{
if (items != null)
{
foreach (var item in items)
{
if (item is MailItem)
{
var mail = item as MailItem;
allItems.Add(mail);
}
}
} int foldersCount = inputFolder.Count;
if (foldersCount > 0)
{
for (int i = 1; i <= foldersCount; i++)
{
var subFolder = inputFolder[i];
if (!string.IsNullOrEmpty(folderName))
{
if (folderName.Equals(subFolder.Name, StringComparison.InvariantCultureIgnoreCase))
{
foreach (var item in subFolder.Items)
{
if (item is MailItem)
{
var mail = item as MailItem;
allItems.Add(mail);
}
}
}
else
{
allItems.AddRange(GetUnderlyingItems(subFolder.Folders, null, folderName));
}
}
else
{
allItems.AddRange(GetUnderlyingItems(subFolder.Folders, subFolder.Items, folderName));
}
}
} } return allItems;
} private static void AnalyzeNewItem()
{
var allItems = GetUnderlyingItems(""); var matchedItems = allItems.Where(s => s.Subject != null && s.Subject.ToLower().Contains("")).ToList();
var count = matchedItems.Count;
foreach (MailItem item in matchedItems)
{
Console.WriteLine(item.Subject);
Console.WriteLine(item.To);
Console.WriteLine(item.SenderName);
Console.WriteLine(item.ReceivedTime);
//Console.WriteLine(item.Body);
} Console.ReadLine();
}
}
}

同时附上常用的邮件发送组件:

public static class MailHelper
{
private const string SMTPHost = "mail.pimco.com";
private const string FromAdress = "hailongli@pimco.com";
private const string FromDisplay = "Hailong LI";
private static readonly MailAddress From;
private static List<string> toList = new List<string>(); static MailHelper()
{
From = new MailAddress(FromAdress, FromDisplay);
toList.Clear();
toList.Add(FromAdress);
} public static void SendToMyself(string subject, string body)
{
MailHelper.Send(new List<string>(), null, subject, body, null);
} private static void Send(List<string> to, string subject, string body, List<System.Net.Mail.Attachment> attachments)
{
MailHelper.Send(to, null, subject, body, attachments);
} private static void Send(List<string> to, List<string> cc, string subject, string body, List<System.Net.Mail.Attachment> attachments)
{
if (to == null || to.Count == )
{
var ex = new ArgumentNullException("must assign a receiver.");
return;
} MailMessage message = new MailMessage { Subject = subject, Body = body };
message.From = From;
to.ForEach(t => message.To.Add(t));
if (cc != null)
{
cc.ForEach(c => message.CC.Add(c));
} if (attachments != null)
{
attachments.ForEach(attachemet => message.Attachments.Add(attachemet));
} SmtpClient client = new SmtpClient(SMTPHost);
client.UseDefaultCredentials = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.SendCompleted += delegate(object sender, AsyncCompletedEventArgs e)
{
SmtpClient smtpClient = sender as SmtpClient;
if (smtpClient != null)
{
smtpClient.Dispose();
}
}; client.SendAsync(message, null);
}
}

最新文章

  1. Fedora javac 命令提示 [javac: 未找到命令...]
  2. bash 取文件特定行
  3. LabVIEW串口通信
  4. 有用的.NET库
  5. java的nio之:java的nio系列教程之pipe
  6. ACE_linux:读写锁
  7. OC - 19.GCD
  8. JSON基础学习
  9. OAuth做webapi认证
  10. Codeforces Round #384 (Div. 2).C
  11. Vijos P1786 质因数分解【暴力】
  12. 【原创】源码角度分析Android的消息机制系列(四)——MessageQueue的工作原理
  13. ES6常用语法(上)
  14. Vue(二)基础
  15. switch变种玩法
  16. HDU 3966 Aragorn&#39;s Story(模板题)【树链剖分】+【线段树】
  17. 目标检测(六)YOLOv2__YOLO9000: Better, Faster, Stronger
  18. LiveBindings --- 把对象之间的属性绑定起来
  19. springboot maven 部署
  20. ACM-选人问题(救济金发放)

热门文章

  1. blkid找不到需要的uuid
  2. 记一次安装kolla遇到DockerException: Error while fetching server API version: Timeout value connect was Timeout的问题
  3. b,u,i,s,这些被删除的标签以及用来替换他们的标签
  4. findControl 可以获取前台页面的控件
  5. Unity---编辑器扩展---更新中
  6. 状压DP 【洛谷P3694】 邦邦的大合唱站队
  7. MySQL中join的用法
  8. vue.js路由嵌套传参
  9. Charles使用方法简介
  10. import与from...import...的区别