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