.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Sending Email in asp.net maintaining security by web.config

Posted By:Nikhil Kumar       Posted Date: April 01, 2010    Points: 25    Category: ASP.NET    URL: http://www.dotnetspark.com  

Sending Email in asp.net maintaining security by web.config. As you learned before how to send email in asp.net by simply giving the information in .aspx.cs page but this is not secure because we gives the secure information of email id in hard code like its passwords.
 

Sending Email in asp.net maintaining security by web.config


Hello Friends.

As you learned before how to send email in asp.net by simply giving the information in .aspx.cs page but this is not secure because we gives the secure information of email id in hard code like its passwords.

when we publish our site then all the .cs files converts into .dlls files because of security but we can give the email information in our web.config instead of .cs file this comes under more security.

One profit of using this information in web.config is : we do not have to write all the line again and again in .cs files means saving of time and improving of security.

So lets start doing this .

Normally our code for sending email in asp.net is.

private void _fn()
{
SmtpClient client = new SmtpClient();
MailMessage message = new MailMessage();
client.Host = "smtp.gmail.com";
client.Port = 587;
client.UseDefaultCredentials = true;
client.Credentials = new System.Net.NetworkCredential("youremailid@gmail.com", "yourpassword");
client.EnableSsl = true;

try
{

MailAddress SendFrom = new MailAddress("youremailid@gmail.com", "Mysite..");
MailAddress SendTo = new MailAddress(EmailTo.Text);
message = new MailMessage(SendFrom, SendTo);
message.Subject = "My Website";
message.Body = "This is a test email";
message.IsBodyHtml = true;
// set the mail's priority to high
message.Priority = MailPriority.High;
client.Send(message);
}
catch (Exception ex)
{
Label1.Text = ex.Message;
}
}
}


But notice here we are giving our email id password which dosen't come under security.

So now our code should be.

.cs file

 

private void _fn()
{
try
{
SmtpClient smtp = new SmtpClient();
MailMessage SendFrom = new MailMessage("youremailid@gmail.com", TextBox6.Text);
SendFrom.Subject = "My Site";
SendFrom.Body = "This is a test email";
SendFrom.IsBodyHtml = true;
SendFrom.Priority = MailPriority.High;
smtp.Send(SendFrom);
}
catch (Exception ex)
{
Label1.Text = ex.Message;
}
}
}


Now we will will the rest information in our project's web.config file . How.

web.Config.

add these line after

tag

 







So here we learned how to send email by using web.config file. When we host our site then we give the connection string in web.config .Do you know why ... because of security so now we should also have to give the email information in web.config .

How much you liked this article, Please tell me as leaving your precious comments .

If you feel any problem then just drop me a line i will try to solve it out..

Thank you !!!

Nikhil Kumar

www.fast-get.com

 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend