Saturday, January 23, 2016

Sending mail C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Text;

namespace Utils
{
    /// <summary>
/// Summary description for clsEmail
/// </summary>
    public class clsEmail
    {
        public clsEmail()
        {
            //
            // TODO: Add constructor logic here
            //
        }




        #region Sending mail
        /// <summary>
        /// Sending mail with user credentials
        /// </summary>
        /// <param name="strFrom"></param>
        /// <param name="strTo"></param>
        /// <param name="strSubject"></param>
        /// <param name="strBody"></param>

        public static void SendNetMail(string strFrom, string strTo, string strSubject, string strBody,string UserName, string Password)
        {
            System.Net.Mail.SmtpClient smtpClient;
            System.Net.Mail.MailMessage message;
            System.Net.NetworkCredential objCredentials;
            string[] strToAddresses;

            try
            {
                message = new System.Net.Mail.MailMessage();

                message.From = new System.Net.Mail.MailAddress(strFrom);

                strToAddresses = strTo.Split(';');

                if (strToAddresses.Length > 0)
                {
                    for (int intToIndex = 0; intToIndex < strToAddresses.Length; intToIndex++)
                    {
                        if (strToAddresses[intToIndex].Trim() != "")
                        {
                            message.To.Add(strToAddresses[intToIndex].Trim());
                        }
                    }
                }

                message.Subject = strSubject;
                message.Body = strBody;
                message.IsBodyHtml = true;

                smtpClient = new System.Net.Mail.SmtpClient(System.Configuration.ConfigurationManager.AppSettings.Get("SMTPEmailServer"), 25);
                objCredentials = new System.Net.NetworkCredential(UserName, Password);
                smtpClient.EnableSsl = false;//change line later
                smtpClient.UseDefaultCredentials = false;
                smtpClient.Credentials = objCredentials;
                smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                smtpClient.Send(message);
            }
            catch (Exception ex)
            {
                //// Gets the executing web page
                //Page page = HttpContext.Current.CurrentHandler as Page;
                //// Checks if the handler is a Page and that the script isn't allready on the Page
                //if (page != null && !page.ClientScript.IsClientScriptBlockRegistered("alert"))
                //{
                //    page.ClientScript.RegisterClientScriptBlock(typeof(clsBaseMethods), "clientscript", "<script language='javascript'>alert('Mail is unable to send due to an invalid email-id or the mail server configuration is not proper. Please contact your system administrator.')</script> ");
                //}
            }
            finally
            {
                smtpClient = null;
                message = null;
                objCredentials = null;
            }
        }
        #endregion


     

    }
}

No comments:

Post a Comment