Click here to Skip to main content
15,880,469 members
Articles / Web Development / ASP.NET
Tip/Trick

How to send an email with app setting in web Config file

Rate me:
Please Sign up or sign in to vote.
5.00/5 (5 votes)
2 Jul 2013CPOL1 min read 70.6K   11   8
How to send an email with app setting in the web config file.

Here I will Show you How to send an email with app setting in web Config file ,Later on I will explain the advantages why I have setting the main setting in the web configuration file. So let’s starts with email sending code in Asp.net. This is the code of email send with mail setting in web configuration file

On the aspx.cs page

Mail Message message = new MailMessage ();
          MailAddress Sender = new MailAddress (ConfigurationManager.AppSettings ["smtpUser"]);
          MailAddress receiver = new MailAddress (txtemail.Text);
          SmtpClient smtp = new SmtpClient()
          {
              Host = ConfigurationManager.AppSettings ["smtpServer"],
              Port = Convert.ToInt32 (ConfigurationManager.AppSettings ["smtpPort"]),
              EnableSsl = true,
              Credentials = new System.Net.NetworkCredential (ConfigurationManager.AppSettings ["smtpUser"], ConfigurationManager.AppSettings ["smtpPass"])

          };
          message.From = Sender;
          message.To.Add(receiver);
          message.Body = txtbody.Text;
          message.IsBodyHtml = true;
          smtp.Send(message);

Here is the design for this we have to write it on on .aspx page

 Email ID <asp:TextBox ID="txtemail" runat="server"></asp:TextBox><br />
Subject  <asp:TextBox ID="txtsubject" runat="server"></asp:TextBox><br />
Body <asp:TextBox ID="txtbody" runat="server" TextMode="MultiLine"></asp:TextBox><br />
<asp:Button ID="btnSendmail" runat="server"  Text="Send"
   onclick="btnSendmail_Click"/>

2nd Now I will show you the simple email sending code with no code setting in web Config File

Mail Message message = new MailMessage ();
          MailAddress Sender = new MailAddress ( sender@mail.com);
          MailAddress receiver = new MailAddress (reciver@mail.com@mail.com);
          SmtpClient smtp = new SmtpClient()
          {
              Host = "smtp.gmail.com", //for gemail
              Port = 587 //for gmail,                EnableSsl = true,
              Credentials = new System.Net.NetworkCredential (youremailaddress,Your Password)

          };
          message.From = Sender;
          message.To.Add(receiver);
          message.Body = txtbody.Text;
          message.IsBodyHtml = true;
          smtp.Send(message);

Email setting in web Configuration file

<appSettings>
        <add key="webpages:Version" value="1.0.0.0" />
        <add key="ClientValidationEnabled" value="true" />
        <add key="UnobtrusiveJavaScriptEnabled" value="true" />
        <add key="smtpServer" value="smtp.gmail.com" />
        <add key="EnableSsl" value = "true"/>
        <add key="smtpPort" value="587" />
        <add key="smtpUser" value="abhimanyu.vij@gmail.com" />
        <add key="smtpPass" value="*****" />
        
    </appSettings>

Advantages for setting the email setting in the web configuration file

The main advantages of set the email setting in web configuration file is code reusability because once we write the code on aspx.cs page we no need to change the setting on the page we simply change the setting in the web config file ,suppose if we write the code on aspx.cs page like I did in the 2nd case and if if want to use the code for any other project then we need to change the setting on the aspx.cs page which is very cumbersome so always set the email setting on the web configuration page that’s all

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
PraiseReally Helpfull article Pin
Member 125606051-Jun-16 19:06
Member 125606051-Jun-16 19:06 
GeneralMy vote of 5 Pin
Abhimanyu vij15-Jul-13 18:43
Abhimanyu vij15-Jul-13 18:43 
GeneralMy vote of 5 Pin
Abhimanyu vij15-Jul-13 18:42
Abhimanyu vij15-Jul-13 18:42 
QuestionThis article is a bit misleading, as your example is exactly what the system.net section is for Pin
Phil Lee NZ3-Jul-13 14:51
Phil Lee NZ3-Jul-13 14:51 
AnswerRe: This article is a bit misleading, as your example is exactly what the system.net section is for Pin
Abhimanyu vij15-Jul-13 18:50
Abhimanyu vij15-Jul-13 18:50 
GeneralRe: This article is a bit misleading, as your example is exactly what the system.net section is for Pin
Phil Lee NZ15-Jul-13 20:59
Phil Lee NZ15-Jul-13 20:59 
GeneralRe: This article is a bit misleading, as your example is exactly what the system.net section is for Pin
Abhimanyu vij15-Jul-13 21:02
Abhimanyu vij15-Jul-13 21:02 
GeneralRe: This article is a bit misleading, as your example is exactly what the system.net section is for Pin
Abhimanyu vij15-Jul-13 21:07
Abhimanyu vij15-Jul-13 21:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.