Search This Blog

Monday, 29 August 2011

SMTP connection

Here is a code to create SMTP connection using class file and web config

First Let us create app setting in web.config, to do this go to web.configuration and in that follow code as below


Let us consider gmail as our host. Under <configuration> write the code as below

web.config code:

  <appSettings>
    <add key="SendMail_Host" value="smtp.gmail.com"/>
    <add key="SendMail_FromUId" value="xyz@gmail.com"/><!--write your user id here, this address will be the mail sending address-->
    <add key="SendMail_FromPwd" value="password"/><!--write your password here-->
 </appSettings>
   

This is where we will creating the sending mail address.

Now let us create a common class file, I have named it CommonCls. But we have to add two name space as shown in the code below

Class Code:

using System.Net.Mail;
using System.Net;

/// <summary>
/// Summary description for clsCommon
/// </summary>
public class clsCommon
{
   

    public clsCommon()
    {
        //
        // TODO: Add constructor logic here
        //
    }


    public void SendMailMessage(string from, string to, string cc, string bcc, string subject, string body)
    {

        MailMessage mMailMessage = new MailMessage();

        mMailMessage.From = new MailAddress(from);
        mMailMessage.To.Add(new MailAddress("abc@gmail.com"));//This is the receiver's address
      

        if ((cc != null) && (cc != string.Empty))
       {
           mMailMessage.CC.Add(new MailAddress(cc));
       }

        if ((bcc != null) && (bcc != string.Empty))
        {
            mMailMessage.Bcc.Add(new MailAddress(bcc));
        }

        mMailMessage.Subject = subject;


        mMailMessage.IsBodyHtml = true;
        mMailMessage.Body = body;

        mMailMessage.Priority = MailPriority.Normal;
        SmtpClient mSmtpClient = new SmtpClient();
        mSmtpClient.EnableSsl = true;
       
        mSmtpClient.Host = ConfigurationManager.AppSettings["SendMail_Host"];
        mSmtpClient.Port = 587;
        mSmtpClient.UseDefaultCredentials = true;
        mSmtpClient.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["SendMail_FromUId"], ConfigurationManager.AppSettings["SendMail_FromPwd"]);
          mSmtpClient.Send(mMailMessage);
    }


 }




Now for the final aspx.cs code:


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Class1: System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            txtName.Focus();
        }
    }
  
    protected void imgSubmit_Click(object sender, ImageClickEventArgs e)
    {
                string body, from, to, cc, bcc, subject;
                clsCommon objClscm = new clsCommon();
                from = ConfigurationManager.AppSettings["SendMail_FromUId"];
                to = ConfigurationManager.AppSettings["SendMail_To"];
                subject = "Hello!";
                body = "Hi this is a test message";
                cc = "";
                 bcc = "";

                objClscm.SendMailMessage(from, to, cc, bcc, subject, body);
                              
         }
}
 

No comments:

Post a Comment