Sending Bulk Email using

Simple function to send bulk email within

After establishing the connection using a OLEDBConnection, we can alternatively use SQLConnection and SQLCommand, we are maintaining count of emails sent and checking for a valid datareader and connection. If all goes well just populate the object with the requisite values, we can just send across the mails

'Bulk email sending
Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
'Opening Connection for collecting email addresses from Database
'I used UDL file to save connection string
Dim myDataReader As OleDbDataReader
Dim myOleDbConnection As OleDbConnection
Dim myOleDbCommand As OleDbCommand
myOleDbConnection = New OleDbConnection(ConfigurationSettings.AppSettings("UDLPath"))
myOleDbCommand = New OleDbCommand("SELECT * FROM Emails", myOleDbConnection)

Dim SentEmailCount As Integer
Dim EmailCount As Integer
SentEmailCount = EmailCount = 0

myDataReader = myOleDbCommand.ExecuteReader()
Do While (myDataReader.Read())
'Sending Email and Counting!
If SendEmail(txtFromName.Text, txtFromEmail.Text, _
myDataReader.GetString(0), txtTitle.Text, txtBody.Text, CInt(drpPriority.SelectedValue)) Then
SentEmailCount = SentEmailCount + 1
End If
EmailCount = EmailCount + 1
lblMessage.Text = "Email Sent Count: " & SentEmailCount.ToString & "
" & _
"Total Email Counts: " & EmailCount.ToString
Catch ex As Exception
'A database connection error possibly occures!
lblMessage.Text = ex.ToString()
' Always call Close when done reading.
If Not (myDataReader Is Nothing) Then
End If

' Close the connection when done with it.
If (myOleDbConnection.State = ConnectionState.Open) Then
End If
End Try
End Sub

'Send mail small module!
Private Function SendEmail(ByVal FromName As String, ByVal FromEmail As String, ByVal ToEmail As String, ByVal Title As String, ByVal Body As String, ByVal Prior As Integer) As Boolean
Dim mes As New MailMessage
With mes
.From = "<" & FromEmail & ">" & FromName
.To = ToEmail
.Subject = Title
.Priority = Prior
.Body = txtBody.Text
SmtpMail.SmtpServer = ConfigurationSettings.AppSettings("SMTPServerName")
End With
Return True
Catch ex As Exception
Return False
End Try
End Function


Author: Er. Ram Singh06 Sep 2010 Member Level: Gold   Points : 2

using System;using System.Data;using System.Configuration;using System.Collections;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using System.Net.Mail;public partial class Default : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) {//Calling the function SendMail Response.Write( SendMail("","","","Test Mail","Test Mail Body")); } public string SendMail(string toList, string from, string ccList, string subject, string body) { MailMessage message = new MailMessage(); SmtpClient smtpClient = new SmtpClient(); string msg = string.Empty; try { MailAddress fromAddress = new MailAddress(from); message.From = fromAddress; message.To.Add(toList); if (ccList != null && ccList != string.Empty) message.CC.Add(ccList); message.Subject = subject; message.IsBodyHtml = true; message.Body = body; smtpClient.Host = ""; smtpClient.Port = 25; smtpClient.UseDefaultCredentials = true; smtpClient.Credentials = new System.Net.NetworkCredential("", "password"); smtpClient.Send(message); msg = "Successful"; } catch (Exception ex) { msg = ex.Message; } return msg; }}

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: