Sunday, December 23, 2012

Creating the .NET Solution Part 5

Email

Sending emails is one task that every website has to be capable of. How many emails you plan to send should certainly determine how you go about sending that email. As we do not yet know how many emails we plan to send, we will initially rely upon the tools that are provided in the .NET framework to send our email. For brevity, we will look at the specific methods from this class here.
As with all our objects, we will have the MEF Export attribute in place that makes this a Pluggable class. The class itself inherits from our IEmail interface. We will have a couple of constants declared—one for the website's receiving the email account and another for the website's sending the email account. (We could have used one variable for both, but a little flexibility never hurt anyone!)
  1. We will continue from last post Creating the .NET Solution Part 4 open the Fishbook solution.
  2. Right click in Interfaces project and select Add/New Item.
    Create IEmail Interface
    Create IEmail Interface
      1. Select Interface Item from C# Template and name it IEMail.cs and then write the following code:
        namespace Fishbook.Interfaces
        {
            public interface IEMail
            {
                void SendEmail(string From,string Subject,string Message);
                void SendEMail(string To,string CC,string BCC,string Subject,string Message);
                void SendEMail(string To[],string[] CC,string[] BCC,string Subject,string Message);
                void SendIndividualEmailsPerRecipient(string[] To,string Subject,string Message);
            }
        }


      2. Right click in Components project and select Add/New Item.
        Create EMail Class
        Create EMail Class
        1. Select Class Item from C# Template and name it EMail.cs and write the following code:
          using System.Net.Mail;
          using System.ComponentModel.Composition;
          using Fishbook.Interfaces;


          namespace Fishbook.Components
          {
              [Export(typeof(IEmail))]
              public class Email : IEmail
              {
                  const string TO_EMAIL_ADDRESS = "website@fishbook.com";
                  const string FROM_EMAIL_ADDRESS = "website@fisharoobook
                  public void SendEmail(string From, string Subject, string Message)
                  {
                      MailMessage mm = new MailMessage(From,TO_EMAIL_ADDRESS);
                      mm.Subject = Subject;
                      mm.Body = Message;

                      Send(mm);
                  }

                  public void SendEmail(string To, string CC, string BCC, string Subject, string Message)
                  {
                      MailMessage mm = new MailMessage(FROM_EMAIL_ADDRESS,To);
                      mm.CC.Add(CC);
                      mm.Bcc.Add(BCC);
                      mm.Subject = Subject;
                      mm.Body = Message;
                      mm.IsBodyHtml = true;

                      Send(mm);
                  }

                  public void SendEmail(string[] To, string[] CC, string[] BCC, string Subject, string Message)
                  {
                      MailMessage mm = new MailMessage();
                      foreach (string to in To)
                      {
                          mm.To.Add(to);  
                      }
                      foreach (string cc in CC)
                      {
                          mm.CC.Add(cc);
                      }
                      foreach (string bcc in BCC)
                      {
                          mm.Bcc.Add(bcc);
                      }
                      mm.From = new MailAddress(FROM_EMAIL_ADDRESS);
                      mm.Subject = Subject;
                      mm.Body = Message;
                      mm.IsBodyHtml = true;

                      Send(mm);
                  }

                  public void SendIndividualEmailsPerRecipient(string[] To, string Subject, string Message)
                  {
                      foreach (string to in To)
                      {
                          MailMessage mm = new MailMessage(FROM_EMAIL_ADDRESS,to);
                          mm.Subject = Subject;
                          mm.Body = Message;
                          mm.IsBodyHtml = true;

                          Send(mm);
                      }
                  }

                  private void Send(MailMessage Message)
                  {
                      SmtpClient smtp = new SmtpClient();
                      smtp.Send(Message);
                  }
              }
          }

        SendEmail() method is a bit different from the others in that it is used for the site to send email to another
        site rather than to a user. The user of this method will provide the email address that the message is from, the subject and the message. This method would be used in a 'Contact Us' page or a similar mail form. At the bottom of this method, you can see a Send() method call. This method spins up an SmtpClient and sends the email message as do each of the other SendEmail() methods. 
        The remaining SendEmail() methods are used in various ways for the site to send an email to the users of the site. These allow sending email to a single user (single user for To, CC, and BCC inputs) or to an array of users (multiple users for To, CC, and BCC inputs). The SendIndividualEmailsPerRecipient () method accepts multiple user names but sends individual emails per user.
        Finally, we get to the Send() method that is used by all of the other methods. This method is responsible for actually sending the emails. But before this method performs any action, we need to add the following section to our web.config just after the configuration tag. It is responsible for telling the .NET framework how to connect to our mail server:
        <system.net>
            <mailSettings>
              <smtp>
                <network host="localhost" port="25"
                defaultCredentials="true" />
              </smtp>
            </mailSettings>
          </system.net>

        This configuration is very flexible in how it sends the email. However, it still requires the webpage to be responsible for sending emails directly. This creates a page with lots of overheads given that there could be a lot of recipients to process, or a lot of network lag involved in the transactions.
        The nice thing about having this class is that we can easily create another class that implements the IEmail interface but uses a mail queue instead of requiring the page to send the email. This would allow the website to just create mail messages and put them in the queue, which is much faster than actually processing and sending the emails. We could then have a queue processor somewhere that would be responsible for sending our emails.

        No comments:

        Post a Comment

        Automatic Traffic Exchange

        YallaTech Facebook page