7.dotnet core how to send mail

Source: Internet
Author: User
Tags dotnet how to send mail

The NuGet package that needs to be used

"Mailkit": "1.8.1",

Method

  /// <summary>        ///Send mail (support HTML send, support add an attachment)/// </summary>        /// <param name= "Subject" >message Header</param>        /// <param name= "email" >Recipient Address</param>        /// <param name= "Content" >Message Content</param>        /// <param name= "filepath" >relative path of the file</param>         Public Static voidSendEmail (stringSubjectstringEmailstringContentstringFilepath=NULL)        {            varMessage =NewMimeMessage (); // SenderMessage. From.add (NewMailboxaddress ("Sender's name","Sender's email")); //The addressee.Message. To.add (NewMailboxaddress ("", email)); //titleMessage. Subject =subject; //produce a textpart that supports HTML            varBODY =NewTextpart (textformat.html) {Text=content}; //first produce a            varmultipart =NewMultipart ("Mixed"); //Add Body Contentmultipart.            ADD (body); if(!string. Isnullorwhitespace (filepath)) {//production of an absolute path//filepath = "Upload//Newsphoto//Readme.txt ";                varAbsolutepath = Path.Combine (_hostingenv.webrootpath,string.                Format (filepath)); //Accessories                varAttachment =NewMimePart () {//read file (only with absolute path)Contentobject =NewContentobject (File.openread (Absolutepath), Contentencoding.default), Contentdisposition=Newcontentdisposition (contentdisposition.attachment), contenttransferencoding=Contentencoding.base64,//file nameFileName =path.getfilename (Absolutepath)}; //Add an attachmentmultipart.            ADD (attachment); }                //Body ContentMessage. Body =multipart; using(varClient =NewSmtpClient ()) {                //connect to an SMTP serverClient. Connect (the SMTP server address"Portfalse); //LoginClient. Authenticate ("Account",the password"); //Sendclient.                Send (message); //DisconnectClient. Disconnect (true); }        }

The above method can send HTML text and attached attachments

7.dotnet core how to send mail

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.