Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Speed up sending multiple emails through smtp server using System.Net.Mail

I am quite a newbie in C# but I have learnt a lot from VB.Net about programming in .Net for windows.

I have just made a simple SMTP client which sends emails from the program. It is a console application and can only send one email through the server at a time. This is very slow and I need to send multiple emails through my client at the same time.

Is this possible in C#?

like image 436
rodit Avatar asked Dec 23 '13 16:12

rodit


People also ask

How do I send multiple emails in SMTP?

For smtp you need to have port number, logon email address and in To:"[email protected];[email protected]" …

How to send email using SMTP c#?

Sending emails from C# using an SMTP server requires only a few lines of code: var smtpClient = new SmtpClient("smtp.gmail.com") { Port = 587, Credentials = new NetworkCredential("username", "password"), EnableSsl = true, }; smtpClient. Send("email", "recipient", "subject", "body");

What is SMTP Mailtrap io?

Mailtrap.io represents a cloud-based email testing service with a fake SMTP server under the hood. You send test emails from your app and Mailtrap catches and puts them into a virtual inbox. You can be sure that none of the emails will get to a real user.


1 Answers

simply use multiple threads (multiple processes).

In C# you can do this with a Task.

new Task(delegate { 
    smtpClient.send(myMessage); 
}).Start();

Just wrap your send command in this object and it will be send Asynchronously.

Be careful if this is wrapped in a loop it will start a new process for each mail.

if you need to send large amounts of mails at the same time I suggest you use a ThreadPool. It lets you control how many concurent threads you'd like to have at the same time.

like image 131
Jmorvan Avatar answered Sep 22 '22 10:09

Jmorvan