blogspot visit counter

Tuesday, 19 March 2013

how to Sending email with GMail's SMTP server in ASP.NET

how to Sending email with GMail's SMTP server in ASP.NET
Small Introduction:
Sending email is a very common task in any web application. In almost every web application (web site), their will atleast be an occassion to send email in any fashion.
sending email on page load passing values in query string
The SmtpMail class in ASP .NET provides properties and methods for sending messages using the Collaboration Data Objects for Windows we will see, how can we send email from an ASP .NET page.
Description:
now I will explain how to implement mail sending concept with attachment in asp.net. To implement this concept first we need to following reference to our application System.Web.Mail namespace What is System.Web.Mail
The SmtpMail class in ASP .NET provides properties and methods for sending messages using the Collaboration Data Objects for Windows 2000 (CDOSYS) message component In this article, we will see, how can we send email from an ASP .NET page. In a nut shell, today, we will be looking into the following:

. What we need to send Email from an ASP .NET?
. How to send an email from an ASP .NET page?
. What is new in sending email? (SmtpMail.SmtpServer)
. How can we send attachments in an email?

After that design your aspx page like this
<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>
After that add this namcespace in your codebehind
using System.Net.Mail;
After that write the following code in button click
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)
{
Response.Write(ex.Message);
}
}
                   
Here we used smtp.Port=587 this is the port number which is used by gmail that’s why we used this port number. In some situations if you get any error regarding security remove smtp.EnableSsl = true;
Example


Download sample code attached




No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...