blogspot visit counter

Tuesday, 19 March 2013

On Page Load How I Can Send Email using Asp.net

On Page Load How I Can Send Email using Asp.net
In my previous Article I explained how to Sending email with GMail's SMTP server in ASP.NET.
This example shows how to Send Email Using Gmail In ASP.NET. If you want to send mail using Gmail account or it's SMTP server in ASP.NET application if you don't have a working smtp server to send mails than sending e-mail with Gmail is best option.

using System.Net.Mail;
After that write the following code in button click
protected void  Page_Load(object sender, EventArgs e)
{
try
{
string from=Request["from"];
string to=Request["to"];
string password=Request["password"]; 
string subject=Request["subject"];
string Body=Request["Body"]; 
MailMessage Msg = new MailMessage();
// Sender e-mail address.
Msg.From = new MailAddress(from);
// 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;
// pass the gmail user id and password
smtp.Credentials=new System.Net.NetworkCredential(from,password);
smtp.EnableSsl = true;
smtp.Send(Msg);
Response.Write("send");
// get the message

}
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

  In this example normal you can write a url as shown below .only you have to change the eamil id and password .

localhost:2796/emailsending/mailsender.aspx?from=xyz@gmail.com&to=avc@gmail.com&password=11123344&subject=hello&Body=this is a col example



Download sample code attached

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...