Reference:
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Mail; using System.Text;
Emailhepler class:
Public Static classEmailhelper {/// <summary> ///C # send mail function/// </summary> /// <param name= "Fromdisplayname" >Sender Mailbox Display name</param> /// <param name= "sto" >Mail Inbox</param> /// <param name= "Ssubject" >Theme</param> /// <param name= "Sbody" >content</param> /// <param name= "Sfiles" >attachment path, absolute path</param> /// <param name= "Ccto" >cc Address</param> /// <param name= "isbodyhtml" >whether the message supports HTML format</param> /// <returns></returns> Public Static BOOLSendmail (stringFromdisplayname,stringStostringSsubject,stringSbody,string[] Sfiles =NULL,string[] Ccto =NULL,BOOLisbodyhtml =false) { ////set from and to addressesMailAddress from=Newmailaddress (EmailSection.Instance.SMTPuser, fromdisplayname); MailAddress to=Newmailaddress (STO); ////Create a MailMessage objectMailMessage Omail =NewMailMessage ( from, to); ////Add Attachment if(Sfiles! =NULL) { foreach(varIteminchsfiles) {OMAIL.ATTACHMENTS.ADD (NewAttachment (item)); } } //add a CC user if(Ccto! =NULL) { foreach(varIteminchccto) {OMAIL.CC.ADD (Newmailaddress (item)); } } ////Mail HeaderOmail.subject =Ssubject; ////email ContentOmail.body =Sbody; ////Mail Formatomail.isbodyhtml =isbodyhtml; ////The encoding used in the messageomail.bodyencoding = System.Text.Encoding.GetEncoding ("GB2312"); ////Set message priority to highOmail.priority =Mailpriority.normal; ////Send mailSmtpClient client =NewSmtpClient (); ////client. useDefaultCredentials = false;Client. Host =EmailSection.Instance.SMTPHost; Client. Credentials=NewNetworkCredential (EmailSection.Instance.SMTPuser, EmailSection.Instance.SMTPpass); Client. Deliverymethod=smtpdeliverymethod.network; Try{client. Send (Omail); return true; } Catch(Exception Err) {Console.WriteLine (Err. Message.tostring ()); return false; } finally { ////Release ResourcesOmail.dispose (); } } }
Emailsection Configuration Related information:
Public classemailsection:configurationsection {Private StaticEmailsection _instance =NULL; Public Staticemailsection Instance {Get { if(_instance = =NULL) {_instance= (emailsection) configurationmanager.getsection (typeof(emailsection). Name); } return_instance; }} [ConfigurationProperty ("SMTPHost", isrequired =true)] Public stringSMTPHost {Get { return(String) This["SMTPHost"]; } Set { This["SMTPHost"] =value;} } [ConfigurationProperty ("Smtpuser", isrequired =true)] Public stringSmtpuser {Get { return(String) This["Smtpuser"]; } Set { This["Smtpuser"] =value;} } [ConfigurationProperty ("Smtppass", isrequired =true)] Public stringSmtppass {Get { return(String) This["Smtppass"]; } Set { This["Smtppass"] =value;} } }
Configuration file configuration:
<= "smtp.exmail.qq.com" = "[email protected]" = " Laiyihuo_1 "></emailsection>
Mail Send Emailhelper