Step 3 of asp.net development. net Mail Batch Sending

Source: Internet
Author: User
Tags mailmessage smtpclient

In a recent project, there was a function of sending payroll in batches, which seemed mysterious and very simple. In the past, we knew that. net had a namespace dedicated to Email sending. This time, we can practice it. I would like to praise. net again here. Haha is so easy to handle and implement.

Let's talk about the theme. The function is required to trigger the operation after the salary report is generated every month. It can be sent to the mailboxes of employees in the address book in batches. So I wrote a class for sending emails, and used it directly when using the heel. The namespace for sending emails is System. Net. Mail, which is also very easy to use (see the code below 〕

 

 /// <Summary>
/// Send an email
/// </Summary>
/// <Param name = "mailHs"> email set hs [key: email address # recipient name [Code]; value: email content] </param>
/// <Param name = "smtpAuthUsername"> sender's email address </param>
/// <Param name = "smtpAuthPassword"> email password </param>
Public void Send (Hashtable mailHs, string smtpAuthUsername, string smtpAuthPassword, string smtpServer, string titel, string subject)
{
String meg = string. Empty;


// Define the transmission protocol
System. Net. Mail. SmtpClient smtp = new System. Net. Mail. SmtpClient (smtpServer );
// Set the sender for authentication
Smtp. Credentials = new System. Net. NetworkCredential (smtpAuthUsername, smtpAuthPassword );

// Obtain the sending status after asynchronous sending is completed
// Smtp. SendCompleted + = new System. Net. Mail. SendCompletedEventHandler (SendCompletedCallback );
System. Net. Mail. MailMessage mail;

Foreach (string var in mailHs. Keys)
{
String [] list = var. Split ('#');
Try
{
Mail = new System. Net. Mail. MailMessage ();
Mail. From = new System. Net. Mail. MailAddress (smtpAuthUsername, titel );
// Reply to person
Mail. ReplyTo = new System. Net. Mail. MailAddress (smtpAuthUsername, titel );
// Recipient
Mail. To. Add (list [0]);
// Mail priority
Mail. Priority = System. Net. Mail. MailPriority. Normal;
// Set html mail
Mail. IsBodyHtml = true;
// Title
Mail. Subject = subject;
// Content
Mail. Body = mailHs [var]. ToString ();

Smtp. Send (mail); //;, list [1]);
Meg = string. Format ("{0} {1} email sent successfully. ", System. DateTime. Now. ToString (), list [1]);
WriteLog (meg );
}
Catch
{
Meg = string. Format ("{0} {1} failed to send the email. ", System. DateTime. Now. ToString (), list [1]);

WriteLog (meg );
}
}
}

 

 

In the code, I use synchronous sending of mails and asynchronous sending, but both methods have their own application scenarios. Because my functional requirement is to send payroll records in batches, the information of each person in my mailbox is different. Therefore, synchronous transmission is used, hashtable is used to store the address and content, and a loop is followed to complete sending. If you use batch mail to send multiple recipients in batches, you can use Asynchronous sending. This will improve the performance and avoid the loss of large data volumes. Remember to use this callback in asynchronous mode:

Smtp. SendCompleted + = new System. Net. Mail. SendCompletedEventHandler (SendCompletedCallback );

In this way, you can write some subsequent processing in this method, such as sending feedback. My function is to write a log file in a log.txt file. In this way, after sending, you can view the sending result. After synchronization is completed, it is OK to write a method. Simple and practical.

Shared! Welcome! Although the knowledge is small, it focuses on accumulation.

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.