MailKit幫助類
阿新 • • 發佈:2017-06-06
spa ngs log map personal count folders names uniq
public class EmailHelp { /// <summary> /// Smtp服務器地址 /// </summary> private static readonly string SmtpServer = ConfigurationManager.AppSettings["SmtpServer"]; /// <summary> /// Pop服務器地址 /// </summary> private static readonly stringPopServer = ConfigurationManager.AppSettings["PopServer"]; /// <summary> /// Imap服務器地址 /// </summary> private static readonly string ImapServer = ConfigurationManager.AppSettings["ImapServer"]; /// <summary> /// SMTP端口 /// </summary> privatestatic readonly int SmtpPort = int.Parse(ConfigurationManager.AppSettings["SmtpPort"]); /// <summary> /// POP端口 /// </summary> private static readonly int PopPort = int.Parse(ConfigurationManager.AppSettings["PopPort"]); /// <summary> /// IMAP端口/// </summary> private static readonly int ImapPort = int.Parse(ConfigurationManager.AppSettings["ImapPort"]); /// <summary> /// 郵件發送 /// </summary> /// <param name="mailFromAccount">發送郵箱賬號</param> /// <param name="mailPassword">發送郵箱密碼</param> /// <param name="message">郵件</param> public static void SendEmali(string mailFromAccount, string mailPassword, MimeMessage message) { using (var client = new MailKit.Net.Smtp.SmtpClient()) { client.Connect(SmtpServer, SmtpPort, false); // Note: since we don‘t have an OAuth2 token, disable // the XOAUTH2 authentication mechanism. client.AuthenticationMechanisms.Remove("XOAUTH2"); // Note: only needed if the SMTP server requires authentication client.Authenticate(mailFromAccount, mailPassword); client.Send(message); client.Disconnect(true); } } /// <summary> /// 創建文本消息 /// </summary> /// <param name="fromAddress">發件地址</param> /// <param name="toAddressList">收件地址</param> /// <param name="title">標題</param> /// <param name="content">內容</param> /// <param name="IsPostFiles">是否將POST上傳文件加為附件</param> /// <returns></returns> public static MimeMessage CreateTextMessage(MailboxAddress fromAddress, IList<MailboxAddress> toAddressList , string title, string content, bool IsPostFiles = false) { var message = new MimeMessage(); message.From.Add(fromAddress); message.To.AddRange(toAddressList); message.Subject = title; //設置消息的主題 var html = new TextPart("html") { Text = content, }; var alternative = new Multipart("alternative"); alternative.Add(html); var multipart = new Multipart("mixed"); multipart.Add(alternative); if (IsPostFiles) { IList<MimePart> multiPartList = GetMimePartList(); foreach (var item in multiPartList) { multipart.Add(item); } } message.Body = multipart; return message; } /// <summary> /// 收郵件 /// </summary> /// <param name="mailFromAccount">發送郵箱賬號</param> /// <param name="mailPassword">發送郵箱密碼</param> /// <param name="searchQuery">查詢條件</param> /// <param name="folderName">文件夾名稱</param> /// <returns></returns> public static IList<IMessageSummary> ReceiveEmail(string mailFromAccount, string mailPassword, string folderName, SearchQuery searchQuery = null) { //打開收件箱 var folder = OpenFolder(mailFromAccount, mailPassword, folderName); //IList<OrderBy> orderByList = new List<OrderBy> { OrderBy.Date }; //查詢所有的郵件 var uidss = folder.Search(searchQuery); IList<IMessageSummary> msgList = new List<IMessageSummary>(); if (uidss.Count > 0)//判斷是否查詢到郵件 { //獲取郵件頭 msgList = folder.Fetch(uidss, MessageSummaryItems.UniqueId | MessageSummaryItems.Full); } folder.Close(); return msgList; } /// <summary> /// 根據唯一號查詢信件 /// </summary> /// <param name="mailFromAccount">郵箱賬號</param> /// <param name="mailPassword">郵箱密碼</param> /// <param name="id">唯一號</param> /// <param name="folderName">文件夾名稱</param> /// <returns></returns> public static MimeMessage GetEmailByUniqueId(string mailFromAccount, string mailPassword, uint id, string folderName) { //打開收件箱 var folder = OpenFolder(mailFromAccount, mailPassword, folderName); UniqueId emailUniqueId = new UniqueId(id); MimeMessage message = folder.GetMessage(emailUniqueId); /*將郵件設為已讀*/ MessageFlags flags = MessageFlags.Seen; folder.SetFlags(emailUniqueId, flags, true); return message; } /// <summary> /// 讀取上傳的文件 /// </summary> /// <returns></returns> public static IList<MimePart> GetMimePartList() { IList<MimePart> mimePartList = new List<MimePart>(); var current = HttpContext.Current; if (current != null) { HttpRequest request = current.Request; HttpFileCollection files = request.Files; int filesCount = files.Count; for (int i = 0; i < filesCount; i++) { HttpPostedFile item = files[i]; MimePart attachment = new MimePart(item.ContentType) { ContentObject = new ContentObject(item.InputStream, ContentEncoding.Default), ContentDisposition = new ContentDisposition(ContentDisposition.Attachment), ContentTransferEncoding = ContentEncoding.Base64, FileName = item.FileName }; mimePartList.Add(attachment); } } return mimePartList; } /// <summary> /// 打開郵箱文件夾 /// </summary> /// <param name="mailFromAccount">郵箱賬號</param> /// <param name="mailPassword">郵箱密碼</param> /// <param name="folderName">文件夾名稱(INBOX:收件箱名稱)</param> /// <returns></returns> public static IMailFolder OpenFolder(string mailFromAccount, string mailPassword, string folderName) { ImapClient client = new ImapClient(); client.Connect(ImapServer, ImapPort); client.Authenticate(mailFromAccount, mailPassword); //獲取所有文件夾 //List<IMailFolder> mailFolderList = client.GetFolders(client.PersonalNamespaces[0]).ToList(); var folder = client.GetFolder(folderName); //打開文件夾並設置為讀的方式 folder.Open(MailKit.FolderAccess.ReadWrite); return folder; } /// <summary> /// 下載郵件附件 /// </summary> /// <param name="mimePart"></param> public static void DownFile(MimePart mimePart) { HttpContext context = HttpContext.Current; // 設置編碼和附件格式 context.Response.ContentType = mimePart.ContentType.ToString(); //context.Response.ContentEncoding = Encoding.UTF8; context.Response.Charset = ""; string fileName = HttpUtility.UrlEncode(mimePart.FileName, Encoding.UTF8); context.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName); using (MemoryStream ms = new MemoryStream()) { mimePart.ContentObject.DecodeTo(ms); ms.Flush(); ms.Position = 0; context.Response.BinaryWrite(ms.GetBuffer()); context.Response.End(); } } }
MailKit幫助類