C # email _ EmailHelper,
EmailHelper class
Public class EmailHelper {// <summary> // send an email /// </summary> /// <param name = "smtpServer"> smtp server </param>/ // <param name = "port"> port </param> /// <param name = "mailFrom"> sender's email address </param> /// <param name =" userPassword "> password </param> /// <param name =" mailTo "> recipient </param> /// <param name =" strcc "> CC </param> /// <param name = "strBcc"> BCC </param> /// <param name = "mailSubject"> Email Subject </param> /// <param name = "mailC Ontent "> content </param> /// <param name =" strs "> attachment </param> /// <returns> success returned true otherwise false </returns> public static bool SendEmail (string smtpServer, int port, string mailFrom, string userPassword, string mailTo, string strcc, string strBcc, string mailSubject, string mailContent, string strs) {try {// set the sender's email information // set SmtpClient smtpClient = new SmtpClient (); smtpClient. deliveryMethod = SmtpDeliveryM Ethod. network; // specify the email sending method smtpClient. host = smtpServer; // specify the SMTP server smtpClient. port = port; // Port smtpClient. credentials = new System. net. networkCredential (mailFrom, userPassword); // verify the username and password smtpClient. enableSsl = true; // use SSL // to set MailMessage mailMessage = new MailMessage (mailFrom, mailTo); // sender and recipient mailMessage. subject = mailSubject; // Subject mailMessage. body = mailContent; // content mailMessage. body Encoding = Encoding. UTF8; // The Body code mailMessage. IsBodyHtml = true; // set it to the HTML format mailMessage. Priority = MailPriority. Normal; // Priority // CC to if (! String. IsNullOrEmpty (strcc) mailMessage. CC. Add (strcc); // BCC if (! String. IsNullOrEmpty (strBcc) mailMessage. Bcc. Add (strBcc); // attachment if (! String. isNullOrEmpty (strs) {List <string> paths = new List <string> (); if (strs. contains (",") {paths = strs. split (','). toList ();} else {paths. add (strs);} foreach (var path in paths) {mailMessage. attachments. add (new Attachment (strs) ;}} smtpClient. send (mailMessage); // Send the MAIL return true;} catch {return false ;}}}
Call the mail sending method:
Static void Main (string [] args) {// obtain various parameters. You do not need to use an empty string path = Directory. getCurrentDirectory (); string file = path + @ "\ Excel.xlsx"; // Appendix 1 string file2 = path + @ "\ Excel2.xlsx "; // Appendix 2 string smtpServer = "smtp.163.com"; // int port of the smtp server in the 163 mailbox = 25; // port string mailFrom = "****** @ 163.com "; // sender's email string pwd = "*********"; // password string mailTo = "123@163.com, 456@qq.com"; // recipient's email, multiple users use commas to separate string mailC C = ""; // cc. Multiple users use commas to separate string mailBcc = ""; // BCC string mailSubject = "test mail "; // subject string mailContent = "HI, this is a test email I sent you"; // content string ah = file + "," + file2; // attachment-file path if (EmailHelper. sendEmail (smtpServer, port, mailFrom, pwd, mailTo, mailCC, mailBcc, mailSubject, mailContent, ah) = true) {Console. writeLine ("sent successfully! ");} Else Console. WriteLine (" failed to send "); Console. ReadKey ();}
Note:
- Confirm that the sender's email address has enabled smtp;
- The smtp corresponding to the mailbox is entered correctly, for example: 126 mailbox: smtp@126.com
- Do you want to use SSL encrypted connections in the corresponding email SendEmail?