Tuesday, April 12, 2011

emailcode using in handler

using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Configuration;
using System.Net.Mail;
using System.Net.Configuration;

//using System.Web.Mail;



public class EmailHandler : IHttpHandler {

    string result = null;
    int id = 0;
    String strMethodName = String.Empty;

    string smtpHost;
    string smtpport;
    string smtpuser;
    string smtppwd;
    SmtpClient client = null;

   
    SqlConnection con = null;
    DataSet ds;
   StringBuilder sb = new StringBuilder();

   
    public void ProcessRequest (HttpContext context) {
       
         con = new SqlConnection("Data Source=CRM1-PC;Initial Catalog=LibraryApp;User ID=sa;Password=hyd@345");
        context.Response.ContentType = "text/plain";
        strMethodName = "sendemail";

        if (!String.IsNullOrEmpty(context.Request.QueryString["StrMethodName"]))
        {
          //  strMethodName = Convert.ToString(context.Request.QueryString["StrMethodName"]);
            strMethodName = "sendemail";
        }

       

        //if (!String.IsNullOrEmpty(context.Request.Form["CategoryId"]))
        //{
        //    categoryId = Convert.ToInt32(context.Request.Form["CategoryId"]);
        //}


        //if (!String.IsNullOrEmpty(context.Request.QueryString["StrMethodName"]))
        //{
        //    strMethodName = Convert.ToString(context.Request.QueryString["StrMethodName"]);
        //}

        if (strMethodName.Length > 0 && strMethodName.Equals("sendemail"))
        {

            bool Result = SendMail("giribhushan1988@gmail.com","hi","hello");
            context.Response.Write(Result);
          
        }
      
     
    }



   
    private static MailMessage message = null;


    public bool SendMail(string toMailAddress, string mailSubject, string mailMessage)
    {
        Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
        MailSettingsSectionGroup settings = (MailSettingsSectionGroup)config.GetSectionGroup("system.net/mailSettings");
         smtpHost = settings.Smtp.Network.Host;
         smtpport = settings.Smtp.Network.Port.ToString();
         smtpuser = settings.Smtp.Network.UserName;
         smtppwd = settings.Smtp.Network.Password;
       
     



        string MessageBody = string.Empty;
        try
        {

            message = new MailMessage();
            message.From = new MailAddress(smtpuser);
        
            message.To.Add(toMailAddress);
           
            message.BodyEncoding = System.Text.Encoding.UTF8;
           
            message.Subject = mailSubject;
            message.Body = mailMessage.ToString();



            message.IsBodyHtml = true;

            client = new SmtpClient();
            client.Host = smtpHost;
            //if (isSSLEnabled)
            //{
            //    client.EnableSsl = true;
            //}

            client.Port =  Convert.ToInt32(smtpport);
            client.EnableSsl = false;
            client.Credentials = new System.Net.NetworkCredential(smtpuser, smtppwd);
          
            client.Send(message);
        }
        catch (Exception ex)
        {
            string x = ex.Message;
        }
        return true;
    }
   

    public bool IsReusable {
        get {
            return false;
        }
    }

}

No comments:

Post a Comment