Emailhelper class
public class Emailhelper {//<summary>//E-mail//</summary>//<param Name= "smtpserver" >SMTP server </param>///<param Name= "Port" > Port </param>///<param Name = "Mailfrom" > Sender mailbox </param>//<param name= "UserPassword" > Password </param>//<param name= "MailTo" > Recipient </param>//<param name= "STRCC" > CC person </param>//<param name= "STRBCC" , BCC </param>//<param name= "Mailsubject" > Mail subject </param>//<param name= "Mailcontent" Content </param>//<param name= "STRs" > Accessories </param>///<returns> Send successful return true otherwise False</ret urns> public static bool SendEmail (string smtpserver, int port, string mailfrom, String UserPassword, String mai LTo, String strcc,string strbcc,string mailsubject, String mailcontent, String STRs) {try { Set the sender'sMail message//Mail Service settings SmtpClient smtpclient = new SmtpClient (); Smtpclient.deliverymethod = smtpdeliverymethod.network;//Specifies how e-mail is sent smtpclient.host = SmtpServer; Specify SMTP Server Smtpclient.port = port;//Port smtpclient.credentials = new System.Net.NetworkCrede Ntial (Mailfrom, UserPassword);//verify user name and password Smtpclient.enablessl = true; Use SSL//Send mail settings mailmessage mailmessage = new MailMessage (Mailfrom, mailTo); Sender and Recipient Mailmessage.subject = mailsubject;//subject Mailmessage.body = mailcontent;//content mailmessage.bodyencoding = encoding.utf8;//Body Encoding mailmessage.isbodyhtml = true;//set to HTML format Mailmessage.priority = mailpriority.normal;//Priority//CC person if (!string. IsNullOrEmpty (STRCC)) mailMessage.CC.Add (STRCC); Secret Send 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 (n EW Attachment (STRs)); }} smtpclient.send (MailMessage); Send message return true; } catch {return false; } } }
Call the method that sent the message:
Static voidMain (string[] args) { //get various parameters, not required with an empty string stringPath =directory.getcurrentdirectory (); stringFile = path +@"\excel.xlsx";//Annex 1 stringFile2 = path +@"\excel2.xlsx";//Annex 2 stringSmtpServer ="smtp.163.com";//163 SMTP server for mailbox intPort = -;//Port stringMailfrom ="******@163.com";//Sender Mailbox stringPWD ="*********";//Password stringMailTo ="[email protected],[email protected]";//recipient mailbox, multiple users separated by commas stringMAILCC ="";//cc person, multiple users separated by commas stringMAILBCC ="";//Secret Delivery stringMailsubject ="Test Message";//Theme stringMailcontent ="HI, this is a test email I sent you.";//content stringAh = file+","+file2;//Attachments-File path if(Emailhelper.sendemail (SmtpServer, Port, Mailfrom, PWD, MailTo, MAILCC, MAILBCC, Mailsubject, mailcontent, ah) = =true) {Console.WriteLine ("sent successfully!"); } ElseConsole.WriteLine ("Send failed"); Console.readkey (); }
Attention:
- Confirm that the sender's mailbox has been opened SMTP;
- The SMTP of the corresponding mailbox is filled out correctly, for example: 126 e-mail: [Email protected]
- Whether to use SSL encrypted connection in the corresponding mailbox SendEmail
C # Send mail _emailhelper