/// <summary> /// 删除 /// </summary> /// <param name="account">配置</param> /// <param name="UID">UID</param> public static void Delete(MailAccount account, string UID) { try { using (POP3_Client pop3Client = new POP3_Client()) { pop3Client.Connect(account.POP3Host, account.POP3Port, false); pop3Client.Login(account.Account, account.Password); if (pop3Client.Messages.Count > 0) { foreach (POP3_ClientMessage messages in pop3Client.Messages) { if (messages.UID == UID) { messages.MarkForDeletion(); } } } } } catch (Exception ex) { throw ex; } }
/// <summary> /// 获取 /// </summary> /// <param name="account">配置</param> /// <param name="receiveCount">已收邮件数、注意:如果已收邮件数和邮件数量一致则不获取</param> /// <returns></returns> public static List <MailModel> Get(MailAccount account, int receiveCount) { try { var filePath = DirFileHelper.GetAbsolutePath("~/Resource/EmailFile/"); var resultList = new List <MailModel>(); using (POP3_Client pop3Client = new POP3_Client()) { pop3Client.Connect(account.POP3Host, account.POP3Port, account.Ssl); pop3Client.Login(account.Account, account.Password); var messages = pop3Client.Messages; if (receiveCount == messages.Count) { return(resultList); } for (int i = messages.Count - 1; receiveCount <= i; i--) { var messageItem = messages[i]; var messageHeader = Mail_Message.ParseFromByte(messageItem.MessageToByte()); resultList.Add(new MailModel() { UID = messageItem.UID, To = messageHeader.From == null ? "" : messageHeader.From[0].Address, ToName = messageHeader.From == null ? "" : messageHeader.From[0].DisplayName, Subject = messageHeader.Subject, BodyText = messageHeader.BodyHtmlText, Attachment = GetFile(filePath, messageHeader.GetAttachments(true, true), messageItem.UID), Date = messageHeader.Date, }); } } return(resultList); } catch (Exception ex) { throw ex; } }
/// <summary> /// 发送 /// </summary> /// <param name="account">配置</param> /// <param name="mailModel">信息</param> public static void Send(MailAccount account, MailModel mailModel) { try { MailMessage mailMessage = new MailMessage(); mailMessage.From = new MailAddress(account.Account, account.AccountName); //发件人 if (!string.IsNullOrEmpty(mailModel.To)) { var ToArray = mailModel.To.Split(','); foreach (var item in ToArray) { mailMessage.To.Add(new MailAddress(item)); } } //抄送人 if (!string.IsNullOrEmpty(mailModel.CC)) { var CCArray = mailModel.CC.Split(','); foreach (var item in CCArray) { mailMessage.CC.Add(new MailAddress(item)); } } //密送人 if (!string.IsNullOrEmpty(mailModel.Bcc)) { var BccArray = mailModel.Bcc.Split(','); foreach (var item in BccArray) { mailMessage.Bcc.Add(new MailAddress(item)); } } //附件 //var filePath = DirFileHelper.GetAbsolutePath("~/Resource/EmailFile/"); //foreach (MailFile item in mailModel.Attachment) //{ // var attachment = new Attachment(filePath + item.FileId); // attachment.Name = item.FileName; // mailMessage.Attachments.Add(attachment); //} mailMessage.Subject = mailModel.Subject; mailMessage.Body = mailModel.BodyText; mailMessage.IsBodyHtml = true; mailMessage.Priority = MailPriority.Normal; mailMessage.SubjectEncoding = Encoding.UTF8; mailMessage.BodyEncoding = Encoding.UTF8; //不被当作垃圾邮件的关键代码--Begin mailMessage.Headers.Add("X-Priority", "3"); mailMessage.Headers.Add("X-MSMail-Priority", "Normal"); mailMessage.Headers.Add("X-Mailer", "Microsoft Outlook Express 6.00.2900.2869"); mailMessage.Headers.Add("X-MimeOLE", "Produced By Microsoft MimeOLE V6.00.2900.2869"); mailMessage.Headers.Add("ReturnReceipt", "1"); //不被当作垃圾邮件的关键代码--End using (SmtpClient smtpClient = new SmtpClient(account.SMTPHost, account.SMTPPort)) { smtpClient.UseDefaultCredentials = false; smtpClient.Credentials = new NetworkCredential(account.Account, account.Password); smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; smtpClient.Send(mailMessage); } } catch (Exception ex) { throw ex; } }