I was asked some time ago to develop an application which involved sending multiple email messages. Later i realized that it is known as mass mail sender.
Means, if you launch an email marketing campaign sending a targeted newsletters and advertisements, you need a mass/bulk sender for that.
Although i have never used any, in fact i know how they work. Hey – you’re talking to the email king here. I am just kidding … let’s move on!
The spammers (i hope you are not one of them) usually are getting mail lists in Text format *.txt so they need the sender software to supports it. Once the sender imports the list it starts sending your newsletter/advert to all email addresses from the just imported list. Pretty cool right?
Well, if you thought so then i must dissapoint you. There are a lot of requirements that must be met before you will be able to use your sender. You need to set up reverse DNS (PTR Record), Domain keys, SPF records, MX records, Get your IP address whitelisted and much more but, it is out of scope of this post.
As mentioned earlier we should first import the email addresses from the text file(s). So let’s do that. I assumed that you keep the text lists in Email_Lists folder.
Protected Sub btnImport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnImport.Click Dim connection As New SqlConnection(ConfigurationManager.ConnectionStrings("MyConn").ConnectionString) Try connection.Open() For Each txt As String In Directory.GetFiles(Server.MapPath("Email_Lists")) Dim emails() As String = IO.File.ReadAllLines(txt) Dim reg As New System.Text.RegularExpressions.Regex("\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*") For Each email As String In emails If reg.IsMatch(email) Then Dim cmd As New SqlCommand() cmd.Connection = connection cmd.CommandText = "INSERT INTO EmailAddresses (Email) VALUES(@email)" cmd.CommandType = Data.CommandType.Text cmd.Parameters.Add("@email", Data.SqlDbType.VarChar).Value = email cmd.ExecuteNonQuery() cmd.Dispose() End If Next Next Catch ex As Exception Response.Write(ex.Message) Finally connection.Close() End Try End Sub
Ok we imported the new email addresses so now we are ready to move on.
The next step would be to send your newsletter or advert to the newly added recipients. I will not bother explaining all of it, but only those necessary parts, so here we go.
From the design aspect you should end up with something like this:
In the background you just need an implementation of the MailMessage and SmtpClient classes:
Private Sub StartCampaign() Dim connection As New SqlConnection("Data Source=xx.xxx.xxx.xxx,1433;" & _ "Initial Catalog=MyDB;" & _ "User ID=xxxxxx;Password=xxxxxxxx;") Try connection.Open() Dim command As New SqlCommand("SELECT TOP(@count) Email FROM EmailAddresses", connection) command.Parameters.Add("@count", Data.SqlDbType.Int).Value = Me.txtNumberOfMessages.Text Dim reader As SqlDataReader = command.ExecuteReader While reader.Read ' send new email to all returned addresses SendEmail(reader(0).ToString) End While reader.Close() Catch ex As Exception ' ToDo: something about the exception if any Finally connection.Close() End Try End Sub Private Sub SendEmail(ByVal email As String) ' declare new mail message Dim message As MailMessage = New MailMessage ' if you plan to have html content then enable property IsBodyHtml message.IsBodyHtml = True ' set the message subject message.Subject = Me.txtSubject.Text.Trim message.From = New MailAddress("firstname.lastname@example.org", "Your Company Name - Newsletter") message.To.Add(New MailAddress(email)) ' set the message body message.Body = Me.txtMessage.Text ' declare new smtpclient to send our message Dim mailClient As SmtpClient = New SmtpClient(Me.txtSmtp.Text, Me.txtSmtpPort.Text) mailClient.Send(message) message.Dispose() End Sub
That’s it my friends. You just learned the basics of sending bulk email. Congrats!!