Mail sending concept in ASP.NET:
It’s very easy one to send mail to other mail id using Gmail account. Do to this follow the steps below.
In visual studio 2008,
Step1: File-> New Website -> give name as Default.aspx-> give ok.
Default.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Send Email using ASP.Net and C# with GMAIL</title>
</head>
<body>
<form id="form1" runat="server">
<div align="center">
<h2> Send email from your GMAIL Account</h2>
<table>
<tr>
<td style="text-align: right">Your Gmail Id : </td><td style="text-align: left"><asp:TextBox ID="txtGmailId" runat="server" Width="200px"></asp:TextBox></td>
</tr>
<tr>
<td style="text-align: right">Your Gmail Password : </td><td style="text-align: left"><asp:TextBox ID="txtPassword" TextMode="Password" runat="server" Width="200px"></asp:TextBox></td>
</tr>
<tr>
<td style="text-align: right">To : </td><td style="text-align: left"><asp:TextBox ID="txtTo" runat="server" Width="200px"></asp:TextBox></td>
</tr>
<tr>
<td style="text-align: right">Subject : </td><td style="text-align: left"><asp:TextBox ID="txtSubject" runat="server" Width="200px"></asp:TextBox></td>
</tr>
<tr>
<td style="text-align: right;vertical-align:top;">Message : </td><td><asp:TextBox ID="txtMessage" runat="server" TextMode="MultiLine" Height="100px" Width="300px"></asp:TextBox></td>
</tr>
<tr>
<td></td>
<td style="text-align: left">
<asp:Button ID="btnSend" runat="server" Text="Send" OnClick="btnSend_Click" Width="100px" /></td>
</tr>
</table>
<asp:Label ID="lblError" ForeColor="red" runat="server" Text=""></asp:Label>
</div>
</form>
</body>
</html>
Step2:
In coding page type the following coding.
Default.aspx.cs:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSend_Click(object sender, EventArgs e)
{
try
{
MailSender.SendEmail(txtGmailId.Text + "@gmail.com", txtPassword.Text, txtTo.Text, txtSubject.Text, txtMessage.Text, System.Web.Mail.MailFormat.Text, "");
lblError.Text = "Mail sent successfully.";
}
catch(Exception ex)
{
lblError.Text = ex.Message;
}
}
}
Step3:
Create a new class file by way of selecting,
Website -> Add New Item-> Choose class file named as MailSender.cs
MailSender.cs:
using System.Web.Mail;
using System;
public class MailSender
{
public static bool SendEmail(
string pGmailEmail,
string pGmailPassword,
string pTo,
string pSubject,
string pBody,
System.Web.Mail.MailFormat pFormat,
string pAttachmentPath)
{
try
{
System.Web.Mail.MailMessage myMail = new System.Web.Mail.MailMessage();
myMail.Fields.Add
("http://schemas.microsoft.com/cdo/configuration/smtpserver",
"smtp.gmail.com");
myMail.Fields.Add
("http://schemas.microsoft.com/cdo/configuration/smtpserverport",
"465");
myMail.Fields.Add
("http://schemas.microsoft.com/cdo/configuration/sendusing",
"2");
//sendusing: cdoSendUsingPort, value 2, for sending the message using
//the network.
//smtpauthenticate: Specifies the mechanism used when authenticating
//to an SMTP
//service over the network. Possible values are:
//- cdoAnonymous, value 0. Do not authenticate.
//- cdoBasic, value 1. Use basic clear-text authentication.
//When using this option you have to provide the user name and password
//through the sendusername and sendpassword fields.
//- cdoNTLM, value 2. The current process security context is used to
// authenticate with the service.
myMail.Fields.Add
("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate","1");
//Use 0 for anonymous
myMail.Fields.Add
("http://schemas.microsoft.com/cdo/configuration/sendusername",
pGmailEmail);
myMail.Fields.Add
("http://schemas.microsoft.com/cdo/configuration/sendpassword",
pGmailPassword);
myMail.Fields.Add
("http://schemas.microsoft.com/cdo/configuration/smtpusessl",
"true");
myMail.From = pGmailEmail;
myMail.To = pTo;
myMail.Subject = pSubject;
myMail.BodyFormat = pFormat;
myMail.Body = pBody;
if (pAttachmentPath.Trim() != "")
{
MailAttachment MyAttachment =
new MailAttachment(pAttachmentPath);
myMail.Attachments.Add(MyAttachment);
myMail.Priority = System.Web.Mail.MailPriority.High;
}
System.Web.Mail.SmtpMail.SmtpServer = "smtp.gmail.com:465";
System.Web.Mail.SmtpMail.Send(myMail);
return true;
}
catch (Exception ex)
{
throw;
}
}
}
No comments:
Post a Comment