Write For Us

Newsletter in ASP.NET - How To Create It

This tutorial intend to teach you how to create newsletter and send mass e-mails in ASP.Net.


Because we will not send a spam e-mails to everyone, we need a registration form, so web site visitors can register to receive newsletter.

First, we're going to see how to send a confirmation email, when somebody registers in a website. This is very common, suppose you want to register a new account on some online service. If you go to the registration form and fill in all the necessary data, once you submit the form, you will most probably receive an email from the company with a message saying welcome to the site, or having your personal informations such as your username, password, name, etc...

This tutorial will show only basics about how to send an e-mail. Detail instructions, about how to send attachments, make HTML e-mails, possible errors etc. you can see on E-mail in ASP.NET tutorial.

So to do start this Newsletter project, open up visual studio 2005, and create a new project there.

Note, you can download complete Newsletter Visual Studio project, used in this tutorial.

So, call your project "Mass_Newsletter". The language we're going to cover is Visual Basic, so make sure this is chosen from the Language List.

Design a registration form. Usually this form will be connected to a database and the values you enter will be recorded with the database, but since we're talking about the mass email in this tutorial, we are not going to cover how to save the data in the database. Make your form similar the form in the image below:

Clearly, the sites often request more details, but this is enough informations for testing our mass-email system.
Next, we will need to implement how to send the email.
Firstly, we will need to import the "Mail" class provided by visual studio. To do so, go to your code page and add the following:

' Code starts here
Imports System.Web.Mail
' Code ends here

Make sure you add it to the very top of your page, just above the declaration of the class.
Now double click the submit button, to handle the onClick event. When you do so, add the following code:

' Code starts here
  Dim theMessage As String
  theMessage = "Thank you for subscribing in our site<br/>"
  theMessage += "This email contains your membership details. Please keep it safe<br/>"
  theMessage += "Username: " + txtUsername.Text + "<br/>"
  theMessage += "Password: " + txtPassword.Text + "<br/>"
  theMessage += "Thanks for using our website"
  Dim mailMessage As New MailMessage()
  mailMessage.From = "subscription@yourwebsite.com"
  mailMessage.To = txtEmail.Text
  mailMessage.Subject = "New subscriber"
  mailMessage.BodyFormat = MailFormat.Html
  mailMessage.Body = theMessage
  mailMessage.Priority = MailPriority.High
  SmtpMail.SmtpServer = "mail.yourwebsite.com"
Catch ex As Exception
End Try
' Code ends here

This is pretty simple, at the beginning we defined a string called "theMessage" that holds the content of the email. Note that we can use html here, and it will be displayed correctly in the email, because we specified the "BodyFormat" to be MailFormat.Html
Then we defined who will receive the message, and who is the sender (mailMessage.To and mailMessage.From).
The priority method is related to the important of the email. It will be sent quicker if you have MailPriority.High, which is our case here.

The SmtpServer, is simply the server that we gonna use to send the email, this is usually on the form of: mail.yourwebsite.com
Make sure also to change "mail.yourwebsite.com" with your real website, and the fields related to it, such as "subscription@yourwebsite.com", etc...

At this moment, you should take into consideration that, the new subscriber should be saved in the database. Because later on we are going to send a newsletter email to all the users. To do that, you should add code just before sending the email method, to save the username information in a specific table, let's call it "tbl_subscribers".

This project cannot be tested offline, because it needs an SMTP server, and therefore you will have to upload the project to your own server to test it out.

It will also be a good idea to redirect the page to a new page, after sending the email, most likely a thank you page such as the one in the image below:

Now we are going to create a new page, for the administrator to send newsletter emails to all the users. The technique is quite the same, but we should consider every single user in our database. For this reason, create a new page and call it "Send_New_Mail.aspx". Design your page to be similar to the following image:

I have attached a small access database with the exercice files, that contains few records for emails of people. We will use these records to send out the newsletters email.
First thing first, go at the begining of the page and add the following:
Imports System.Threading

Then create a new function like the function below:

Private Sub SendEmails()
        Dim myConnectionString As String
        myConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Context.Server.MapPath("Database\") & "myDB.mdb" & ";"
        Dim con As New System.Data.OleDb.OleDbConnection
        con.ConnectionString = myConnectionString
        Dim adp As New System.Data.OleDb.OleDbDataAdapter("select * from tbl_subscribers", con)
        Dim dt As New System.Data.DataTable
        For i As Integer = 0 To dt.Rows.Count - 1
        Dim mailMessage As New MailMessage()
        mailMessage.From = "administrator@yourwebsite.com"
        mailMessage.To = dt.Rows(i).Item("myEmail")
        mailMessage.Subject = txtSubject.Text
        mailMessage.BodyFormat = MailFormat.Html
        mailMessage.Body = txtContent.Text
        mailMessage.Priority = MailPriority.High
        SmtpMail.SmtpServer = "mail.yourwebsite.com"
        lblStatus.Text = "All emails sent successfully"
    Catch ex As Exception
    End Try
End Sub

We will call this function in a new thread, instead of simple call on button click. This is important because, when we try to send Newsletter emails, often could be a lot of subscribers, and the application might return a runtime timeout error, besides that, shared hosting timeout is usually 30 seconds, so it is better to go with scalable solution with threading, that can hold both small and large mail lists.

Now double click the "Send Email" button to handle the onClick event. Add the following Code:

' Code starts here
Dim trd As Thread
trd = New Thread(AddressOf SendEmails)
'Code ends here

Again, this is not gonna work offline. You should test it out on your own server.

Nothing really complicated, the trick here was, to use the same method we provided above, but instead of sending the email to a specific person, we're having a For Loop, to pass through every single email and send out that email to, besides creating the new thread at runtime.
Also, we are reading the Mail Subject, Body and To... from the textboxes we created and our database.

You can make this application even better. With every new facility it will look more professional. If you have strong demands but don't have time to reinvent the wheel, you can get fast results with some advanced solution, like Absolute Newsletter.

I hope you enjoyed learning from this tutorial. Happy programming!

This tutorial is written by Tea Maker.

Tutorial toolbar:  Tell A Friend  |  Add to favorites  |  Feedback  |   Google