In this article I will explain how to send email using asp.net.
Description:
In realtime we will use gmail,yahoo,hotmail etc to send mails to particular user here I will show to how to implement mail sending concept in asp.net.
To implement mail concept in asp.net first we need to add following reference to our application System.Web.Mail namespace
What is System.Web.Mail
System.Web.Mail is a namespace which contains classes that enable us to construct and send messages using the CDOSYS (Collaboration Data Objects for Windows 2000) message component. The mail message is delivered either through the SMTP mail service built into Microsoft Windows 2000 or through an arbitrary SMTP server.
How we can get this reference (System.Web.Mail)
We need to add System.web.dll reference to our application for that follow below steps
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Send Mail using asp.net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table style=" border:1px solid" align="center">
<tr>
<td colspan="2" align="center">
<b>Send Mail using asp.net</b>
</td>
</tr>
<tr>
<td>
From:
</td>
<td>
<asp:TextBox ID="txtFrom" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Subject:
</td>
<td>
<asp:TextBox ID="txtSubject" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
To:
</td>
<td>
<asp:TextBox ID="txtTo" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td valign="top">
Body:
</td>
<td>
<asp:TextBox ID="txtBody" runat="server" TextMode="MultiLine" Columns="30" Rows="10" ></asp:TextBox>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="btnSubmit" Text="Send" runat="server" onclick="btnSubmit_Click" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
|
using System.Web.Mail;
|
protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
MailMessage Msg = new MailMessage();
// Sender e-mail address.
Msg.From = txtFrom.Text;
// Recipient e-mail address.
Msg.To = txtTo.Text;
Msg.Subject = txtSubject.Text;
Msg.Body = txtBody.Text;
// your remote SMTP server IP.
SmtpMail.SmtpServer = "10.20.72.1";
SmtpMail.Send(Msg);
Msg = null;
Page.RegisterStartupScript("UserMsg", "<script>alert('Mail sent thank you...');if(alert){ window.location='SendMail.aspx';}</script>");
}
catch (Exception ex)
{
Console.WriteLine("{0} Exception caught.", ex);
}
}
|
Other related posts are
|
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
Subscribe by RSS
Subscribe by Email
|
|||
|
|

Subscribe by RSS
Subscribe by Email
43 comments :
hi i used dis code....but its gvng error as "The transport failed to connect to the server."..
wat mistake i have done...
i wrote as it as code...
even im getting error...
hi,
that would be problem with smtp server please give your smtp server address don't use the ip address mentioned in code.
wat do u mean by smtp server..???
can u help me in sending an mail for forgot password
@poonam
smtp(simple mail transfer protocol) server are the Email servers to exchange the mails.
@poonam
To send mail for forgot password get details of user based on UserName or Email then bind Userdetails to body of email and then send mail like as i explained above
can u send me d coding plzzzz......
whch smtp code should v use...???
SmtpMail.SmtpServer = "assign the smtpserver address here";
Ex :
It would be something like this '50.22.12.227'
For gmail SmtpServer address as below:
SmtpMail.SmtpServer ="smtp.gmail.com"
hi i am getting this error
{System.Web.HttpException: Invalid mail attachment 'file123.pdf'.
at System.Web.Mail.MailAttachment.VerifyFile()
at System.Web.Mail.MailAttachment..ctor(String filename, MailEncoding encoding
Hai sir,
Pls send the details about smtp server and how we can get the server address from the system
The server rejected one or more recipient addresses.
Hi. how to get my smpt server? idk what should I replace in the IP. Thanks :)
Dear Sir
I am having different issue,I have to send a web page like "contact us Form" in email body. If the recipient fill that form when he open the mail,then after submitting the detail all the information should go to admin/the mail sender.I am using C# and sql server 2005.
I am looking forward to hearing from you.
Thanks in advance
Hi first of all thanks for ur incredible works! i got my smtp ip address from cmd prmpt window. i used that address. but still i am not receiving the mail. but wen i click the button the page just reloads;.
@Rathinamoorthy...
for smtp server your network people will define some address you need to get that address not ip address of server.
hey sir,
from where should i get the address of smtp server
@rajan kumar...
Please check above comments....
Hii suresh..In.Net tab system.web.dll is not showing so how can i solve it..
when i use this codeing the error comes this... plz tell me the solutions sir....
System.Web.HttpException (0x80004005): The transport failed to connect to the server. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Runtime.InteropServices.COMException: The transport failed to connect to the server. --- End of inner exception stack trace --- at System.RuntimeType.InvokeDispMethod(String name, BindingFlags invokeAttr, Object target, Object[] args, Boolean[] byrefModifiers, Int32 culture, String[] namedParameters) at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams) at System.Web.Mail.SmtpMail.LateBoundAccessHelper.CallMethod(Object obj, String methodName, Object[] args) at System.Web.Mail.SmtpMail.LateBoundAccessHelper.CallMethod(Object obj, String methodName, Object[] args) at System.Web.Mail.SmtpMail.CdoSysHelper.Send(MailMessage message) at System.Web.Mail.SmtpMail.Send(MailMessage message) at withoutattachment.btnSubmit_Click(Object sender, EventArgs e)
That is the problem with your mail server. Please check whether you given correct smtp server address or not...
hi i like this but i didn't get system.web.dll in my add reference window y this happend
Hello Friends, i have replaced SmtpMail.SmtpServer = "155.152.1.20" with my IP Address but still i am not getting mail
sir your code is work but not sending a mail to proper addresss can u help???????
can u send me d coding plzzzz...
pramod2011eng@gmail.com
hai, me using .net 2008 version, i didnt saw the system.web.dll on .net tab, plz help me
some other options are system.data, system.web service etc
can u help me in sending an mail for forgot password
very usefull....
can you please upload application for sending SMS in c#?
i developed application with system.Net.Mail namespace . I heard system.web.Mail is obsolete from .net 3.5. I used code as below .
" Dim smtpclient As New SmtpClient
Dim mailmsg As New MailMessage
Try
Dim frmAddress As New MailAddress(txtFrm.Text, "test")
mailmsg.From = frmAddress
mailmsg.To.Add(txtTo.Text)
mailmsg.Subject = txtSub.Text
mailmsg.Body = txtBody.Text
mailmsg.IsBodyHtml = False
smtpclient.Send(mailmsg)
Response.Write("Sent Mail successfully")
Catch ex As Exception
Response.Write(ex.Message)
End Try" .
In web.config "
" .
I'm able send mail with in my domail and not able to send google, yahoo . What should i do send emails to google,yahoo etc through my web application.
where is database of this example sir ??
will you please let me knw about this
If the to mail id its giving wrong
Hi sir,
If we gave wrong To mail id then its giving error.
when mail id is wrong it should to from mail id how to do it
For all whose having doubt for mail not sent
If we run the programme on local server it will not work(mail will not sent),even if we have internet facility so.
it needs an smtp server to work this code.
thank you..
Hi,
i am using framework 4.0
i'm not getting system.web.dll to add reference in my project.
then what i do.plz help me
System.Runtime.InteropServices.COMException: At least one of the From or Sender fields is required, and neither was found.
this error is occured .....what should i do...its urgent please help me..
it doesnt send any mail..pls help me...
Respected Sir,
thank you,but can i sent mail from diffrent mail server for example i host my web on xyz.com and can
i use mail.abc.com as smtp server or not?
there is any requirements to modify web.config file in above code?
i got no errors..but it does send any mail..
give me ur email...i send to u
h
am getting this error by using smtp.gmail.com
//error
The server rejected the sender address. The server response was: 530 5.7.0 Must issue a STARTTLS command first. zy5sm32051754pbb.43 - gsmtp
can u send me the mail inbox code plzzz...
hi
system.mail.dll does not exists system.mail exists so will it work