Sorted By: Tag (ecommerce)

Sending E-mail to All List of Membership Users with ASP.Net Using Built-in Membership API

Most of the Asp.Net developers are using Membership class of Asp.Net and in this blog post we will see how to send e-mail to all of the membership users at once...
2010-11-26 09:23
Tugberk Ugurlu


ASP.Net has a very powerfull built-in membership api and it has all the things we need;

  • User Management
  • CRUD (Create, Read, Update & Delete) of Membership Model
  • User approvement
  • Changing password
  • Recovering password

And the other things. Also, it is safe to say so many developer use this magical thing. 

The registered users are so important to our company and we want to let them know we live and our new things once in a while. As a result, newsletters' main purpose is that.

But how can we send e-mail to all of the registered users inside our magical Membership SQL Database? There are some ways but here we will see the simple and most useful way. We will be talking to database of course in order to get the e-mail addresses but we won't be directly talking to membership database. ASP.Net has already done that for us. 

As Scott Hanselman says, talk is cheap, show me the code Smile Let's start with lesson - 101 : File > New > Project and select ASP.Net Web Application.

file-new-project-vs-2010-pro.pngnew-web-application-cs-vs-2010-pro.PNG

After creating a new project, I will be doing this example on default page but feel free to do it anywhere you like to. Maybe you could create a class library project and that would be nicer given that you could use it in any other projects. So we will be applying the DRY (Don't repeat yourself) rule.

Inside Default.aspx.cs file, I created the below private class;

 

        private class Users {

            public string UserName { get; set; }
            public string EmailAddress { get; set; }
        
        } 

 

Why did I create that? We will see why in a sec. Our main aim here is to send e-mail to our registered users and in order to do that we need all of the users, right? So, we need to talk to database for that. As mentioned before, we won't be using T-Sql for that. In fact, we won't even use linq queries.

Let's write the code of our private IList of Users (this Users class is the class that we just created above)

 

 

        private IList MembershipUserEmailAddresses() {

            IList addresses = new List();

            foreach (var item in Membership.GetAllUsers()) {

                MembershipUser user = (MembershipUser)item;

                addresses.Add(new Users { EmailAddress = user.Email.ToString(), UserName = user.UserName.ToString() });

            }

            return addresses;
        
        } 
        //The below code has been created by tinyMCE by mistake
        //

 

Now we have our data. Pretty easy. Ok, now is the time to write the code of our e-mail send method. Before doing that, make sure that you already configured your web.config file for your host settings. It is pretty straight forward.

 

  
    
      
        
      
    
   

After this little c─▒nfiguration, it is time to write the e-mail send method. So, we have multiple users in hand. We have some options here;

  1. We could send the e-mail to every each user separately
  2. We could send the e-mail to every each user at once by adding them as Bcc

I will demonstrate the 2nd option here. Here is the acctual send method;

 

        private void Send(string Sender, string SenderDisplayName, IList Recivers) {

            MailMessage MyMessage = new MailMessage();
            SmtpClient MySmtpClient = new SmtpClient();

            //Optional
            MyMessage.IsBodyHtml = true;


            //Required Fields
            MyMessage.From = new MailAddress(Sender, SenderDisplayName);
            MyMessage.Subject = "subject of the message";
            MyMessage.Body = "My Message... Here is my message";

            for (int i = 0; i < Recivers.Count(); i++) {

                MyMessage.Bcc.Add(new MailAddress(Recivers[i].EmailAddress,Recivers[i].UserName));

            }

            MySmtpClient.Send(MyMessage);
        
        } 

        //The below code has been created by tinyMCE by mistake
        //

 

As you see, it is not so complicated. Of course, it could be much more pretty but I figured that we are demonstrating the function here, so I made a quick and simple one.

 

What do we do now? Finally, it has come to fruition. I will fire it up on the page_load event but you could do it wherever you want;

 

        protected void Page_Load(object sender, EventArgs e) {

            Send("info@example.com", "My Name", MembershipUserEmailAddresses());

        } 

 

That's it. It is very simple and straight forward.

You could do this by connecting to sql directly if you need any further fillitering but it is the way it works and I guess you get the idea. 

Enjoy your coding Smile

The whole code

using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;

namespace MembershipUsersSendEmail
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e) {

            Send("info@example.com", "My Name", MembershipUserEmailAddresses());

        }

        private class Users {

            public string UserName { get; set; }
            public string EmailAddress { get; set; }
        
        }

        private IList MembershipUserEmailAddresses() {

            IList addresses = new List();

            foreach (var item in Membership.GetAllUsers()) {

                MembershipUser user = (MembershipUser)item;

                addresses.Add(new Users { EmailAddress = user.Email.ToString(), UserName = user.UserName.ToString() });

            }

            return addresses;
        
        }

        private void Send(string Sender, string SenderDisplayName, IList Recivers) {

            MailMessage MyMessage = new MailMessage();
            SmtpClient MySmtpClient = new SmtpClient();

            //Optional
            MyMessage.IsBodyHtml = true;


            //Required Fields
            MyMessage.From = new MailAddress(Sender, SenderDisplayName);
            MyMessage.Subject = "subject of the message";
            MyMessage.Body = "My Message... Here is my message";

            for (int i = 0; i < Recivers.Count(); i++) {

                MyMessage.Bcc.Add(new MailAddress(Recivers[i].EmailAddress,Recivers[i].UserName));

            }

            MySmtpClient.Send(MyMessage);
        
        }
    }
} 

//The below code has been created by tinyMCE by mistake
//

Tags