.Net邮件收发类库LumiSoft.Net

  • 开源协议:未知
  • 操作系统: Windows Linux OS X
  • 开发语言:C#
  • 项目所有者:pruiz
  • 收录时间:2017-05-21
  • 分享:
编辑评级
3

项目详细介绍

Lumisoft.NET这个非常优秀的开源组件,用该组件来设计开发邮件工具,将变得更加方便,功能更加强大。Lumisoft.NET组件除了提供邮件发送功能外,还提供了邮件接收等功能的处理(包括基于POP3协议和IMAP协议)。网上很多文章基本介绍如何使用该组件来收取邮件较多,较少介绍使用该组件做邮件发送功能的。本文主要探寻使用该组件实现邮件的发送功能,邮件发送有两种方式,一种是不用发件人即可发送邮件,一种是使用发件人账户密码和SMTP服务器来实现邮件发送的,本文分别对这两种方式进行介绍。

组件下载地址:http://www.lumisoft.ee/lswww/download/downloads/ 

组件论坛地址:http://www.lumisoft.ee/Forum/default.aspx?g=forum 

 

示例代码:

发送邮件:

    using LumiSoft.Net.SMTP.Client;  
      
      
    Mime m = new Mime();  
    MimeEntity mainEntity = m.MainEntity;  
    // Force to create From: header field  
    mainEntity.From = new AddressList();  
    mainEntity.From.Add(new MailboxAddress(txtFrom.Text, txtFrom.Text));  
     // Force to create To: header field  
     mainEntity.To = new AddressList();  
     mainEntity.To.Add(new MailboxAddress(txtTo.Text, txtTo.Text));  
    mainEntity.Subject = txtSubject.Text;  
    //添加正文  
    mainEntity.ContentType = MediaType_enum.Multipart_mixed;  
    MimeEntity textEntity = mainEntity.ChildEntities.Add();  
    textEntity.ContentType = MediaType_enum.Text_html;  
    textEntity.ContentTransferEncoding = ContentTransferEncoding_enum.Base64;  
    textEntity.DataText = txtBody.Text;  
    //发送附件  
      MimeEntity attachmentEntity = new MimeEntity();  
      attachmentEntity.ContentType = MediaType_enum.Application_octet_stream;  
      attachmentEntity.ContentDisposition = ContentDisposition_enum.Attachment;  
      attachmentEntity.ContentTransferEncoding = ContentTransferEncoding_enum.Base64;  
      attachmentEntity.ContentDisposition_FileName = "c:/test.jpg";  
      attachmentEntity.DataFromFile("c:/test.jpg");  
          
      mainEntity.ChildEntities.Add(attachmentEntity);  
          
      SMTP_Client.QuickSend(m);  

 

收邮件:

       public List<Mime> GetEmails(string pop3Server, string pop3Port, string username, string password)  
            {  
                bool pop3UseSsl = false;  
                List<string> gotEmailIds = new List<string>();  
                List<Mime> result = new List<Mime>();  
                using (POP3_Client pop3 = new POP3_Client())  
                {  
                    try  
                    {  
                        pop3.Connect(pop3Server, Convert.ToInt32(pop3Port), pop3UseSsl);  
                        pop3.Authenticate(username, password, false);  
                        POP3_ClientMessageCollection infos = pop3.Messages;  
                        foreach (POP3_ClientMessage info in infos)  
                        {  
                            if (gotEmailIds.Contains(info.UID))  
                                continue;  
                            byte[] bytes = info.MessageToByte();  
                            gotEmailIds.Add(info.UID);  
                            Mime mime = Mime.Parse(bytes);  
                            result.Add(mime);  
                        }  
                    }  
                    catch (Exception ex)  
                    {  
                        throw new Exception(ex.Message);  
                    }  
                }  
                return result;  
            }  

删除邮件:

    
using LumiSoft.Net.POP3.Client;  
using LumiSoft.Net;  
 using LumiSoft.Net.Mime;  
private void DeleteMail()  
            {  
                using (POP3_Client c = new POP3_Client())  
                {  
                    c.Connect(pop3Server, Convert.ToInt32(pop3Port));  
                    c.Authenticate(username, password, false);  
                    if (c.Messages.Count > 0)  
                    {  
                        foreach (POP3_ClientMessage mail in c.Messages)  
                        {  
                            mail.MarkForDeletion();  
                        }  
                    }  
                }  
            }  

 

标签:lumisoft