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

Answers were Sorted based on User's Feedback



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

Answer / swapna

MailMessage message = new MailMessage ();
message.From = <email>;
message.To = <email>;
message.Subject = "Scheduled Power Outage";
message.Body = "Our servers will be down tonight.";
SmtpMail.SmtpServer = "localhost";
SmtpMail.Send (message);


MailMessage and SmtpMail are classes defined in the .NET
Framework Class Library's System.Web.Mail namespace.

Due to a security change made to ASP.NET just before it
shipped, you need to set SmtpMail's SmtpServer property
to "localhost" even though "localhost" is the default.

In addition, you must use the IIS configuration applet to
enable localhost (127.0.0.1) to relay messages through the
local SMTP service.

Is This Answer Correct ?    59 Yes 7 No

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

Answer / kranthi.j

protected void btnsubmit_Click(object sender, EventArgs e
{
System.Net.Mail.MailMessage msg = new
System.Net.Mail.MailMessage(txtfrom.Text, txtname.Text,
txtSubject.Text, txtmessage.Text);

System.Net.Mail.SmtpClient mysmtp = new
SmtpClient("192.168.1.80");
try
{
mysmtp.Send(msg);
lblmsg.Text = "mail sent";
}
catch (Exception ex)
{
lblmsg.Text = ex.Message;
}
}
}

Is This Answer Correct ?    16 Yes 4 No

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

Answer / shivani

System.Web.Mail.MailMessage obMessage = new
System.Web.Mail.MailMessage();
obMessage.To = "Receiver@gmail.com";
obMessage.From = "sender@gmail.com";
obMessage.Subject = strSubject;
obMessage.Body = "Hi! this is the way to send mail;
obMessage.BodyFormat = HTMl;
try
{
SmtpMail.SmtpServer = "";
SmtpMail.Send(obMessage);
bRet = true;
}
catch (System.Web.HttpException exhttp)
{
Trace.Write("Error sending mail " + exhttp.Message);
}

Is This Answer Correct ?    12 Yes 2 No

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

Answer / pragyna

protected void btnsubmit_Click(object sender, EventArgs e
{
System.Net.Mail.MailMessage msg = new
System.Net.Mail.MailMessage(txtfrom.Text, txtname.Text,
txtSubject.Text, txtmessage.Text);

System.Net.Mail.SmtpClient mysmtp = new
SmtpClient("192.168.1.80");
try
{
mysmtp.Send(msg);
lblmsg.Text = "mail sent";
}
catch (Exception ex)
{
lblmsg.Text = ex.Message;
}
}
}

OR

MailMessage message = new MailMessage ();
message.From = <email>;
message.To = <email>;
message.Subject = "Scheduled Power Outage";
message.Body = "Our servers will be down tonight.";
SmtpMail.SmtpServer = "localhost";
SmtpMail.Send (message);


MailMessage and SmtpMail are classes defined in the .NET
Framework Class Library's System.Web.Mail namespace.

Is This Answer Correct ?    11 Yes 4 No

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

Answer / shivani

mailmessage obmail = new mailmessage();
obmail.To= "Dummy@gmail.com";
obmail.from= "sender@gmail.com";
obmail.Subject="Mail sending through web";
obmail.body="Deascription";
try
{
smtp.server="";
smtp.send(obmail);
}
catch(System.Web.HttpException exhttp)
{
trace.write("error sending mail " +exhttp.message);
}

Is This Answer Correct ?    11 Yes 5 No

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

Answer / knowledgenet

Import system.net.mail

Protected sub button_click(ByVal sender as object, ByVal e
as system.EventArgs)handles button.click

dim smtp As New SmtpClient()
smtp.send(New
MailMessage(txtfrom.text,txtto.text,txtsubject.text,txtbody.text))
label.text="Message sent"
End sub

Is This Answer Correct ?    9 Yes 4 No

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

Answer / 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

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

Answer / mahesh swami

<appSettings>
<add key="SMTPUserName" value="abcxyzabc4u@gmail.com" />
<add key="SMTPPassword" value="pnagajsuma" />
</appSettings>



using System.Configuration;
using System.Net.Mail;
using System.Text;
using System.Net;



protected void SendAcknowledgementMail()
{
MailMessage oMailMessage = new MailMessage();
NetworkCredential oCredentials = new
NetworkCredential(ConfigurationManager.AppSettings["SMTPUserName"].ToString(),
ConfigurationManager.AppSettings["SMTPPassword"].ToString());

oMailMessage.To.Add(txtEmailAddress.Text);
oMailMessage.Subject = "Testing";
oMailMessage.From = new
MailAddress(ConfigurationManager.AppSettings["SMTPUserName"].ToString());
oMailMessage.Body = "Hi this is a sample testing
mail. Pls ignore this.";
oMailMessage.IsBodyHtml = false;

SmtpClient oSmtpClient = new
SmtpClient("smtp.gmail.com");
oSmtpClient.UseDefaultCredentials = false;
oSmtpClient.EnableSsl = true;
oSmtpClient.Credentials = oCredentials;
oSmtpClient.Port = 587;
if (!string.IsNullOrEmpty(oMailMessage.Subject))
{
oSmtpClient.Send(oMailMessage);
}
}




protected void btnsend_Click(object sender, EventArgs e)
{
SendAcknowledgementMail();
}

Is This Answer Correct ?    2 Yes 0 No

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

Answer / arumugam.m

asp.net is a web based application,so asp.net can send e-
mail from an asp.net application

Is This Answer Correct ?    8 Yes 7 No

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

Answer / anand

protected void btn_submit_Click(object sender, EventArgs e)
{
try
{
string rd = "";
MailMessage mM = new MailMessage();
mM.From = new MailAddress(Email.Value);
mM.To.Add("<email>");
mM.Subject = title.Value;
if (Radio1.Checked)
{
rd = Radio1.Value;
}
else
{
rd = Radio2.Value;
}
mM.Body = rd + "<br>" + name.Value + "<br>" +
title.value + "<br>" + Company.Value + "<br>" + Email.Value
+ "<br>" + url.Value + "<br>" + Comment.Value;
mM.IsBodyHtml = true;
mM.Priority = MailPriority.High;
SmtpClient smtp = new
SmtpClient("smtp.gmail.com", 587);
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new
NetworkCredential("username", "P@ssword"); //From user
credentails
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(mM);
AlertBox("Mail Send successfully");
}
catch
{
AlertBox("Mail Send Failed");
}
}

Is This Answer Correct ?    8 Yes 7 No

Post New Answer

More ASP.NET Code Interview Questions

how the value of label is printed through a button in asp.net web application

3 Answers  


Code for Document Validation in XML.NET?

0 Answers   TCS,


how can we close a web page in asp.net without using jscript?

4 Answers  


hold checkbox values

4 Answers  


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

0 Answers   TCS,






Listview design in .net

1 Answers  


Coding for .NET Delegates?

0 Answers  


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

0 Answers  


Code for Using Keyboard Events?

0 Answers  


how to convert Dataset to Object Array or list in c# .net

4 Answers  


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

16 Answers   DataPoint, Infosys, Persistent, Radar, TCS, Wipro,


How to Create Scrollable Micro Windows?

2 Answers  


Categories
  • ASP.NET Code Interview Questions ASP.NET Code (46)
  • VB.NET Code Interview Questions VB.NET Code (9)
  • C Sharp Code Interview Questions C Sharp Code (51)
  • ADO.NET Code Interview Questions ADO.NET Code (8)