Introduction: Sometimes
it is required to send email from our website to
other person. It’s very easy.In previous article i explained How to send emails in asp.net using Gmailin asp.net and Send emails in asp.net using Gmail | Howto set Smtp settingin web.config file to send emails in asp.net using Gmail inasp.net Now in this article i will explain how you can send mail
by setting the SMTP settting in web.config file.
Benefit: If you want to change the credentials then the real benefit of setting it in web.config file is that you need to change the credentials from one place even if you have used the email code in multiple pages.
Implementation: Let's create an asp.net demo web page to test this.
C#.NET Code to send emails in asp.net
Benefit: If you want to change the credentials then the real benefit of setting it in web.config file is that you need to change the credentials from one place even if you have used the email code in multiple pages.
Implementation: Let's create an asp.net demo web page to test this.
<table style="width:100%;">
<tr>
<td>
Send To(Email Address)</td>
<td>
<asp:TextBox ID="txtEmailId"
runat="server"
Columns="60"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Subject</td>
<td>
<asp:TextBox ID="txtSubject"
runat="server"
Columns="60"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Body</td>
<td>
<asp:TextBox ID="txtBody"
runat="server"
Columns="60"
Rows="5"
TextMode="MultiLine"></asp:TextBox>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="btnSendEmail"
runat="server"
onclick="btnSendEmail_Click"
Text="Send
Email" />
</td>
</tr>
</table>
- In the web.config
file write as:
<system.net>
<mailSettings>
<smtp>
<network host="smtp.yourdomain.com"
port="25"
userName="you@yourdomain.com"
password="YourPassword"/>
</smtp>
</mailSettings>
</system.net>
<appSettings>
<add key="EmailID" value=" you@yourdomain.com"/>
</appSettings>
C#.NET Code to send emails in asp.net
- Include following namespaces:
using System.Configuration;
using System.Net.Mail;
- Now in the code behind file(.aspx.cs) write the code to send Email on send mail button as:
protected void
btnSendEmail_Click(object sender, EventArgs e)
{
string fromEmail = ConfigurationManager.AppSettings["EmailID"].ToString();
string toEmail = txtEmailId.Text.Trim();
MailMessage message = new
MailMessage(fromEmail, toEmail);
message.Subject = txtSubject.Text.Trim();
message.Body = txtBody.Text.Trim();
SmtpClient smtp = new
SmtpClient();
smtp.Send(message);
}
VB.NET Code to send emails in asp.net
- Include following namespaces:
imports System.Configuration
imports System.Net.Mail
Protected Sub btnSendEmail_Click(sender As Object,
e As EventArgs)
Dim
fromEmail As String =
ConfigurationManager.AppSettings("EmailID").ToString()
Dim
toEmail As String = txtEmailId.Text.Trim()
Dim
message As New MailMessage(fromEmail, toEmail)
message.Subject
= txtSubject.Text.Trim()
message.Body
= txtBody.Text.Trim()
Dim
smtp As New SmtpClient()
smtp.Send(message)
End Sub
Now over to you:
" I hope you have got the real benefit of setting the smtp settings to send mail in web.config file and
If you like my work; you can appreciate by leaving your comments, hitting
Facebook like button, following on Google+, Twitter, Linked in and Pinterest,
stumbling my posts on stumble upon and subscribing for receiving free updates
directly to your inbox . Stay tuned and stay connected for more technical
updates."
4 comments
Click here for commentsthis works just great!
ReplyThanks..i am glad my article helped you..stay tuned and keep reading..:)
ReplyThanks for this, is there any other way to automatically send the email everyday at specific time and how to add attachment to the email.
ReplyHi, to send attachment with email read the following article..
Replyhttp://www.webcodeexpert.com/2013/06/how-to-send-mail-with-multiple.html
If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..