c#QQ邮件编程学习(收发邮件)

本次c#实现邮件管理编程的目的是实现第三方邮件管理,邮箱基于QQ邮箱,发送邮件直接采用.NET自带的System.Net.Mail类,接收邮件采用第三方组件Lumisoft.Net。现将基本实现的接收邮件和发送邮件代码记录如下。

1.发送邮件:基于System.Net.Mail。

using System.Net.Mail;
  public void SendMail(string MailFrom, string MailTo, string MailPwd, string Mailtitle,string MailCon,string attachMentUrl)
        {
            SmtpClient client = new SmtpClient("smtp.qq.com");
            client.EnableSsl = true;
            client.UseDefaultCredentials = false;
            client.Credentials = new System.Net.NetworkCredential(MailTo + "@qq.com", MailPwd);

            MailAddress From = new MailAddress(MailFrom + "@qq.com", "fxy", Encoding.UTF8);
            MailAddress To = new MailAddress(MailTo + "@qq.com", "", Encoding.UTF8);

            MailMessage myMessage = new MailMessage(From,To);
            myMessage.Body = MailCon;
            myMessage.BodyEncoding = Encoding.UTF8;
            myMessage.Subject = Mailtitle;
            myMessage.SubjectEncoding = Encoding.UTF8;
            myMessage.IsBodyHtml = true;

            Attachment attachment = new Attachment(attachMentUrl);
            myMessage.Attachments.Add(attachment);

            try
            {
                client.Send(myMessage);
            }
            catch (InvalidOperationException e)
            { }
            catch (Exception e)
            { }
            finally
            {
                Console.ReadLine();
            }
        }

2.接收邮件:基于Lumisoft.Net(

using LumiSoft.Net.POP3.Client;
using LumiSoft.Net.Mail;
 public void GetEmails()
        {
            using (POP3_Client c = new POP3_Client())
            {
                c.Connect("pop.qq.com", 995, true);
                c.Login("[email protected]", "sjgqkszeqlcqgihc");
                if (c.Messages.Count > 0)
                {
                    for (var i = 0; i < c.Messages.Count; i++)
                    {
                        var t = Mail_Message.ParseFromByte(c.Messages[i].MessageToByte());
                        var from = t.From;
                        var to=t.To;
                        var date = t.Date;
                        var subject = t.Subject;
                        var bodyText=t.BodyText;
                    }
                    
                }
            }
        }

 

你可能感兴趣的:(c#QQ邮件编程学习(收发邮件))