Wednesday, June 20, 2012

How to send e-mail in ASP.Net?


Asp.Net provides some classes, using these classes we can send e-mail to any destination address.
Basically for sending e-mail there are following classes are required-

1. MailMessage
2. SmtpClient
 

MailMessage Class-
 
This is provides facility to create object for sending e-mail by setting required properties values.
After creating object for MailMessage class we can configure mail object by using following properties-

To    - for setting destination address
From    - for setting source address
Subject - for setting subject of e-mail
Body    - for setting body of e-mail

These all are basic property to send email, based on your  need these properties can be used.
 

SmtpClient Class-
 
This class provides facility to send e-mail by using the Simple Mail Transfer Protocol (SMTP), we can pass MailMessage Class object as a parameter to send a mail.

SmtpClient class has also some properties, which is used for sending e-mail, these properties can be set in web.config file, for this we need to add setting under <system.net> tag.

Like this-
 

<system.net>
    <mailSettings>
    ----------------
    ----------------
    </mailSettings>
</system.net>


Here I am explaining, How to send e-mail in ASP.Net?

In this example, I have used SmtpClient and MailMessage Classes. For this you need to include System.Net.Mail namespace in our code.

Here is code-


Add following code in Web.config file-

   <system.net>
    <mailSettings>
      <smtp deliveryMethod="Network" from="abc@gmail.com">
        <network enableSsl="true"
        host="smtp.gmail.com"
        userName="abc@gmail.com"
        password="12343"
        port="587" />
      </smtp>
    </mailSettings>
  </system.net>

 
This is STMP related setting; these setting will be used while sending e-mail.


Now .cs code-

protected void btnSend_Click(object sender, EventArgs e)
        {
            //Creating MainlMessage object
            MailMessage msgEmail = null;
            try
            {
                msgEmail = new MailMessage();
                msgEmail.IsBodyHtml = true;

                //Adding to address
                if (!string.IsNullOrEmpty(txtEmailTo.Text))
                {
                    msgEmail.To.Add(txtEmailTo.Text);
             
                    //Adding subject
                    msgEmail.Subject = txtSubject.Text;

                    //Adding body
                    msgEmail.Body = txtBody.Text;
          
                   //Creating object of SMTP class
                    SmtpClient smtpEmail = new SmtpClient();

                    //Sending message
                    smtpEmail.Send(msgEmail);

                    Response.Write("<script>alert('Message sent successfully.');</script>");
                }
                else
                {
                    Response.Write("<script>alert('Enter To address.');</script>");
                }
             }
         catch (Exception ex)
            {
                Response.Write("<script>alert('" + ex.Message + "');</script>");
            }
        finally
            {
                msgEmail.Dispose();
            }
        }


In this code, I have created object (msgEmail) of MailMessage class, using properties of this class object I am configuring mail object, after configuration finally I am creating object for SmtpClient class and passing msgEmail object to Send() method for sending  message.

While sending message all the SMTP related settings will be taken from web.config file. We can add SMTP settings in web.config file like I have done, Web.config file contains separate section for this. Under  <mailSettings> section in web.config file all mail setting can be set.


If we are not setting these configurations in web.config file then we have to set this setting by using code. For this we have to use SmtpClient Class of System.Net.Mail Namespace. like this-

SmtpClient objSmtp = new SmtpClient("Host Address");
smtp.Credentials = new NetworkCredential("username", "Password");
smtp.Send(msgEmail);


Here first we need to set Host Address  while creating object of SmtpClient class object, after that we have to set credentials for sending mail, Here  msgEmail is object of MailMessage Class. Finally using Send() method of SmtpClient class object message will be sent.



Thanks

No comments:

Post a Comment