How to send e-mail from an ASP.NET application?

Answer Posted / jitendra kumar

For sending a mail in Asp.net,import System.Net.Mail
Namespace.
This is a simple application which u can use as a feedback
form or contact us page.
Create a simple default.aspx page with following code.
<form id="form1" runat="server">

<div style="text-align: center">
<div>
<asp:Label ID="lblErrorMsg" runat="server" Text="Label"></
asp:Label>
</div>
<table border="1">
<tr>
<td colspan="2" style="font-weight: 700; text-align:
center; background-color: #F7C331;">
Send Mail in ASP.net through SMTP
</td>
</tr>
<tr>
<td style="font-weight: 700; text-align: right;">
TO
</td>
<td>
<asp:TextBox ID="txtTo" runat="server" Width="242px"></
asp:TextBox>
</td>
</tr>
<tr>
<td style="font-weight: 700; text-align: right;">
From
</td>
<td>
<asp:TextBox ID="txtFrom" runat="server" Width="242px"></
asp:TextBox>
</td>
</tr>
<tr>
<td style="font-weight: 700; text-align: right;">
CC
</td>
<td>
<asp:TextBox ID="txtcc" runat="server" Width="242px"></
asp:TextBox>
</td>
</tr>
<tr>
<td style="font-weight: 700; text-align: right;">
BCC
</td>
<td>
<asp:TextBox ID="txtbcc" runat="server" Width="242px"></
asp:TextBox>
</td>
</tr>
<tr>
<td style="font-weight: 700; text-align: right;">
Subject
</td>
<td>
<asp:TextBox ID="txtSub" runat="server" Width="242px"></
asp:TextBox>
</td>
</tr>
<tr>
<td style="font-weight: 700; text-align: right;">
Message
</td>
<td>
<asp:TextBox ID="txtMsg" runat="server"
TextMode="MultiLine" Height="75px" Width="246px"></
asp:TextBox>
</td>
</tr>
<tr>
<td style="font-weight: 700">
Attachment
</td>
<td>
<asp:FileUpload ID="FileUpload1" runat="server"
Width="244px" />
</td>
</tr>
<tr>
<td>

</td>
<td>
<asp:Button ID="Button1" runat="server"
OnClick="Button1_Click" Text="SendMail" />
</td>
</tr>
</table>
</div>
</form>
And paste the code in Default.aspx.cs page(i,e codebehind )
try
{
SmtpClient stp = new SmtpClient();
//Create a smtpclient object for sending mail.
MailMessage mail = new MailMessage();
//Create a MailMessage object for drafting mail.
MailAddress madd = new MailAddress(txtFrom.Text);
mail.From = madd;
mail.To.Add(txtTo.Text);

if (!string.IsNullOrEmpty(txtcc.Text.Trim()))
{
mail.CC.Add(txtcc.Text.Trim());
}
if (!string.IsNullOrEmpty(txtbcc.Text.Trim()))
{
mail.CC.Add(txtbcc.Text.Trim());
}

if (FileUpload1.HasFile == true)
{
string attachFile =
FileUpload1.PostedFile.FileName.ToString();
mail.Attachments.Add(new Attachment(attachFile));
}

mail.Subject = txtSub.Text.Trim();
mail.Body = txtMsg.Text.Trim();


stp.Send(mail);//sending mail using Send method of
SmtpClient class.
lblErrorMsg.Text = "Mail successfully sent.";
}
catch (Exception ex)
{
lblErrorMsg.Text = ex.Message;
}

Mail Setting in Web.Config file.
Using this code under the configuration tag
<system.net>
<mailSettings>
<smtp deliveryMethod="Network"
from="jitendra@infotechsolution.com" >
<network defaultCredentials="true" host="192.168.0.1"
port="25" userName=" jitendra@infotechsolution.com "
password="test"/>

</smtp>
</mailSettings>
</system.net>

Is This Answer Correct ?    4 Yes 2 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Coding for Synchronizing Cache Access in ASP.NET?

3123


What is the code of Password Recovery or Forget your password? Plz tell in c # language.

3573


Code for Using Keyboard Events?

2135


how to create a search bar which access data from various websites and retrieves the data

2556


How to use Client-side Script to Focus Controls in ASP.NET?

2366






Code for Communicating over Sockets?

1952


ArrayList declaration in .net

2751


Code for Presenting Parent/Child Data in a Data Grid Row?

2114


How we use ajax in asp.net through javaScript. Please givee me an example.

2508


Coding for .NET Delegates?

2100


Common UI for Multiple web applications. Suppose there are 35 websites using same third party controls.These 3rd party controls are made together that all 35 websites can use these controls.If we put all 3rd party controls and use its dll in 35 websites,only class files will be accessable. But I want to use CSS,images also in all 35 websites. how I can design the N-tier solution for this project.

2252


Code for Creating a Form Using PlaceHolder Controls?

2352


Code for Document Validation in XML.NET?

1989


how to track links visited in google using iframes

2285


How to get Dynamically Linked Comboboxes Set?

2032