zoukankan      html  css  js  c++  java
  • IMAP收邮件

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using LumiSoft.Net.IMAP;
    using LumiSoft.Net.IMAP.Client;
    using System.IO;
    using LumiSoft.Net.Mail;
    using System.Net.Mime;
    using System.Net.Mail;
    using LumiSoft.Net.MIME;
    namespace EMailTest
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Handle(object sender, LumiSoft.Net.EventArgs<IMAP_r_u> e)
            {
                Console.WriteLine(e.Value.ToString());
            }
            private void button1_Click(object sender, EventArgs oe)
            {
                IMAP_Client client = new IMAP_Client();
    
                try
                {
                    //连接邮件服务器通过传入邮件服务器地址和用于IMAP协议的端口号
                    //SSL 993 Other 143
                    //client.Connect("imap.qq.com", 993, true);
                    //client.Login("4587405@qq.com", "*******");
    
                    client.Connect("imap.163.com", 143, false);
                    client.Login("ylx-1982@163.com", "*****");
                    client.GetFolders(null).ToList().ForEach(f => {
    
                        Console.WriteLine(f.FolderName);
                        var s = client.FolderStatus(f.FolderName);
                        s.ToList().ForEach(sIt => {
                            Console.WriteLine("总数:{0},未读:{1},最近{2}", sIt.MessagesCount, sIt.MessagesCount, sIt.UnseenCount);
                        });
    
                    });
                    
    
                
                    client.SelectFolder("INBOX");
                   
                    var seqSet = IMAP_t_SeqSet.Parse("1000:*");
                    var items = new IMAP_t_Fetch_i[]
                    {
                        new IMAP_t_Fetch_i_Envelope(),
                        new IMAP_t_Fetch_i_Uid(),
                        new IMAP_t_Fetch_i_Flags(),
                        new IMAP_t_Fetch_i_InternalDate(),
                        new IMAP_t_Fetch_i_Rfc822()
                    };
    
    
                    //Fetch 第一个参数false时seqSet有效
                    client.Fetch(false, seqSet, items, (s, e) =>
                    {
                        try
                        {
                            
                            var email = e.Value as IMAP_r_u_Fetch;
                            //using (var ctx = new DBTEntities())
                            //{
                            //    var ent = new T_EMail();
                            //    ent.Flags = email.Flags.Flags.ToString();
                            //    ent.ReceiveDate = email.InternalDate.Date;
                            //    ent.Subject = email.Envelope.Subject;
                            //    ent.UId = email.UID.UID;
                            //    ctx.T_EMail.AddObject(ent);
                            //    ctx.SaveChanges();
    
                            //}
                            Console.WriteLine(" ");
                            Console.WriteLine("标题:" + email.UID.UID +"," + email.InternalDate.Date   +"," + email.Envelope.Subject );
                            Console.WriteLine("------------内容------------------------");
    
                            if (email.Rfc822 != null)
                            {
                                email.Rfc822.Stream.Position = 0;
                                var mine = Mail_Message.ParseFromStream(email.Rfc822.Stream);
                                email.Rfc822.Stream.Close();
    
                                //Console.WriteLine(mine.BodyHtmlText);
                                //Console.WriteLine(mine.Body.MediaType);
                                if (mine.Attachments.Count() > 0)
                                {
                                    
                                    var list= mine.Attachments.ToList();
                                    foreach (var att in list)
                                    {
                                        var part = att.Body as MIME_b_SinglepartBase;
    
                                        string filename=@"C:xx" + att.ContentType.Param_Name;
                                        File.WriteAllBytes(filename, part.Data);
                                  
                                      
                                    }
                                   
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("Handle-Err:" + ex.Message);
                        }
                        
                    });
    
    
    
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                finally
                {
    
                }
    
    
    
            }
        }
    }
    View Code
      private void button2_Click(object sender, EventArgs e)
            {
                var list = new List<EmailAccount>();
                //list.Add(new EmailAccount() { Username = "doomguards@126.com", Password = "8888", SmtpHost = "smtp.126.com", SmtpPort = 25 });
                //list.Add(new EmailAccount() { Username = "ylx-1982@163.com", Password = "****", SmtpHost = "smtp.163.com", SmtpPort = 25 });
                list.Add(new EmailAccount() { Username = "wdfrog@hotmail.com", Password = "8888", SmtpHost = "smtp-mail.outlook.com", SmtpPort = 587,EnableSsl=true });//587,
                //list.Add(new EmailAccount() { Username = "4587405@qq.com", Password = "***", SmtpHost = "smtp.qq.com", SmtpPort = 25 });//
                foreach (var account in list)
                {
                    TestMail(account);
                    Console.WriteLine("完成:" + account);
                }
                Console.WriteLine("完成!");
            }
            private void TestMail(EmailAccount account)
            {
                string _to = "4587405@qq.com";
                string _from = account.Username;
                string _subject = "Using the new SMTP client.邮箱SMTP测试";
                string _body = @"//设置邮箱端口,pop3端口:110, smtp端口是:25  Using this new feature, you can send an e-mail message from an application very easily.";
                MailMessage message = new MailMessage();
                message.From = new MailAddress(_from);
                //可以利用MailMessage.To.Add方法增加要发送的邮件地址  
                message.To.Add(new MailAddress("1228953303@qq.com"));
                message.To.Add(new MailAddress(_to));
                message.Subject = _subject;
                message.Body = _body;
    
    
                #region 添加附件
                Attachment a = new Attachment(@"d:/x.jpg");
                message.Attachments.Add(a);
               // message.Attachments.Add(new Attachment(@"C:Documents and SettingsAdministrator桌面蓝牙串口—线缆噩梦终结者.docx"));
                #endregion
                //设置邮箱的地址或IP  
                using (SmtpClient client = new SmtpClient(account.SmtpHost, account.SmtpPort))
                {
                
                    //设置邮箱端口,pop3端口:110, smtp端口是:25   
                    client.EnableSsl = account.EnableSsl;
                    //设置超时时间  
                    client.Timeout = 0;
                    client.DeliveryMethod = SmtpDeliveryMethod.Network;
    
                    //要输入邮箱用户名与密码  
                    // client.SendCompleted += new SendCompletedEventHandler(client_SendCompleted);
                    client.Credentials = new NetworkCredential(account.Username, account.Password);
                    client.Send(message);
                    client.Dispose();
                    
                }
                
            }
    
            void client_SendCompleted(object sender, AsyncCompletedEventArgs e)
            {
                if (e.Error != null)
                {
                    Console.WriteLine(e.Error);
                }
            }
            public class EmailAccount
            {
                public string Username { get; set; }
                public string Password { get; set; }
                public string SmtpHost { get; set; }
                public int SmtpPort { get; set; }
                public bool EnableSsl { get; set; }
                public EmailAccount()
                {
                    EnableSsl = false;
                    SmtpPort = 25;
                }
                public override string ToString()
                {
                    return Username;
                }
    
            }
    View Code
  • 相关阅读:
    【2019/5/24】周进度报告
    第一阶段意见汇总以及改进
    【2019/5/18】周进度报告
    用户体验评价-搜狗输入法
    第二阶段冲刺1
    第十四周总结
    找水王
    第十三周总结
    程序员修炼之道-从小工到专家阅读笔记02
    第十二周总结
  • 原文地址:https://www.cnblogs.com/wdfrog/p/3935282.html
Copyright © 2011-2022 走看看