Sending Mass/Bulk E-mail Using ASP.NET

Mass/Bulk E-mail Sending

Mass/Bulk E-mail Sending

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)
            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
                    End If
        Catch ex As Exception
        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:

Mass Sender WebForm

In the background you just need an implementation of the MailMessage and SmtpClient classes:

    Private Sub StartCampaign()
        Dim connection As New SqlConnection("Data,1433;" & _
                                            "Initial Catalog=MyDB;" & _
                                            "User ID=xxxxxx;Password=xxxxxxxx;")

            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
            End While
        Catch ex As Exception
            ' ToDo: something about the exception if any
        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("", "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)
    End Sub

That’s it my friends. You just learned the basics of sending bulk email. Congrats!!


14 thoughts on “Sending Mass/Bulk E-mail Using ASP.NET

  1. I will met the requirement that need to send mass emails. Please can you tell me all the requirement. I will be grateful.

  2. Hey,

    Can you please help me with this, i really need some thing like that, i don’t understand things on the page? i want to have a software that can do this, so please if you can help me let me know.

  3. I am designing one application i have a need to sent one messege to multiple people with compose mail functionality. i have a database in that database stored many records i want to retrive mail ids and then sent emails to all people. can u help me.

  4. thanks for the code. but while trying for bulk emails like more than 100 email address it takes more than 7mins. Is there any solution where in we can reduce the time? Imagine sending to 1000s

  5. @AKS it could be something on your side as when i tested it took like very few seconds for sending 20+ messages. In addition, you could try to put all the email addresses into generic list of string type and then call the sendemail method when iterating this list. It could happen that reader is slow for some reason. Give it a try and let me know if it helps.

Leave a Reply

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