Using the email Send function

Source: Internet
Author: User
Tags email account mail account mailmessage smtpclient

The last time CRM used the email send function, although the final implementation, but not satisfied with the effect.

Later found some information, re-organized a copy, nonsense not much to say, directly on the code

Using system;using system.collections.generic;using system.linq;using system.text;using System.Net;using System.net.mail;namespace CRM. Business.att{class Kencerycommonmethod {//<summary>/////Features: Enable mail delivery, split the method of sending messages///&LT            ;/summary> public class Email {#region--------------------field--------------------            private string _servicetype = "SMTP";            private string _host; #endregion #region--------------------Properties--------------------//<summary>//Send            Email///</summary> public string from {get; set;}            <summary>///Recipient mailbox List///</summary> public string[] to {get; set;} <summary>//CC mailbox list///</summary> public string[] cc {get; s Et }//<summary>//E-mail list/// </summary> public string[] BCC {get; set;}            <summary>///Mail Subject///</summary> public string Subject {get; set;}  <summary>///e-mail content///</summary> public string Body {get; set; }///<summary>//IS HTML format///</summary> public bool Isbodyht            ml {get; set;}  <summary>////Accessories list///</summary> public string[] Attachments {get; set;            }///<summary>//E-mail Service type (pop3,smtp,imap,mail, etc.), default to SMTP//</summary>                public string ServiceType {get {return _servicetype;}            set {_servicetype = value;} }//<summary>///mailbox server, if no mailbox server is defined, the mailbox server is composed according to servicetype and sender///</summary > PubliC string Host {get {return string. IsNullOrEmpty (_host)? (this.) ServiceType + "." + Sender.split ("@".                ToCharArray (), stringsplitoptions.removeemptyentries) [1]): _host;            } set {_host = value;}  }///<summary>//E-mail account (default to Sender's email account)///</summary> public string            UserName {get; set;} <summary>///email password (default to sender's mailbox password), default format GB2312///</summary> public string P            Assword {get; set;} <summary>//e-mail priority///</summary> public mailpriority mailpriority {get ; Set }///<summary>//message body encoding Format///</summary> public Encoding encod            ing {get; set;} #endregion #region------------------Calling Method------------------//<summary>///construction parameters, sending mail, using method Note: calling///</in public method                summary> public void Send () {var mailmessage = new MailMessage (); Read the to Recipient's mailbox list if (this. To! = null && this. To.length > 0) {foreach (String to in). To) {if (string.                        IsNullOrEmpty (to)) continue; try {mailMessage.To.Add (new mailaddress) (to.                        Trim ())); } catch (Exception ex) {throw new Exception (ex.m                        Essage); }}//Read cc CC e-mail address if (this. Cc! = null && this. Cc.length > 0) {foreach (var cc in).             CC)       {if (string.                        IsNullOrEmpty (CC)) continue; try {mailMessage.CC.Add (new MailAddress (CC).                        Trim ())); } catch (Exception ex) {throw new Exception (ex.m                        Essage); }}//Read attachments message attachment if (this. Attachments! = null && this. Attachments.length > 0) {foreach (var attachment in). Attachments) {if (string.                        IsNullOrEmpty (attachment)) continue;                        try {mailMessage.Attachments.Add (new Attachment (Attachment));  } catch (Exception ex) {throw new Exception (ex. Message); }}//Read BCC secret copy address if (this. BCC! = null && this. Bcc.length > 0) {foreach (var bcc in). BCC) {if (string.                        IsNullOrEmpty (BCC)) continue; try {mailMessage.Bcc.Add (new MailAddress (BCC).                        Trim ())); } catch (Exception ex) {throw new Exception (ex.m                        Essage);                    }}}//read from sender address try { Mailmessage.from = new MailAddress (this.                from); } catch (Exception ex) {throw new Exception (ex).                Message); }//Message title Encoding Encoding = ENCODING.GEtencoding ("GB2312"); Mailmessage.subject = string. Format ("? ={0}?" B? {1}?= ", encoding. Headername, convert.tobase64string (encoding. GetBytes (this.                Subject), Base64formattingoptions.none)); Whether the message body is in HTML format mailmessage.isbodyhtml = this.                isbodyhtml; Message body Mailmessage.body = this.                Body; Mailmessage.bodyencoding = this.                Encoding; Message priority Mailmessage.priority = this.                mailpriority; Send message code implementation var SmtpClient = new SmtpClient {Host = this. Host, Credentials = new NetworkCredential (this. UserName, this.                Password)};                Authentication try {smtpclient.send (mailmessage); } catch (Exception ex) {throw new Exception (ex).                Message);    }            }        #endregion}///<summary>//e-Mail Send interface call: BOOL Istrue=emailinfo.sendemail (parameter,........);            Parameter interpretation reference method///</summary> public static class EmailInfo {//<summary> Mail sending method, passing parameters (using if problems, debugging)///</summary>//<param name= "from" > Sender mailbox Name (read from the configuration file, For example: [email protected]) (required) </param>//<param name= "to" > Recipient mailbox List, can be passed multiple, using string[] representation (read from the configuration file) ( Required) </param>///<param name= "CC" > CC email list, can be passed multiple, using string[] representation (read from profile) </param>// /<param name= "Bcc" > Secret Reader Mailbox list, can be passed multiple, using string[] representation (read from profile) </param>//<param name= "Subject" > Mail Subject, construction (required) </param>//<param name= "Body" > Email content, construct the message content sent, can send Web page (required) </param>// <param name= "isbodyhtml" > is HTML format, true Yes, false to no </param>//<param name= "attachments" > Mailbox attachment, You can pass multiple, using string[] representations (from the configuration file</param>//<param name= "host" > Mailbox server (read from the configuration file, such as: [email protected]) (required) </param> <param name= "password" > email password (read from config file, password from mailbox) (required) </param>//<returns> mail Send Succeeds, returns True, otherwise returns false</returns> public static bool SendEmail (string from, string["to, string[] cc, string[            ] Bcc, string subject, string body, bool isbodyhtml, string[] attachments, string host, string password) {//mailbox send does not meet, limit these parameters must pass if (from = = "" | | To. Length <= 0 | | Subject = = "" | | BODY = = "" | | Host = = "" | |                Password = = "") {return false;                    } var emil = new Email {from = @from, to = to,                    CC = cc, bcc = bcc, Subject = Subject, BODY = body, isbodyhtml = isbodyhtmL, Attachments = Attachments, host = host, Password = Password                }; try {Emil.                    Send ();                return true; } catch (Exception ex) {throw new Exception (ex).                Message); }            }        }    }}

  

Using the email Send function

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.