Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

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

Code for Communicating over Sockets?

2433


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

2602


ArrayList declaration in .net

3266


Code for Creating a Form Using PlaceHolder Controls?

2895


Coding for Synchronizing Cache Access in ASP.NET?

3703


Code for Using Keyboard Events?

2669


how to track links visited in google using iframes

2773


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.

2752


Coding for .NET Delegates?

2626


Code for Document Validation in XML.NET?

2426


How to Bind Nested XML to a Repeater Control with Container.DataItem?

3681


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

3040


How to get Dynamically Linked Comboboxes Set?

2503


i have a gird with columns all are coming from database,this will bind in item templete in gridview as textboxex.and i have button below named Update.i want to update all the records in the grid,but if user change the value of one textbox,what is the easy way 2 do this

2621


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

3021