Thursday, 29 December 2011

Send mail through Smtp as gmail to any other mail

Source code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>






//Add this red color script from your solution or download it from google


<%----%>





Swash Contact Form




Please enter the following requested
information below to send us your Message.





 

<%--
--%>


<%----%>





Send a mail From www.swashglobal.com




My code behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnReset_Click(object sender, EventArgs e)
{

}

protected void btnSendmail_Click(object sender, ImageClickEventArgs e)
{
try
{

MailMessage mail = new MailMessage();
SmtpClient smtp = new SmtpClient();
mail.From = new MailAddress(txtName.Text);
// mail.To.Add(new MailAddress(txtTo.Text));
mail.To.Add(new MailAddress(txtEmail.Text));

mail.Subject = txtSubject.Text;
mail.Body = txtName.Text + txtMessage.Text;
mail.IsBodyHtml = true;
smtp.Host = "smtp.gmail.com";




smtp.EnableSsl = true;
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
NetworkCred.UserName = txtName.Text;
NetworkCred.Password = "Password123";
//NetworkCred.UserName = "xyz@shibashish.com";
// NetworkCred.Password = "Password123!";
smtp.UseDefaultCredentials = true;

smtp.Credentials = new System.Net.NetworkCredential(txtName.Text, "Password123");

smtp.Port = 587;//this is Gmail port for e-mail
//smtp.Port = 25;// this is default port no
smtp.Send(mail);//send an e-mail

lblStatus.Text = "Message Sended";
Page.RegisterStartupScript("ScriptDescription", "");
}
catch (Exception ex)
{
throw ex;
}
}
}

No comments:

Post a Comment