Mail Send Emailhelper

Source: Internet
Author: User
Tags mailmessage smtpclient

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.