C # general class for simple mass mailing,

Source: Internet
Author: User
Tags mailmessage smtpclient

C # general class for simple mass mailing,

1 public static class Email 2 {3 /// <summary> 4 /// sender 5 /// </summary> 6 public static string mailFrom {get; set ;} 7 8 /// <summary> 9 /// recipient 10 /// </summary> 11 public static string [] mailToArray {get; set ;} 12 13 /// <summary> 14 /// CC 15 /// </summary> 16 public static string [] mailCcArray {get; set ;} 17 18 /// <summary> 19 // Title 20 /// </summary> 21 public static string mailSubjec T {get; set;} 22 23 // <summary> 24 // body 25 /// </summary> 26 public static string mailBody {get; set ;} 27 28 /// <summary> 29 // sender password 30 /// </summary> 31 public static string mailPwd {get; set ;} 32 33 // <summary> 34 // SMTP Mail Server 35 /// </summary> 36 public static string host {get; set ;} 37 38 // <summary> 39 // mail server port 40 /// </summary> 41 public static int port {get; set;} 4 2 43 // <summary> 44 // whether the body is in html format. 45 // </summary> 46 public static bool isbodyHtml {get; set ;} 47 48 /// <summary> 49 // attachment 50 /// </summary> 51 public static string [] attachmentsPath {get; set ;} 52 53 public static bool Send () 54 {55 // use the specified email address to initialize MailAddress instance 56 MailAddress maddr = new MailAddress (mailFrom ); 57 58 // initialize MailMessage instance 59 MailMessage myMail = new MailMessage (); 60 61/ /Add email address 62 to the recipient address set if (mailToArray! = Null) 63 {64 for (int I = 0; I <mailToArray. length; I ++) 65 {66 myMail. to. add (mailToArray [I]. toString (); 67} 68} 69 70 // Add email address 71 if (mailCcArray! = Null) 72 {73 for (int I = 0; I <mailCcArray. length; I ++) 74 {75 myMail. CC. add (mailCcArray [I]. toString (); 76} 77} 78 // sender address 79 myMail. from = maddr; 80 81 // email title 82 myMail. subject = mailSubject; 83 84 // The Subject content of the email is encoded as 85 myMail. subjectEncoding = Encoding. UTF8; 86 87 // email body 88 myMail. body = mailBody; 89 90 // email Body code 91 myMail. bodyEncoding = Encoding. default; 92 93 // email priority 94 myMail. P Riority = MailPriority. high; 95 96 // email is not in html Format 97 myMail. isBodyHtml = isbodyHtml; 98 99 // Add Attachment 100 try101 {102 if (attachmentsPath! = Null & attachmentsPath. length> 0) 103 {104 Attachment attachFile = null; 105 foreach (string path in attachmentsPath) 106 {107 attachFile = new Attachment (path); 108 myMail. attachments. add (attachFile); 109} 110} 111} 112 catch (Exception err) 113 {114 throw new Exception ("error when adding attachment:" + err. message); 115} 116 117 SmtpClient client = new SmtpClient (); 118 119 // specify the sender's email address and password to verify the sender's identity 120 client. credentials = new NetworkCredential (mailFrom, mailPwd); 121 122 // set SMTP Mail Server 123 // client. host = "smtp. "+ myMail. from. host; 124 client. host = host; 125 126 // SMTP mail server port 127 client. port = port; 128 129 // whether to use secure connection 130 // client. enableSsl = true; 131 132 trypattern {134 // send the email to the SMTP Mail Server 135 client. send (myMail); 136 return true; 137} 138 catch (SmtpException ex) 139 {140 string msg = ex. message; 141 return false; 142} 143 144}

 

Related Article

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.