Here I will explain how to send email using gmail in asp.net or send email using gmail smtp server in asp.net using c#.
Description:
To implement this mail concept in your asp.net application first we need to add this reference to our application System.Web.Mail namespace
The System.Net.Mail namespace contains classes used to send electronic mail to a Simple Mail Transfer Protocol (SMTP) server for delivery.
for that we need to add System.web.dll reference to our application.
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Send Mail using with gmail crendentials in 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 gmail credentials in asp.net</b>
</td>
</tr>
<tr>
<td>
Gmail Username:
</td>
<td>
<asp:TextBox ID="txtUsername" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Gmail Password:
</td>
<td>
<asp:TextBox ID="txtpwd" runat="server" TextMode="Password"></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.Net.Mail;
|
protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
MailMessage Msg = new MailMessage();
// Sender e-mail address.
Msg.From = new MailAddress(txtUsername.Text);
// Recipient e-mail address.
Msg.To.Add(txtTo.Text);
Msg.Subject = txtSubject.Text;
Msg.Body = txtBody.Text;
// your remote SMTP server IP.
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.Credentials=new System.Net.NetworkCredential(txtUsername.Text,txtpwd.Text);
smtp.EnableSsl = true;
smtp.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);
}
}
|
|
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
24 comments :
Hi Suresh Garu,
Nice post regarding Gmail Credentials.I had seen so many sites for this but i didn't find clear explanation like this.It helped me.Thank You
thanks prasanthi
keep visiting......
Thank u sooooooooooooooooo much........ very useful.......
i send my first mail through asp.net........
Thank you
can u send hoe to send multiple reciepients
Simply Great ! answer to send mail
Hi Suresh
I m getting Error like
"Failure sending mail." in catch.
check whether you enable your mail settings Forwarding and POP/IMAP
in your gmail based on mentioned details in article
how to send email to multiple recipients, like we have list of email ids in gridview and program send email to all the user who are selected by user through checkbox control
@Imtiaz..
If you want to send multiple recipients you need to append all the emails with semicolon like test@gmail.com;test@gmail.com and send mail as usual it will send mail to all the recipients...
hai suresh garu if i check mail in inbox to delte i must get delete button above so can u give suggestion how to write for it my mail id is togaru.kranthi@gmail.com
sir how can we track delivery report of these email ??
thanks
hai Suresh Garu ,
I want some help from you , for example
In Gmail we sending the mail if mail id wrong wet get one mail that is Delivery sending failed mail..the same functionality how we implement in Asp.net website.
hi suresh...i want to send a verfication mail to his email Id when user fills signup form in a website and when he clicks on that link then his account will activate and he will able to navigate in that website.plz help me as soon as possible.
hi..
thks a lot man..
how can i attach a file with mail????
i've used your code bt niether m getting any error nor its sending any mail
Arpeeta ::kindly Set the break POint. .
Thank you very much..........got mail without any error.
visited lot of sites but cant got any solution.But this code working fine...
Sir,,,Also can you suggest any code for sending SMS...???
any help will be most useful for me.......!!!
thanks in advance......
Thanks Man. Your helped me lot. in past i had try various example but had not succeed in sending mail. but your code is working perfectly
Thanking your many times.
Ankit Patel
sir when we send email to wrong email address
Then it does not show any message.
Page.RegisterStartupScript("UserMsg", "alert('Mail sent thank you...');if(alert){ window.location='SendMail.aspx';};
Above line show error in my page
Dear Suresh,
I need your help to sending mail. I have written same code. But i am unable to send email using this code. Please let me know. Where i am wrong.
Nice Post sir,
I have a problem, i need to send email to multiple email id. I cannot use semi colon to provide email id because i have a bulk data of email id. How can i do this. please help me.