C# POP3、IMAP、SMTP收发邮件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Lesnikowski.Client;
using Lesnikowski.Client.IMAP;
using Lesnikowski.Mail;
using Lesnikowski.Mail.Fluent;
using Lesnikowski.Mail.Headers;
using Lesnikowski.Mail.Headers.Constants;

namespace Email
{
    public class Class1
    {
       public void POP3_Subject_Message(String User, String Password,String Server)
       {
          Pop3 pop3 = new Pop3();
          pop3.Connect(Server);              
          pop3.User = User;
          pop3.Password = Password;
          pop3.Login();
          pop3.GetAccountStat();                      
          SimpleMailMessageBuilder builder = new SimpleMailMessageBuilder();
          for (int i = 1; i <= pop3.MessageCount; i++)
          { ISimpleMailMessage mail = builder.CreateFromEml(pop3.GetMessage(i));
            Console.WriteLine(mail.Subject);
          }
           pop3.Close(true);
        }

       public void IMAP_Subject_Message(String User, String Password, String Server)
       {
           Imap imap = new Imap();
           imap.Connect(Server);

           imap.User = User;
           imap.Password = Password;
           imap.Login();

           imap.SelectInbox();
           List<long> uidList = imap.SearchFlag(Flag.Unseen);
           foreach (long uid in uidList)
           {
               ISimpleMailMessage mail = new SimpleMailMessageBuilder()
                   .CreateFromEml(imap.GetMessageByUID(uid));
               Console.WriteLine(mail.Subject);
           }
           imap.Close(true);
        }

       public void SMTP_Send_Message(String User, String Password, String Server)
       {
           ISimpleMailMessage message = Mail
           .Html(@"Html with an image: <img src=""cid:lena"" />")
           .AddVisual(@"c:\lena.jpeg").SetContentId("lena")
           .AddAttachment(@"c:\tmp.doc").SetFileName("document.doc")
           .To("to@mail.com")
           .From("from@mail.com")
           .Subject("Subject")
           .Create();
           Smtp smtp = new Smtp();
           smtp.Connect(Server);
           smtp.Ehlo(HeloType.EhloHelo, "yourname");

           smtp.User = User;
           smtp.Password = Password;
           smtp.Login();

           smtp.SendMessage(message);

           smtp.Close(true);   
       }
     }
}


  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值