Thursday, September 20, 2012

Email Manager Class

There are many situations in which you would want to send emails from your application. This class just manages that.

see the code snippet below and please feel free to copy and implement in your application.



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Yournamespace.Core.Emails
{
    public static class EmailManager
    {
        public static void SendEmail(string emailTo, string emailFrom, string emailSubject, string emailMessage)
        {
            List<System.Net.Mail.MailAddress> toList = new List<System.Net.Mail.MailAddress>();
            System.Net.Mail.MailAddress address = new System.Net.Mail.MailAddress(emailTo);

            toList.Add(address);
            SendEmail(toList, null, emailFrom, emailSubject, emailMessage, null);
        }

        public static void SendEmail(List<System.Net.Mail.MailAddress> list, string emailSubject, string emailMessage)
        {
            SendEmail(list, null, null, emailSubject, emailMessage, null);
        }

        public static void SendEmail(List<System.Net.Mail.MailAddress> toList, List<System.Net.Mail.MailAddress> ccList, string emailFrom, string emailSubject, string emailMessage, List<System.Net.Mail.Attachment> attachments)
        {
            System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
            System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();

            try
            {
                //setup client
                client.Host = Configuration.ConfigManager.GetSMTPServer();

                //add all to email addresses.
                if (toList != null)
                {
                    foreach (System.Net.Mail.MailAddress mailAddrs in toList)
                    {
                        msg.To.Add(mailAddrs);
                    }
                }

                //add all cc email addresses.
                if (ccList != null)
                {
                    foreach (System.Net.Mail.MailAddress mailAddrs in ccList)
                    {
                        msg.CC.Add(mailAddrs);
                    }
                }

                if (emailFrom != null)
                {
                    msg.From = new System.Net.Mail.MailAddress(emailFrom);
                }
                else
                {
                    msg.From = new System.Net.Mail.MailAddress(Configuration.ConfigManager.GetDefaultFromAddress());
                }

                msg.Subject = emailSubject;
                msg.Body = emailMessage;

                //add attachments
                if (attachments != null)
                {
                    foreach (System.Net.Mail.Attachment attach in attachments)
                    {
                        msg.Attachments.Add(attach);
                    }
                }

                try
                {
                    client.Send(msg);
                }
                catch (Exception e)
                {
                    throw new Exception(EmailErrors.EmailError_SendEmailFailed.ToString() + " - " + (int)EmailErrors.EmailError_SendEmailFailed + " - " + e.Message);
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                client.Dispose();
                msg.Dispose();
            }
        }
    }
}


No comments:

Post a Comment