使用开源的LumiSoft.Net,对邮件进行收发

1.前言

 首先呢我不是太喜欢收钱的东西,一来这个东西我挣不了钱,二来不容易改进。所有就找了个开源的收发邮件的组件LumiSoft.Net。大家可以网上搜索相关资料。

2.通过POP3协议获取服务器上所有邮件

 using(POP3_Client pop=new POP3_Client())
{
pop.Connect(popser.ServerAddress,popser.Port);//通过POP3地址,端口连接服务器。
pop.Authenticate(popser.Account,MailDecry.DecryptStringByDES(popser.Password),true);//登录之后验证用户的合法性
foreach(POP3_ClientMessage Msg in pop.Messages)//遍历获取到的邮件实体
{
Mail_Message m=Mail_Message.ParseFromByte(Msg.MessageToByte()); //想真正用邮件实体的话,还要类型转换一下

Entity.EMail mail = FillMailEntity(m,Msg.Size);//填充本地的实体类
mail.POP = popser;
mail.POP.Password = MailDecry.EncryptStringByDES(mail.POP.Password);
mail.Owner=Owner;
foreach(MIME_Entity att in m.Attachments)//获取邮件中的附件
{
FillMailEntitysAtt(att, mail);
}
Msg.MarkForDeletion();//删除服务器上的邮件 如果需要可以删除服务器上的邮件
}
}

 

3.通过SMTP协议发送邮件

MemoryStream ms = new MemoryStream();//定义一个数据流管道
CreateMailMessage(email).ToStream(ms, new MIME_Encoding_EncodedWord(MIME_EncodedWordEncoding.Q, Encoding.UTF8), Encoding.UTF8);
//CreateMailMessage(email)返回一个Mail.Message实体
ms.Position = 0;

SMTP_Client.QuickSendSmartHost(null, SMTPServerEntity.ServerAddress, SMTPServerEntity.Port, false, SMTPServerEntity.Account, MailDecry.DecryptStringByDES(SMTPServerEntity.Password), email.Sender, email.Accepter.Split(','), ms);

这个我们就通过SMTP发送了一条邮件到目的地址

4.总结

代码很简单,费了半天的时间去找和编写代码。希望给能用到的人一点帮助。我在163服务器上测试通过,不过偶尔会有乱码的问题。这个还有待解决。

posted @ 2011-11-08 17:42  David.You  阅读(1660)  评论(0编辑  收藏  举报