MailKit help class, MailKit

Source: Internet
Author: User

MailKit help class, MailKit



Public class EmailHelp {// <summary> // Smtp server address // </summary> private static readonly string SmtpServer = ConfigurationManager. deleetask[ "SmtpServer"]; // <summary> // Pop server address // </summary> private static readonly string PopServer = ConfigurationManager. deleetask[ "PopServer"]; // <summary> // Imap server address // </summary> private static readonly string ImapServer = ConfigurationManager. A PpSettings ["ImapServer"]; // <summary> // SMTP port // </summary> private static readonly int SmtpPort = int. parse (ConfigurationManager. appSettings ["SmtpPort"]); // <summary> // POP port // </summary> private static readonly int PopPort = int. parse (ConfigurationManager. appSettings ["PopPort"]); // <summary> // IMAP port // </summary> private static readonly int ImapPort = int. parse (ConfigurationMan Ager. appSettings ["ImapPort"]); /// <summary> /// send email /// </summary> /// <param name = "mailFromAccount"> send email account </param> /// <param name = "mailPassword"> send email password </param> // <param name = "message"> email </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 oau2token, disable // the xoau2authentication mechanism. client. authenticationmechanic isms. remove ("xoau2"); // Note: only needed if the SMTP server requires authentication client. authenticate (mailFromAccount, mailPassword); client. send (message); client. disconnect (true );}} /// <summary> /// create a text message /// </summary> /// <param name = "fromAddress"> Sending address </param> /// <Param name = "toAddressList"> recipient address </param> /// <param name = "title"> title </param> /// <param name = "content"> content </param> /// <param name = "IsPostFiles"> Add POST files as attachments </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; // set the message topic 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) {multip Art. add (item) ;}} message. body = multipart; return message ;} /// <summary> /// receive email /// </summary> /// <param name = "mailFromAccount"> email sending account </param> /// <param name = "mailPassword"> send email password </param> /// <param name = "searchQuery"> query condition </param> /// <param name = "folderName"> folder name </param> /// <returns> </returns> public static IList <IMessageSummary> ReceiveEmail (string mailFromAccount, string mailPassword, String folderName, SearchQuery searchQuery = null) {// open the inbox var folder = OpenFolder (mailFromAccount, mailPassword, folderName ); // IList <OrderBy> orderByList = new List <OrderBy> {OrderBy. date}; // query all emails var uidss = folder. search (searchQuery); IList <IMessageSummary> msgList = new List <IMessageSummary> (); if (uidss. count> 0) // determine whether the email is queried {// obtain the email header msgList = folder. fetch (uidss, MessageSummaryItems. UniqueId | MessageSummaryItems. full);} folder. close (); return msgList ;} /// <summary> /// query a letter by unique ID /// </summary> /// <param name = "mailFromAccount"> email account </param> // /<param name = "mailPassword"> email password </param> // <param name = "id"> Unique id </param> /// <param name =" folderName "> folder name </param> // <returns> </returns> public static MimeMessage GetEmailByUniqueId (string mailFromAccount, string mailPassword, ui Nt id, string folderName) {// open the inbox var folder = OpenFolder (mailFromAccount, mailPassword, folderName); UniqueId emailUniqueId = new UniqueId (id); MimeMessage message = folder. getMessage (emailUniqueId);/* set the email to read */MessageFlags flags = MessageFlags. seen; folder. setFlags (emailUniqueId, flags, true); return message ;} /// <summary> /// read the uploaded file /// </summary> /// <returns> </returns> public static ILi St <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> /// open the email folder /// </summary> /// <param name = "mailFromAccount"> email account </param> /// <param name = "mailPassword"> email password </param> // <param name = "folderName"> folder name (INBOX: inbox name) </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); // obtain all folders // List <IMailFolder> mailFolderList = client. getFolders (client. personalNamespaces [0]). toList (); var folder = client. getFolder (folderName); // open the folder and set it to the Read mode folder. open (MailKit. folderAccess. readWrite); return folder ;} /// <summary> /// download the email attachment /// </summary> /// <param name = "mimePart"> </param> public static void DownFile (MimePart mimePart) {HttpContext context = HttpContext. current; // set the encoding and attachment format 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 ();}}}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.