Sorted By: Month (11) and Year (2010)

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
//

Is There Anybody Out There Who Doesn't Have Gravatar !

Sometimes we get sick to put our avatar pic. to every web site that we have registered to view our avatar. In this point gravatar.com helps us.
2010-11-19 22:14
Tugberk Ugurlu


Sometimes we get sick to put our avatar pic. to every web site that we have registered to view our avatar. In this point gravatar.com helps us. 

Gravatar works very basicly and securely. Firstly, you need to go to gravatar.com and sing up. After uploading your avatar and configure it with your e-mail address, your are basically done.

When you are writing a comment to a blog post, you are mostly required to enter your e-mail address and that e-mail address is enough to put your global avatar along with your comment. Of course, this is valid if the web site accepts gravatar pictures. 

Do not panic whether your e-mail address will be on picture's url. It won't. The picture url will similarly looks like that;

http://www.gravatar.com/avatar.php?gravatar_id=17698e3ad0e0dc70853cddda166bc573&size=80&default=identicon

You could easlily take advantages of gravatar and use it on your website. You could read implementation guidance for more information.

If you are using ASP.Net MVC and you are familiar with NuGet, the implementation will be so easy.

Implementation of Gravatar on ASP.Net MVC

  1. Write this command on Package Management Console : PM> install-package Microsoft.MVC.Helpers
  2. After the instalation, you will see Microsoft.MVC.Helpers.Gravatar.GetHTML(...) method. That method will generate the gravatar automatically.

Hope this helps !

TourismGeek.Com, Tourism Professionals Gathering Point...

Currently I am working on an ASP.Net MVC project which will be a blog spot for tourism professionals. If all goes as planned, it will be online at the beginning of new year !
2010-11-14 13:35
Tugberk Ugurlu


Update on the 28th of April, 2011

I had to postpone the project for a while. So there is no such an existing thing yet.

For over a month, I have been working on an ASP.Net MVC 2.0 Project which will be a blog spot for tourism professionals. If all goes as planned, it will be online at the beginning of new year ! 

The website will a multilingual website and it will offers greate amonth of academical tourism resources. TourismGeek.Com will not be blog website of a person, it will be the collection of artciles from tourism profecionals, experts and undergraduate students.


The Brief Information About TourismGeek.Com and Introduction


TourismGeek.Com is not a web site. It would be so insulting to call it 'web site' ! Then, what is this ? Here some of the explanatory words that I've come up with but not sure if they fit;

  • Tourism article database
  • Tourism community gathering place
  • The thing that tugberk has created to satisfy his egos ! (Most likely to me !)
  • Best friend of a toruism student, an intern and even an expert...
After a couple of days, you will figure out on your own what this web site is. So there is no need you to understand it now !

The articles you will see on this website will be all about tourism or a major which is somehow related to tourism (economics, business administration, computer science, etc.) and written by toursim professionals (experts, lecturers, undergraduate students, etc.).

One of the main aim of this web site [not that there are so many aims of it :)] is to make the information easily reachable. How so? Let's see;

  • The articles of the professionals will be under tourismgeek.com/{here is the article number} ! For instance, tourismgeek.com/100 ! That easy ! Not complex, crazy, mind exploding urls !
  • Professionals writting on this website has a single profile. Wanna see their profile? Easy ! Browse this : tourismgeek.com/{here is the user name of the professional} ! For instance, tourismgeek.com/tugberk ! That easy ! Not complex, crazy, mind exploding urls !
  • Looking for something specific? Easy ! Write it on the search box on the top-right side and search !

We will rumble with tourismgeek.com soon !

Tags