How to create a simple email blaster using VB.NET

How to create a simple email blaster using VB.NET

Creating a simple email blaster

When you perform email marketing, you will be sending out large quantities of emails to perhaps thousands of recipients. Doing this in a normal email client like Microsoft Outlook is a very tedious process. That’s why most people will either use an email blaster program or engage the services of Email Sending Providers for a fee. Here, we will show you a simple VB.NET command-line program to blast out emails.

Pre-requisites

You will need to have a basic understanding of the VB.NET programming language and you should also have Microsoft Visual Studio 2012 or later installed on your computer.

Creating the mailing list recipients and email template

For the mailing list recipients, create a text file called emails.txt and write one recipient email address per line. As for the email template, create a text file called template.txt and in this file, the first line will be the email subject. The rest of the lines are considered to be the email content.

Compiling the codes

Open Visual Studio and click New Project. Under Templates > Visual Basic > Windows, select Console Application.

Name your project EmailBlaster and then click OK.

In your Visual Studio, create a new Console Application

Once your project has been created, you will see a file called Module1.vb. Edit that file and paste the following code in it. Remember to modify the code for your own settings.

You can modify the values in the following variables:

  • emailfile should contain the full path to the emails.txt file.
  • templatefile should contain the full path to the template.txt file.
  • fromname is the name of the sender, meaning your name.
  • fromemail is the email address of the sender, meaning your email address.
  • smtpusername is the username you use to login to the SMTP server.
  • smtppassword is the password you use to login to the SMTP server.
  • smtpserver is your smtp server hostname.
  • smtpport is the port which is used to connect to the SMTP server.
  • smtpssl should be set to True if your SMTP server requires SSL connections.

Once you are done editing the file, you can just compile the codes to create the commandline program.

VB.NET codes

Imports System
Imports System.Net.Mail
Imports System.IO

Module Module1

    Sub Main()
        'Edit the below to fit your own paths and configurations
        Dim emailfile As String = "C:\path_to_this_file\emails.txt"
        Dim templatefile As String = "C:\path_to_this_file\template.txt"
        Dim fromname As String = "my_name"
        Dim fromemail As String = "my_email_address"
        Dim smtpusername As String = "my_username"
        Dim smtppassword As String = "my_password"
        Dim smtpserver As String = "my_smtp_server"
        Dim smtpport As Integer = 25 'modify if your SMTP server uses a different port
        Dim smtpssl As Boolean = False 'set to True if your SMTP server requires SSL

        'Do not edit anything below this line

        Try
            'reading the template
            Dim subject As String = ""
            Dim body As String = ""
            Using reader As StreamReader = New StreamReader(templatefile)
                ' assume first line is subject
                subject = reader.ReadLine
                body = reader.ReadToEnd
            End Using

            'reading list of email recipients & sending email one by one
            Dim email As String
            Using reader As StreamReader = New StreamReader(emailfile)
                Do While reader.Peek <> -1
                    email = reader.ReadLine
                    If email.Trim <> "" Then
                        Using smtp_server As New SmtpClient
                            smtp_server.UseDefaultCredentials = False
                            smtp_server.Credentials = New Net.NetworkCredential(smtpusername, smtppassword)
                            smtp_server.Port = smtpport
                            smtp_server.EnableSsl = smtpssl
                            smtp_server.Host = smtpserver

                            Dim mesg = New MailMessage()
                            mesg.From = New MailAddress(fromemail, fromname)
                            mesg.To.Add(email)
                            mesg.Subject = subject
                            mesg.IsBodyHtml = False
                            mesg.Body = body
                            smtp_server.Send(mesg)
                            Console.WriteLine("Mail sent to " & mesg.To.ToString)
                        End Using
                    End If
                Loop
            End Using
        Catch Ex As Exception
            Console.WriteLine(Ex.ToString)
        End Try
    End Sub

End Module

Your compiled program should be called EmailBlaster.exe. To start sending emails, just run the program on the command-line.

Was this article helpful?

Related Articles