Tuesday, April 30, 2013

How to send E-mail in Asp.net using web.config?


Introduction:In this post I go to tell you to send E-mail using web.config. Sometime we create a web application and there is lot of form to send E-email to users. If in future E-mail address of Administration will change then we have to change the E-mail address of Administration in every web form.  If we want it would not happen in future so we mention the E-mail address detail in web.config file of application.
Description:
Now see how we can do it simply. Add the below mention information in web.config file.
<appSettings>
    <add key="Email" value="your E-mail address"/>
  </appSettings>
  <system.net>
    <mailSettings>
      <smtp deliveryMethod="Network">
        <network enableSsl="true" port="587" host="smtp.gmail.com" userName="E-mail addrerss" password="Password"/>
      </smtp>
    </mailSettings>
</system.net>
Now add a web form to application.
  <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
    <table>
    <tr><td>Email Address</td><td>
        <asp:TextBox ID="txtemail" runat="server"></asp:TextBox></td></tr>
        <tr><td>Subject</td><td>
            <asp:TextBox ID="txtsubject" runat="server"></asp:TextBox></td></tr>
            <tr><td>Comment</td><td>
                <cc1:Editor ID="Editor1" runat="server" />
                &nbsp;</td></tr>
                <tr><td>&nbsp;</td><td>&nbsp;</td></tr>
                <tr><td></td><td><asp:Button ID="dtnsend" runat="server" Text="Send" onclick="dtnsend_Click" />
                    </td></tr>
    </table>
   
Double click on Send button that redirect you to .aspx.cs page. Add the namespace to page.
using System.Net.Mail;
using System.Configuration;

On button click add below mention code:
protected void dtnsend_Click(object sender, EventArgs e)
    {
        try
        {
            string Email = ConfigurationManager.AppSettings["Email"].ToString();
            string Emailto = txtemail.Text;
            MailMessage mail = new MailMessage(Email, Emailto);
            mail.Subject = txtsubject.Text;
            mail.Body = Editor1.Content ;
            mail.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient();
            smtp.Send(mail);
            Response.Write("Email send successfully");
            Clear();
        }
        catch (Exception ex)
        {

        }
    }
    public void Clear()
    {
        txtemail.Text = "";
        Editor1.Content = "";
        txtsubject.Text = "";
    }

Now debug the application and check the result.

Related Articles on Send Email:
How to Send Email with Attachment in Asp.net


Is it helpful?

If yes post your comment to admire my work. You can like me on Facebook, Google+, Linkedin and Twitter via hit on Follow us Button and also can get update follow by Email.

2 comments:

  1. You can examine the services of all the companies and select the best one. Nevertheless, there are particular aspects that you need to think about while examining a cloud hosting company.

    ReplyDelete