1、设置邮件的pop3
以163网易邮箱为例:
https://jingyan.baidu.com/article/adc81513804be9f722bf7350.html
2、下载OpenPop.dll
官网下载地址:http://sourceforge.net/projects/hpop/
3、读取设置pop3邮箱的所有邮件内容:
public class Pop3Helper
{
public static void ReadPop3()
{
using (Pop3Client client = new Pop3Client())
{
if (client.Connected)
{
client.Disconnect();
}
// Connect to the server, false means don't use ssl
client.Connect("pop3.163.com", 110, false);
// Authenticate ourselves towards the server by email account and password
client.Authenticate("xxx@163.com", "pop3授权码");
//email count
int messageCount = client.GetMessageCount();
for (int i = 1; i <= messageCount; i++)
{
Message message = client.GetMessage(i);
NameValueCollection customHeaders = message.Headers.UnknownHeaders;
string sender = message.Headers.From.DisplayName;
string from = message.Headers.From.Address;
string subject = message.Headers.Subject;
DateTime Datesent = message.Headers.DateSent;
MessagePart messagePart = message.MessagePart;
GetCustomInfo(customHeaders);
//email body
string body = " ";
if (messagePart.IsText)
{
body = messagePart.GetBodyAsText();
}
else if (messagePart.IsMultiPart)
{
MessagePart plainTextPart = message.FindFirstPlainTextVersion();
MessagePart htmlTextPart = message.FindFirstHtmlVersion();
if (htmlTextPart != null)
{
body = htmlTextPart.GetBodyAsText();
}
if (plainTextPart != null)
{
body = plainTextPart.GetBodyAsText();
}
else
{
List<MessagePart> textVersions = message.FindAllTextVersions();
if (textVersions.Count >= 1)
body = textVersions[0].GetBodyAsText();
else
body = "<<OpenPop>> Cannot find a text version body in this message.";
}
}
}
}
}
//获取自定义字段内容
private static void GetCustomInfo(NameValueCollection col)
{
if (col != null && col.Count > 0)
{
foreach (var cHeaderKey in col.AllKeys)
{
switch (cHeaderKey)
{
case "X-CamId":
Console.WriteLine(string.Format("CamId:{0}", col[cHeaderKey]));
break;
case "X-SubId":
Console.WriteLine(string.Format("SubId:{0}", col[cHeaderKey]));
break;
case "X-UserId":
Console.WriteLine(string.Format("UserId:{0}", col[cHeaderKey]));
break;
default:
break;
}
}
}
}
}