C # send mails in batches

Source: Internet
Author: User
Tags mailmessage smtpclient

Public void Send () {MailMessage mail = new MailMessage (); try {// set the mail title. subject = "youzai WebService Report"; mail. subjectEncoding = System. text. encoding. UTF8; // set the sender string SendEmail = ConfigurationManager. deleetask[ "SendEmail"]. toString (); MailAddress Addressfrom = new MailAddress (SendEmail, "uzai", System. text. encoding. UTF8); // The mail sender's mail. from = Addressfrom;/* this is written here because it may be sent to multiple contacts. Each address is separated by a comma. */List
 
  
MailAddress = new List
  
   
(); Foreach (string address in mailAddress) {if (address! = String. empty) {mail. to. add (new MailAddress (address, address, System. text. encoding. UTF8) ;}/// set the mail content to mail. body = mailContent; // set the mail format. bodyEncoding = System. text. encoding. UTF8; // mail. isBodyHtml = true; // sets the mail sending level. priority = MailPriority. normal; // send the notification mail. deliveryNotificationOptions = DeliveryNotificationOptions. onSuccess; SmtpClient client = new SmtpClient (); // set the name of the host used for SMTP transaction, and enter the IP address or the client. host = "smtp.gmail.com"; client. port = 25; client. timeout = 9999; client. usedefacrecredentials = true; client. enableSsl = true; // The email login name and password string EmailLogName = ConfigurationManager. appSettings ["EmailLogName"]. toString (); string EmailLogPwd = ConfigurationManager. appSettings ["EmailLogPwd"]. toString (); client. credentials = new System. net. networkCredential (EmailLogName, EmailLogPwd); client. deliveryMethod = SmtpDeliveryMethod. network; // send email client. send (mail);} catch (Exception) {// when the email fails to be sent, use the alternative method to call the alternate mailbox to Send SendBackUp ();} finally {mail. dispose () ;}} public void SendBackUp () {MailMessage mail = new MailMessage (); try {// set the mail title. subject = "youzai WebService Report"; mail. subjectEncoding = System. text. encoding. UTF8; // set the sender string SendEmail = ConfigurationManager. deleetask[ "SendEmail"]. toString (); MailAddress Addressfrom = new MailAddress (SendEmail, "uzai", System. text. encoding. UTF8); // The mail sender's mail. from = Addressfrom;/* this is written here because it may be sent to multiple contacts. Each address is separated by a comma. */List
   
    
MailAddress = new List
    
     
(); Foreach (string address in mailAddress) {if (address! = String. empty) {mail. to. add (new MailAddress (address, address, System. text. encoding. UTF8) ;}/// set the mail content to mail. body = mailContent; // set the mail format. bodyEncoding = System. text. encoding. UTF8; // mail. isBodyHtml = true; // sets the mail sending level. priority = MailPriority. normal; // send the notification mail. deliveryNotificationOptions = DeliveryNotificationOptions. onSuccess; SmtpClient client = new SmtpClient (); // set the name of the host used for SMTP transaction, and enter the IP address or the client. host = "smtp.gmail.com"; client. port = 25; client. timeout = 9999; client. usedefacrecredentials = true; client. enableSsl = true; // The email login name and password string EmailLogName = ConfigurationManager. appSettings ["EmailLogName"]. toString (); string EmailLogPwd = ConfigurationManager. appSettings ["EmailLogPwd"]. toString (); client. credentials = new System. net. networkCredential (EmailLogName, EmailLogPwd); client. deliveryMethod = SmtpDeliveryMethod. network; // send email client. send (mail);} catch (Exception) {// when the email fails to be sent, use the alternate method to call the alternate mailbox to Send ();} finally {mail. dispose ();}}
    
   
  
 

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.