Send Bulk Mail Using Visual Basic

Introduction:

In this tutorial you will be learning how to send bulk mail in Visual Basic. Steps of Creation: Step 1: First we are going to create a new project with: 5 Text-boxes - Email From, Email Pass, Email To, Subject, Message 1 Button Rename them appropriately to: emailFrom, emailPass, emailTo, subject, msg Step 2: In the button one click event we are going to create an integer which is how many times we are going to send the email and another one to keep the amount sent.
  1. Private Sub button1_Click(sender As Object, e As EventArgs) Handles button1.Click
  2. Dim sent As Integer = 0
  3. Dim toSend As Integer = 1000
  4. End Sub
Step 3: Next lets create the email MailMessage and Import System.Net.Mail
  1. Imports System.Net.Mail
  1. Private Sub button1_Click(sender As Object, e As EventArgs) Handles button1.Click
  2. Dim sent As Integer = 0
  3. Dim toSend As Integer = 1000
  4. Do Until sent => toSend
  5. Try
  6. Dim SmtpServer As New SmtpClient()
  7. Dim mail As New MailMessage()
  8. SmtpServer.Credentials = New Net.NetworkCredential(emailFrom.Text, emailPass.Text)
  9. SmtpServer.EnableSsl = True
  10. SmtpServer.Port = 587
  11. SmtpServer.Host = "smtp.gmail.com"
  12. mail = New MailMessage()
  13. mail.From = New MailAddress(emailFrom.Text)
  14. mail.To.Add(emailTo.Text)
  15. mail.Subject = subject.Text
  16. mail.Body = msg.Text
  17. SmtpServer.Send(mail)
  18. sent += 1
  19. Catch ex As Exception
  20. MsgBox(ex.ToString)
  21. End Try
  22. Loop
  23. End Sub
Step 4: That's the basic idea. We can make it run on a new Thread so it doesn't freeze the program's UI while running.
  1. Private Sub button1_Click(sender As Object, e As EventArgs) Handles button1.Click
  2. Dim trd As Threading.Thread
  3. trd = New Threading.Thread(AddressOf BulkMail)
  4. trd.isBackground = True
  5. trd.Start()
  6. End Sub
  7.  
  8. Private Function BulkMail()
  9. Dim sent As Integer = 0
  10. Dim toSend As Integer = 1000
  11. Do Until sent => toSend
  12. Try
  13. Dim SmtpServer As New SmtpClient()
  14. Dim mail As New MailMessage()
  15. SmtpServer.Credentials = New Net.NetworkCredential(emailFrom.Text, emailPass.Text)
  16. SmtpServer.EnableSsl = True
  17. SmtpServer.Port = 587
  18. SmtpServer.Host = "smtp.gmail.com"
  19. mail = New MailMessage()
  20. mail.From = New MailAddress(emailFrom.Text)
  21. mail.To.Add(emailTo.Text)
  22. mail.Subject = subject.Text
  23. mail.Body = msg.Text
  24. SmtpServer.Send(mail)
  25. sent += 1
  26. Catch ex As Exception
  27. MsgBox(ex.ToString)
  28. End Try
  29. Loop
  30. End Function
Project Completed! That's it! Here is the finished source:
  1. Imports System.Net.Mail
  2. Public Class Form1
  3.  
  4. Private Sub button1_Click(sender As Object, e As EventArgs) Handles button1.Click
  5. Dim trd As Threading.Thread
  6. trd = New Threading.Thread(AddressOf BulkMail)
  7. trd.isBackground = True
  8. trd.Start()
  9. End Sub
  10.  
  11. Private Function BulkMail()
  12. Dim sent As Integer = 0
  13. Dim toSend As Integer = 1000
  14. Do Until sent => toSend
  15. Try
  16. Dim SmtpServer As New SmtpClient()
  17. Dim mail As New MailMessage()
  18. SmtpServer.Credentials = New Net.NetworkCredential(emailFrom.Text, emailPass.Text)
  19. SmtpServer.EnableSsl = True
  20. SmtpServer.Port = 587
  21. SmtpServer.Host = "smtp.gmail.com"
  22. mail = New MailMessage()
  23. mail.From = New MailAddress(emailFrom.Text)
  24. mail.To.Add(emailTo.Text)
  25. mail.Subject = subject.Text
  26. mail.Body = msg.Text
  27. SmtpServer.Send(mail)
  28. sent += 1
  29. Catch ex As Exception
  30. MsgBox(ex.ToString)
  31. End Try
  32. Loop
  33. End Function
  34. End Class
Feel like a challenge? If you want to have a beginner/intermediate test you can try: - Being able to set a custom amount of times to send the emails. - Being able to randomly send a pre-set email a random number of times each time you press the button. Or, alternatively send emails. - Add support for multiple email accounts.

Comments

Add new comment