How to send and multi-thread round-robin emails to be sent

Source: Internet
Author: User

How to send and multi-thread round-robin emails to be sent

By default, the C # program has a thread. This thread executes the code that starts and ends with the Main method. Every Command executed directly or indirectly by Main is executed by the default thread (or Main thread). When Main returns, this thread will also terminate. However, you can create a secondary thread to execute code in parallel with the main thread. These threads are usually called "Auxiliary Threads ".

Multi-threaded processing solves the problem of throughput and responsiveness, but also brings about resource sharing problems, such as deadlocks and contention states. Multithreading is especially suitable for tasks that require different resources (such as file handles and network connections. Allocating multiple threads to a single resource may cause synchronization problems. threads are frequently blocked to wait for other threads, which is contrary to the original intention of multithreading.

Next I will share with you-how to use multi-thread polling to send emails to be sent. Implementation process: design the sender data table, configure the sender server, and develop the WinForm program for sending.

1. design the producer
The mail sending record contains the sender, recipient, mail title, mail content, increase time, sending time, and failed message.

2. Mail sending Configuration
The C # SMTP protocol is used to send emails. You need to set the sender and sending server information.

3. Implement the WinForm Program
The program interface is designed as the toolbar, the information to be sent, the sending status, and the sending process information. The program is designed to have two timers, one polling the emails to be sent and the other sending the emails.


3. 1. read emails to be sent from the database
Use the timer to poll the sender and read the emails to be sent.


. The thread sends an email
Use a thread to call the sending mail component to send emails.


3. Mail sending
After the email is successfully sent, the status of the email to be sent is updated to "sent". When the email fails to be sent, the status of the email to be sent is updated to "to be sent ", set the next round time to the next 30 minutes.


At this point, the multi-thread round-robin (WRR) function for sending emails has been completed. Multithreading is difficult to debug in C # development. To do this, you need to record the program details and report the thread execution status for developers to maintain. Using a Thread to send an email independently can easily lead to program death. Therefore, I use a timer to call the Thread ).

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.