Send Bulk Email Email with Link using C# and in ASP.Net

Last Reply 4 months ago By pandeyism

Posted 4 months ago

I inherited a large Classic Asp site many years ago. Those in charge were happy to continue that way. 

Only recently have I convinced them that we should start converting to .NET.  I only tell this to let you know I'm a little shy in .NET skills, but am working on them. I have a new project on the site that I'm trying to do all in .NET.  I found the following code and it works fine as is.

Send Bulk (Mass) Email in ASP.Net using C# and VB.Net

However, and you knew there would be one of those, I would like to do something a little different.  I have a SQL table with names and email addresses from folks who have opted-in to receive newsletters. 

So what I want/need to do instead of having a grid view and
checking every name, is to just read the name and email address from the table and send the newsletter to all of them. 

Which would include a link to opt-out as well obviously.  I
know with a little time and research I can figure this out, but those that write my paycheck are in a little bit of a hurry, so any help would be greatly appreciated.


You are viewing reply posted by: pandeyism 4 months ago.
Posted 4 months ago

Hi jrcarr,

Refer below sample.

I have created below sample taking reference to below links -

Send Bulk (Mass) Email in ASP.Net using C# and VB.Net

Send user Confirmation email after Registration with Activation Link in ASP.Net


<asp:Button Text="Send Bulk Email" runat="server" OnClick="SendBulkEmail" />



using System.Net;
using System.Net.Mail;
using System.Data;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Configuration;


Imports System.Net
Imports System.Net.Mail
Imports System.Data
Imports System.Threading.Tasks
Imports System.Data.SqlClient




protected void SendBulkEmail(object sender, EventArgs e)
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    SqlConnection con = new SqlConnection(constr);
    SqlCommand cmd = new SqlCommand("SELECT Id,Name, Email FROM CustomersEmail", con);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataTable dtCustomers = new DataTable();

    string subject = "Welcome Email";
    string body = "To opt-out ";
    body += "<a href = '" + Request.Url.AbsoluteUri.Replace("CS.aspx", "Default.aspx?Email=" + dtCustomers.Rows[0]["Email"].ToString()) + "'>click here</a>.";
    Parallel.ForEach(dtCustomers.AsEnumerable(), row =>
        SendEmail(row["Email"].ToString(), subject, string.Format(body, row["Name"]));

private bool SendEmail(string recipient, string subject, string body)
    MailMessage mm = new MailMessage("", recipient);
    mm.Subject = subject;
    mm.Body = body;
    mm.IsBodyHtml = true;
    SmtpClient smtp = new SmtpClient();
    smtp.Host = "";
    smtp.EnableSsl = true;
    NetworkCredential NetworkCred = new NetworkCredential();
    NetworkCred.UserName = "";
    NetworkCred.Password = "<password>";
    smtp.UseDefaultCredentials = true;
    smtp.Credentials = NetworkCred;
    smtp.Port = 587;
    return true;


protected void Page_Load(object sender, EventArgs e)
    string email = Request.QueryString["Email"];
    // Based on email change the status in database.


Protected Sub SendBulkEmail(sender As Object, e As EventArgs)
    Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Dim con As SqlConnection = New SqlConnection(constr)
    Dim cmd As SqlCommand = New SqlCommand("SELECT Id,Name, Email FROM CustomersEmail", con)
    Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)
    Dim dtCustomers As DataTable = New DataTable()
    Dim subject As String = "Welcome Email"
    Dim body As String = "To opt-out "
    body += "<a href = '" & Request.Url.AbsoluteUri.Replace("CS.aspx", "Default.aspx?Email=" & dtCustomers.Rows(0)("Email").ToString()) & "'>click here</a>."

    Parallel.ForEach(dtCustomers.AsEnumerable(), _
                         Return SendEmail(row("Email").ToString(), subject, String.Format(body, row("Name")))
                     End Function)
End Sub

Private Function SendEmail(ByVal recipient As String, ByVal subject As String, ByVal body As String) As Boolean
    Dim mm As MailMessage = New MailMessage("", recipient)
    mm.Subject = subject
    mm.Body = body
    mm.IsBodyHtml = True
    Dim smtp As SmtpClient = New SmtpClient()
    smtp.Host = ""
    smtp.EnableSsl = True
    Dim NetworkCred As NetworkCredential = New NetworkCredential()
    NetworkCred.UserName = ""
    NetworkCred.Password = "<password>"
    smtp.UseDefaultCredentials = True
    smtp.Credentials = NetworkCred
    smtp.Port = 587
    Return True
End Function


Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    Dim email As String = Request.QueryString("Email")
    'Based on email change the status in database.
End Sub