Batch mail sending

Source: Internet
Author: User

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. Web;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. net. mail;
Using system. configuration;
Using system. collections;
Using system. Data. sqlclient;
Public partial class _ default: system. Web. UI. Page
{
Public String mailcontent;
Protected void page_load (Object sender, eventargs E)
{

}

Public void send ()
{
Mailmessage mail = new mailmessage ();
Try
{
// Set the mail title
Mail. Subject = "youzai WebService Report ";
Mail. subjectencoding = system. Text. encoding. utf8;
// Set the sender of the email
String sendemail = configurationmanager. receivetask[ "sendemail"]. tostring ();
Mailaddress addressfrom = new mailaddress (sendemail, "uzai", system. Text. encoding. utf8); // mail sender
Mail. From = addressfrom;
/* This is written here because it may be sent to multiple contacts. Each address is separated by a comma (,).
*/
List <string> mailaddress = new list <string> ();
Foreach (string address in mailaddress)
{
If (address! = String. Empty)
{
Mail. to. Add (New mailaddress (address, address, system. Text. encoding. utf8 ));
}
}
// Set the email content
Mail. Body = mailcontent;
// Set the mail format
Mail. bodyencoding = system. Text. encoding. utf8;
// Mail. isbodyhtml = true;
// Set the mail sending level
Mail. Priority = mailpriority. normal;
// Send a notification
Mail. deliverynotificationoptions = deliverynotificationoptions. onsuccess;
Smtpclient client = new smtpclient ();
// Set the name of the host used for SMTP transaction, and enter the IP address.
Client. Host = "smtp.gmail.com ";
Client. Port = 25;
Client. Timeout = 9999;
Client. usedefacrecredentials = true;
Client. enablessl = true;
// Email login name and password
String emaillogname = configurationmanager. receivettings ["emaillogname"]. tostring ();
String emaillogpwd = configurationmanager. receivettings ["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 email address for sending an exception
Sendbackup ();
}
Finally
{
Mail. Dispose ();
}
}
Public void sendbackup ()
{
Mailmessage mail = new mailmessage ();
Try
{
// Set the mail title
Mail. Subject = "youzai WebService Report ";
Mail. subjectencoding = system. Text. encoding. utf8;
// Set the sender of the email
String sendemail = system. configuration. configurationmanager. receivetask[ "sendemail"]. tostring ();
Mailaddress addressfrom = new mailaddress (sendemail, "uzai", system. Text. encoding. utf8); // mail sender
Mail. From = addressfrom;
/* This is written here because it may be sent to multiple contacts. Each address is separated by a comma (,).
*/
List <string> mailaddress = new list <string> ();
Foreach (string address in mailaddress)
{
If (address! = String. Empty)
{
Mail. to. Add (New mailaddress (address, address, system. Text. encoding. utf8 ));
}
}
// Set the email content
Mail. Body = mailcontent;
// Set the mail format
Mail. bodyencoding = system. Text. encoding. utf8;
// Mail. isbodyhtml = true;
// Set the mail sending level
Mail. Priority = mailpriority. normal;
// Send a notification
Mail. deliverynotificationoptions = deliverynotificationoptions. onsuccess;
Smtpclient client = new smtpclient ();
// Set the name of the host used for SMTP transaction, and enter the IP address.
Client. Host = "smtp.gmail.com ";
Client. Port = 25;
Client. Timeout = 9999;
Client. usedefacrecredentials = true;
Client. enablessl = true;
// Email login name and password
String emaillogname = configurationmanager. receivettings ["emaillogname"]. tostring ();
String emaillogpwd = configurationmanager. receivettings ["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 email address for sending an exception
Send ();
}
Finally
{
Mail. Dispose ();
}
}
}

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.