C#接收邮件
来源:互联网 发布:cad2007软件自学网 编辑:程序博客网 时间:2024/06/12 01:38
最近由于工作需要,接触到了邮件服务器.以前我用CF写过,感觉没有什么,我想用C#来实现会更简单,但是万万没想到C#没有提供邮件接收的方法,令我很不解.通过我在网上查找,发现了一个国外公司写好的接收邮件软件,通过我把他的DLL破解之后就不用注册了,要不可是需要花钱买序列号的啊好贵的啊!不过我希望各位如果有能力还是去买正版的为好!我破解他的确是没有办法的办法拉!请各位能够理解^_^
下面是接收邮件的主程序代码如下:
using System;
using System.Text;
using System.IO;
using Email.POP3;
namespace TestPOP3
...{
class example
...{
[STAThread]
static void Main(string[] args)
...{
//我测试的时候用的是163的邮箱,163的免费POP邮件服务器是pop.163.com。而163官方给出的是
//pop.126.com在这里不能用,原因是这个邮件服务器是有SSL加密的,GMAIL我也测试了也不能用都是这个原因
POP3 objPOP3 = new POP3("pop.163.com", 110, "用户名", "密码");
Console.WriteLine(objPOP3.Connect() ? "Connected" : "Can't connect");
try
...{
if (objPOP3.IsAPOPSupported)
...{
Console.WriteLine(objPOP3.SecureLogin() ? "Secure Logged in" : "Can't login");
}
else
...{
Console.WriteLine(objPOP3.Login() ? "Logged in" : "Can't login");
}
objPOP3.QueryServer();
Console.WriteLine("Emails count: " + objPOP3.TotalMailCount);
//以下的FOR循环是显示出所有收件箱里面的邮件信息
for (int i = 1; i <= objPOP3.TotalMailCount; i++)
...{
EmailMessage objEmail = objPOP3.GetMessage(i, false); // use true to get headers only
Console.WriteLine("NEW MESSAGE:------------------");
Console.WriteLine("FROM: " + objEmail.From);
Console.WriteLine("TO: " + objEmail.To);
Console.WriteLine("CC: " + objEmail.Cc);
Console.WriteLine("SUBJECT: " + objEmail.Subject);
Console.WriteLine("DATE: " + objEmail.Date);
Console.WriteLine("CONTENT-TYPE: " + objEmail.ContentType);
Console.WriteLine("CHARSET: " + objEmail.Charset);
Console.WriteLine("MESSAGE-ID: " + objEmail.GetCustomHeader("Message-ID"));
Console.WriteLine("MESSAGE SIZE: " + objEmail.Size);
if (objEmail.IsAnyAttachments)
...{
for (int a = 0; a < objEmail.Attachments.Count; a++)
...{
//调用邮件附件的方法
processAttachment((Attachment)objEmail.Attachments[a], 1);
}
}
else
...{
Console.WriteLine("BODY: " + Encoding.Default.GetString(Convert.FromBase64String(objEmail.Body)));
}
//下面注册掉的代码是删除该邮件
//objPOP3.DeleteMessage(i);
}
objPOP3.Close();
}
catch (System.Exception e)
...{
Console.WriteLine(e.Message);
Console.ReadLine();
objPOP3.Close();
return;
}
}
static void processAttachment(Attachment att, int nesting)
...{
for(int i = 0; i < nesting * 2; i++) Console.Write("-");
//以下注释掉的代码可以打开,以下都是关于邮件附件的相关信息,因为我只需要得到附件的文件信息^_^
//Console.WriteLine("ATT: ");
//Console.WriteLine("ContentTransferEncoding: " + att.ContentTransferEncoding);
//Console.WriteLine("ContentType: " + att.ContentType);
//Console.WriteLine("EstimatedSize: " + att.EstimatedSize);
//Console.WriteLine("FileName: " + att.FileName);
//processBody("HtmlBody", att.HtmlBody);
//processBody("TextBody", att.TextBody);
//Console.WriteLine("IsAnyAttachments: " + att.IsAnyAttachments);
//Console.WriteLine("IsFileAttachment: " + att.IsFileAttachment);
if (att.IsAnyAttachments)
...{
for (int a = 0; a < att.Attachments.Count; a++)
...{
processAttachment((Attachment)att.Attachments[a], nesting * 2);
}
}
if(att.IsFileAttachment)
...{
//这里说一下在保存邮件附件之前必须"c:/pop3"该文件夹是存在的,否则是保存不了的
att.Save(@"c:/pop3" + att.FileName);
Console.WriteLine("附件保存成功!附件名称为:" + att.FileName);
}
}
static void processBody(string bodytype, string body)
...{
if (body == null)
...{
Console.WriteLine(bodytype + ": null");
return;
}
if (body.Length > 1000)
...{
Console.WriteLine(bodytype + ": " + body.Substring(0, 1000) + "...");
}
else
...{
Console.WriteLine(bodytype + ": " + body);
}
}
}
}
using System.Text;
using System.IO;
using Email.POP3;
namespace TestPOP3
...{
class example
...{
[STAThread]
static void Main(string[] args)
...{
//我测试的时候用的是163的邮箱,163的免费POP邮件服务器是pop.163.com。而163官方给出的是
//pop.126.com在这里不能用,原因是这个邮件服务器是有SSL加密的,GMAIL我也测试了也不能用都是这个原因
POP3 objPOP3 = new POP3("pop.163.com", 110, "用户名", "密码");
Console.WriteLine(objPOP3.Connect() ? "Connected" : "Can't connect");
try
...{
if (objPOP3.IsAPOPSupported)
...{
Console.WriteLine(objPOP3.SecureLogin() ? "Secure Logged in" : "Can't login");
}
else
...{
Console.WriteLine(objPOP3.Login() ? "Logged in" : "Can't login");
}
objPOP3.QueryServer();
Console.WriteLine("Emails count: " + objPOP3.TotalMailCount);
//以下的FOR循环是显示出所有收件箱里面的邮件信息
for (int i = 1; i <= objPOP3.TotalMailCount; i++)
...{
EmailMessage objEmail = objPOP3.GetMessage(i, false); // use true to get headers only
Console.WriteLine("NEW MESSAGE:------------------");
Console.WriteLine("FROM: " + objEmail.From);
Console.WriteLine("TO: " + objEmail.To);
Console.WriteLine("CC: " + objEmail.Cc);
Console.WriteLine("SUBJECT: " + objEmail.Subject);
Console.WriteLine("DATE: " + objEmail.Date);
Console.WriteLine("CONTENT-TYPE: " + objEmail.ContentType);
Console.WriteLine("CHARSET: " + objEmail.Charset);
Console.WriteLine("MESSAGE-ID: " + objEmail.GetCustomHeader("Message-ID"));
Console.WriteLine("MESSAGE SIZE: " + objEmail.Size);
if (objEmail.IsAnyAttachments)
...{
for (int a = 0; a < objEmail.Attachments.Count; a++)
...{
//调用邮件附件的方法
processAttachment((Attachment)objEmail.Attachments[a], 1);
}
}
else
...{
Console.WriteLine("BODY: " + Encoding.Default.GetString(Convert.FromBase64String(objEmail.Body)));
}
//下面注册掉的代码是删除该邮件
//objPOP3.DeleteMessage(i);
}
objPOP3.Close();
}
catch (System.Exception e)
...{
Console.WriteLine(e.Message);
Console.ReadLine();
objPOP3.Close();
return;
}
}
static void processAttachment(Attachment att, int nesting)
...{
for(int i = 0; i < nesting * 2; i++) Console.Write("-");
//以下注释掉的代码可以打开,以下都是关于邮件附件的相关信息,因为我只需要得到附件的文件信息^_^
//Console.WriteLine("ATT: ");
//Console.WriteLine("ContentTransferEncoding: " + att.ContentTransferEncoding);
//Console.WriteLine("ContentType: " + att.ContentType);
//Console.WriteLine("EstimatedSize: " + att.EstimatedSize);
//Console.WriteLine("FileName: " + att.FileName);
//processBody("HtmlBody", att.HtmlBody);
//processBody("TextBody", att.TextBody);
//Console.WriteLine("IsAnyAttachments: " + att.IsAnyAttachments);
//Console.WriteLine("IsFileAttachment: " + att.IsFileAttachment);
if (att.IsAnyAttachments)
...{
for (int a = 0; a < att.Attachments.Count; a++)
...{
processAttachment((Attachment)att.Attachments[a], nesting * 2);
}
}
if(att.IsFileAttachment)
...{
//这里说一下在保存邮件附件之前必须"c:/pop3"该文件夹是存在的,否则是保存不了的
att.Save(@"c:/pop3" + att.FileName);
Console.WriteLine("附件保存成功!附件名称为:" + att.FileName);
}
}
static void processBody(string bodytype, string body)
...{
if (body == null)
...{
Console.WriteLine(bodytype + ": null");
return;
}
if (body.Length > 1000)
...{
Console.WriteLine(bodytype + ": " + body.Substring(0, 1000) + "...");
}
else
...{
Console.WriteLine(bodytype + ": " + body);
}
}
}
}
- C#接收邮件
- c#接收邮件类
- C#接收邮件函数
- C#接收邮件
- C#接收邮件
- C#接收邮件
- C#邮件发送和接收
- C# 利用Jmail接收邮件
- C# 利用Jmail接收邮件
- C#邮件发送和接收
- C# 利用Jmail接收邮件
- c# 利用Jmail接收邮件
- C# 利用Jmail接收邮件
- C# 利用Jmail接收邮件
- (转载)用pop3 c#接收邮件
- C#实现POP3邮件接收程序
- jmail + c#实现邮件的接收
- C#邮件接收系统核心代码(接收POP3邮件IMAP邮件)
- 完美夏天
- 牛人十个月自学C++ 现在做C#开发工作
- USB设备驱动程序导读(8)
- 《J2EE核心模式》(DAO模式)
- USB设备驱动程序导读(9)
- C#接收邮件
- 数据库应用_碰到数据库日志非常大的情况
- 淘客联盟系统维护光盘2008新春大礼包
- 《JAVA编程思想》学习备忘(第155页:Initialization & Cleanup)-4
- 追MM与Java的23种设计模式
- 第一天使用新帐号,试试
- .NET 自定义Button控件(二)
- 对计算机科学的反思
- 回味