pop3可以发也可以收也比较简单就是功能不强大 它还需要注册到计算机
收取的时候返回的是全部数据 getunreadmessage()不支持了
imap 引用LumiSoft.Net.Dll 就行 不需要注册到计算机 dll下载 https://files.cnblogs.com/0banana0/LumiSoft.Net.rar
获取未读(有bug)
/// <summary> /// 获取未读邮件 /// </summary> /// <param name="mailServer">邮件服务器地址</param> /// <param name="username">邮箱用户名</param> /// <param name="password">邮箱密码</param> /// GetUnReadMails("smtp.exmail.qq.com", "****", "****"); public static void GetUnReadMails(string mailServer, string username, string password) { IMAP_Client IMAPServer = new IMAP_Client(); try { //连接邮件服务器通过传入邮件服务器地址和用于IMAP协议的端口号 IMAPServer.Connect(mailServer, 143); //登陆邮箱 IMAPServer.Authenticate(username, password); IMAPServer.SelectFolder("INBOX"); IMAP_SequenceSet set = new IMAP_SequenceSet(); int count1 = IMAPServer.GetUnseenMessagesCount();//邮件未读的条数 int count2 = IMAPServer.MessagesCount;//邮件总数 set.Parse(string.Format("{0}:{1}", count1, count2));//获取未读 IMAP_FetchItem[] items = IMAPServer.FetchMessages(set, IMAP_FetchItem_Flags.Envelope | IMAP_FetchItem_Flags.Header | IMAP_FetchItem_Flags.Message | IMAP_FetchItem_Flags.MessageFlags | IMAP_FetchItem_Flags.UID, true, false); foreach (IMAP_FetchItem item in items) { if (item.MessageData == null) continue; byte[] data = item.MessageData; string body = System.Text.Encoding.Default.GetString(data); } } catch (Exception ex) { throw ex; } finally { IMAPServer.Disconnect(); } }
发邮件
static public void SendMail() { string sendTo="111@qq.com"; string sendCc = "222@qq.com"; string subject = "subjrct"; string body="body"; string accountName = "333@qq.com"; string password = "chwl1234"; string smtpServer = "smtp.exmail.qq.com"; int smtpPort = 143; //.net smtp System.Web.Mail.MailMessage mailmsg = new System.Web.Mail.MailMessage(); mailmsg.To = sendTo; mailmsg.Cc = sendCc; //mailmsg.Cc = cc; mailmsg.Subject = subject; mailmsg.Body = body; //附件地址 string Attachment = @"F:\\文档\\dll\\LumiSoft.Net.dll"; //附件处理 mailmsg.Attachments.Add(new System.Web.Mail.MailAttachment(Attachment)); //sender here mailmsg.From = accountName; // certify needed mailmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");//1 is to certify //the user id mailmsg.Fields.Add( "http://schemas.microsoft.com/cdo/configuration/sendusername", accountName); //the password mailmsg.Fields.Add( "http://schemas.microsoft.com/cdo/configuration/sendpassword", password); System.Web.Mail.SmtpMail.SmtpServer =smtpServer; try { System.Web.Mail.SmtpMail.Send(mailmsg); } catch(Exception ex) { }