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

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);
}
}

C#:新邮件监听及搜索的更多相关文章

  1. vue 前端处理监听关键字搜索

    根据组件的业务需要,有时候搜索是把关键字返回给后台,后台处理后再把数据返回给前端渲染(多次请求服务器):有时候是前端把页面的数据全部获取下来,前端处理关键字的搜索(影响页面加载) 我这个文章是介绍第二 ...

  2. H5新特性监听手机的返回键

    var hiddenProperty ='hidden' in document ? 'hidden' :'webkitHidden' in document ? 'webkitHidden' : ' ...

  3. eas中删除原来的监听事件添加新的监听事件

    kdtEntrys.removeKDTEditListener(kdtEntrys.getListeners(KDTEditListener.class)[0]);  kdtEntrys.addKDT ...

  4. ORACLE清理、截断监听日志文件(listener.log)

    在ORACLE数据库中,如果不对监听日志文件(listener.log)进行截断,那么监听日志文件(listener.log)会变得越来越大,想必不少人听说过关于"LISTENER.LOG日 ...

  5. 涂抹Oracle笔记1-创建数据库及配置监听程序

    一.安装ORACLE数据库软件及创建实例OLTP:online transaction processing 指那些短事务,高并发,读写频繁的数据库系统.--DB_BLOCK_SIZE通常设置较小.O ...

  6. ORACLE监听配置及测试实验(2)

    实验四 在tnsname.ora里添加默认监听代号 [oracle@oracle01 admin]$ vi tnsnames.ora 添加一行 PORT1528=(ADDRESS = (PROTOCO ...

  7. 为什么有监听socket和连接socket,为什么产生两个socket

    为什么有监听socket和连接socket,为什么产生两个socket 先看一半的socket建立连接的双方的过程: 客户端: socket()---->创建出 active_socket_fd ...

  8. 11g R2 rac linstener 监听配置

    两个节点host,ipvip ,scan的信息 #eth0-Public IP 162.12.0.1    cqltjcpt1 162.12.0.3    cqltjcpt2 #eth1 PRIVAT ...

  9. linux epoll机制对TCP 客户端和服务端的监听C代码通用框架实现

    1 TCP简介 tcp是一种基于流的应用层协议,其“可靠的数据传输”实现的原理就是,“拥塞控制”的滑动窗口机制,该机制包含的算法主要有“慢启动”,“拥塞避免”,“快速重传”. 2 TCP socket ...

随机推荐

  1. Java50道经典习题-程序32 左移右移

    题目:取一个整数a从右端开始的4-7位.分析:比如取123456789从右端开始的4-7位即:3456(1)先使a右移4位.(2)设置一个低4位全为1,其余全为0的数.可用~(~0<<4) ...

  2. PDG科普篇

    作者:马健邮箱:stronghorse_mj@hotmail.com发布:2009.09.26 更新历史 2014.11.11补充了文字版PDG的部分内容增加CX PDF等打包格式的相关内容 2009 ...

  3. 遍历一个二维数组的简便方法(减少foreach次数)

    在一些特定场合可以使用下, 还是有局限性 输出结果 : 另一种场景 : 输出结果 : 更复杂的场景 : 输出结果 :

  4. c#写对象来读取TXT文本文件

    本博文让你知道怎样写对象,怎样读取文本文件,怎样根据实际条件判断与获取需要的文本行.参考下面网友的问题,根据源文来看,有些行输出的格式,需要把“,”替换为空格. 第一行还附加入后面的子行每一行的后面, ...

  5. javascript 实现类似百度联想输入,自动补全功能

    js  实现类似百度联想输入,自动补全功能 方案一: search是搜索框id="search" //点击页面隐藏自动补全提示框 document.onclick = functi ...

  6. 生成全局唯一ID

    在实际业务处理中,有时需要生成全局唯一ID来区别同类型的不同事物,介绍一下几种方式及其C++实现 //获取全局唯一ID //server_id为服务的id,因当同一个服务部署在多个服务器上时,需要区别 ...

  7. Java与其它语言的比较

    Java与C/C++相比.Java语言是一种完全的面对对象语言,虽然他的底层(运行时库)是用C语言开发 的,可是并不依赖于C.因为Java的运行是在运行时库的支持下运行的,所以运行的效率比起可以更接近 ...

  8. BZOJ 1974 [Sdoi2010] auction 代码拍卖会(数位dp)

    题目描述 随着iPig在P++语言上的造诣日益提升,他形成了自己一套完整的代码库.猪王国想参加POI的童鞋们都争先恐后问iPig索要代码库.iPig不想把代码库给所有想要的小猪,只想给其中的一部分既关 ...

  9. jq表单验证

    <body> <form method="post" action=""> <div class="int"& ...

  10. Warning: Call to 'toArray()' with pre-sized array argument 'new String[list.size()]'

    当使用如下代码将List转换为Array类型时: List<String> list = new ArrayList<>(); String[] array = list.to ...