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

Last Reply one month ago By pandeyism

Posted one month 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.

Thanks

Posted one month 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

HTML

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

Namespaces

C#

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

VB.Net

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

Code

C#

CS.aspx.cs

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();
    da.Fill(dtCustomers);

    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("sender@gmail.com", recipient);
    mm.Subject = subject;
    mm.Body = body;
    mm.IsBodyHtml = true;
    SmtpClient smtp = new SmtpClient();
    smtp.Host = "smtp.gmail.com";
    smtp.EnableSsl = true;
    NetworkCredential NetworkCred = new NetworkCredential();
    NetworkCred.UserName = "sender@gmail.com";
    NetworkCred.Password = "<password>";
    smtp.UseDefaultCredentials = true;
    smtp.Credentials = NetworkCred;
    smtp.Port = 587;
    smtp.Send(mm);
    return true;
}

DefaultCS.aspx.cs

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

Vb.aspx.vb

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()
    da.Fill(dtCustomers)
    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(), _
                     Function(row)
                         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("sender@gmail.com", recipient)
    mm.Subject = subject
    mm.Body = body
    mm.IsBodyHtml = True
    Dim smtp As SmtpClient = New SmtpClient()
    smtp.Host = "smtp.gmail.com"
    smtp.EnableSsl = True
    Dim NetworkCred As NetworkCredential = New NetworkCredential()
    NetworkCred.UserName = "sender@gmail.com"
    NetworkCred.Password = "<password>"
    smtp.UseDefaultCredentials = True
    smtp.Credentials = NetworkCred
    smtp.Port = 587
    smtp.Send(mm)
    Return True
End Function

DefaultVB.aspx.vb

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