Email class
Public classEmail {/// <summary> ///sent by/// </summary> Public stringMailfrom {Get;Set; } /// <summary> ///Recipient/// </summary> Public string[] Mailtoarray {Get;Set; } /// <summary> ///cc/// </summary> Public string[] Mailccarray {Get;Set; } /// <summary> ///title/// </summary> Public stringMailsubject {Get;Set; } /// <summary> ///Body/// </summary> Public stringMailbody {Get;Set; } /// <summary> ///Sender Password/// </summary> Public stringmailpwd {Get;Set; } /// <summary> ///SMTP mail server/// </summary> Public stringHost {Get;Set; } /// <summary> ///whether the body is in HTML format/// </summary> Public BOOLisbodyhtml {Get;Set; } /// <summary> ///Accessories/// </summary> Public string[] Attachmentspath {Get;Set; } Public BOOLSend () {//Initializes a mailaddress instance with the specified mail addressMailAddress maddr =Newmailaddress (Mailfrom); //initializing an MailMessage instanceMailMessage MyMail =NewMailMessage (); //Add a mail address to a collection of recipient addresses if(Mailtoarray! =NULL) { for(inti =0; i < mailtoarray.length; i++) {myMail.To.Add (Mailtoarray[i]. ToString ()); } } //Add an email address to the CC recipient address Collection if(Mailccarray! =NULL) { for(inti =0; i < mailccarray.length; i++) {myMail.CC.Add (Mailccarray[i]. ToString ()); } } //Sender AddressMymail.from =maddr; //the title of the e-mailMymail.subject =Mailsubject; //the encoding used for the subject content of the e-mailMymail.subjectencoding =Encoding.UTF8; //Email BodyMymail.body =Mailbody; //encoding of the body of the e-mailMymail.bodyencoding =Encoding.default; Mymail.priority=Mailpriority.high; Mymail.isbodyhtml=isbodyhtml; //Add an attachment with an attachment Try { if(Attachmentspath! =NULL&& attachmentspath.length >0) {Attachment attachfile=NULL; foreach(stringPathinchAttachmentspath) {Attachfile=NewAttachment (path); MYMAIL.ATTACHMENTS.ADD (Attachfile); } } } Catch(Exception err) {Throw NewException ("there was an error when adding an attachment:"+err); } smtpclient SMTP=NewSmtpClient (); //setting up an SMTP mail serverSmtp. Host =host; //SMTP. Enablessl = true;Smtp. Port = -; //SMTP. useDefaultCredentials = true; //Specify the sender's e-mail address and password to verify sender identitySmtp. Credentials =NewSystem.Net.NetworkCredential (Mailfrom, mailpwd); Try { //send a message to an SMTP mail serverSMTP. Send (MyMail); return true; } Catch(System.Net.Mail.SmtpException ex) {return false; } } }
Send code
Static voidMain (string[] args) {Email email=NewEmail (); Email.mailfrom="[email protected]"; Email.mailpwd="jie0628"; Email.mailsubject="Programmer's Life"; Email.mailbody="a lot of programmers actually very hard one thing"; Email.isbodyhtml=true; Email.host="smtp.qq.com"; Email.mailtoarray=New string[] {"[email protected]" }; //Email.mailtoarray = new string[] {"[email protected]", "[Email protected]"}; //Email.mailccarray = new string[] {"[Email protected]"};Email.attachmentspath =New string[] { }; if(email.) Send ()) {Console.WriteLine ("message sent successfully ~"); Console.readkey (); } Else{Console.WriteLine ("Message Delivery Failed ~"); Console.readkey (); } }
C # Send mail