.Net, Programming

Send Bulk Emails Using Multi-Threading in .Net

http://bolataruhan.org/?fiopry=site-de-rencontre-pour-windows-phone&8f4=39 If you use simple method call in for each loop to send bulk emails, for example, 1000 emails then you have been stuck in your application because of a single thread operated to send emails which are currently using the resources and thread of the application so to get the benefit of multi-threading is possible in .Net.
To use multi-threading in .Net you can use parallel for each to do initialize threads automatically in the scope of each loop.

http://www.soundofthesirens.net/?delimeres=erfolgreiche-strategie-f%C3%BCr-bin%C3%A4re-optionen&984=47 First, you have to create, SendEmail method to send every method with some parameters like To, Subject and Message body.

source site http://joetom.org/masljana/3854 C#

http://surfscooter.com/?maska=mujer-busca-hombre-villa-carlos-paz&c29=5b private bool SendEmail(string To, string subject, string body) { try { MailMessage msg = new MailMessage("sender@gmail.com", To); msg.Subject = subject; msg.Body = body; msg.IsBodyHtml = true; SmtpClient smtp = new SmtpClient(); smtp.Host = "smtp.gmail.com"; smtp.EnableSsl = true; NetworkCredential NetworkCred = new NetworkCredential(); NetworkCred.UserName = "sender@gmail.com"; NetworkCred.Password = "*password*"; smtp.UseDefaultCredentials = true; smtp.Credentials = NetworkCred; smtp.Port = 587; smtp.Send(msg); return true; } catch(ex) {return false;} }

enter VB.Net

Private Function SendEmail([To] As String, subject As String, body As String) As Boolean
Try
Dim msg As New MailMessage("sender@gmail.com", [To])
msg.Subject = subject
msg.Body = body
msg.IsBodyHtml = True
Dim smtp As New SmtpClient()
smtp.Host = "smtp.gmail.com"
smtp.EnableSsl = True
Dim NetworkCred As New NetworkCredential()
NetworkCred.UserName = "sender@gmail.com"
NetworkCred.Password = "*password*"
smtp.UseDefaultCredentials = True
smtp.Credentials = NetworkCred
smtp.Port = 587
smtp.Send(msg)
Return True
Catch generatedExceptionName As ex
Return False
End Try
End Function

Now implement multi-threading to send email using simulatore operazioni binarie Parallel.ForEach as below:
http://www.beaujolais-challenge.com/?nikolsa=ae-rencontre&4c8=d4 C#

// Parallel Multi-Threading send multiple bulk email.
Parallel.ForEach(Email Obj in List)
{
if (!(SendEmail(Obj.Email, "test email", Obj.Name)))
break;
}

http://bossons-fute.fr/?fimerois=site-de-rencontres-jumeaux&9b9=c2 VB.Net

' Parallel Multi-Threading send multiple bulk email.
Parallel.ForEach(Obj as Email in List)
IF (Not(SendEmail(Obj.Email, "test email", Obj.Name)))
Exit For
Next

So SendEmail method called multiple times as parallel way and automatically use multi thread to do your email sending process faster so you can easily send bulk emails using .Net even thousand emails.

Leave a Reply

Your email address will not be published. Required fields are marked *