Tuesday 28 February 2012

mail send with asp.net with google app email id create


using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net;
using System.Net.Mail;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        lblmsg.Text = "";
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        SmtpClient smtpClient = new SmtpClient();
        MailMessage message = new MailMessage();
        try
        {

            //MailAddress fromAddress = new MailAddress(txtEmailId.Text);
         
          //  message.From = fromAddress;


            message.From = new MailAddress(txtEmailId.Text.ToString(), txtEmailId.Text, System.Text.Encoding.UTF8);

            message.To.Add("******@yahoo.co.in");
            message.CC.Add("**********@********.com");
          message.Bcc.Add("**********@*********.com");
            message.IsBodyHtml = true;
           
            message.Subject = "testing mail";
            message.Body = "Name=" + txtName.Text + "<br/>Email Id = " + txtEmailId.Text + "<br/>" + txtContactNo.Text + "<br/><hr/>" + "Contact Query :" + txtCQuery.Text;
            smtpClient.EnableSsl = true;


            //SmtpClient mySmtpClient = new SmtpClient();
            System.Net.NetworkCredential myCredential = new System.Net.NetworkCredential("*******@gmail.com", "**********");
            smtpClient.Host = "smtp.gmail.com";
            smtpClient.UseDefaultCredentials = false;
            smtpClient.Credentials = myCredential;
            smtpClient.ServicePoint.MaxIdleTime = 1;

            smtpClient.Send(message);
            message.Dispose();

           // smtpClient.Send(message);
         
            lblmsg.Visible = true;
            lblmsg.Text = "We Will Contact You Soon......";

            Clertxt();
        }
        catch (Exception ex)
        {
            lblmsg.Text = "Send Email Failed." + ex.Message;
        }




    }
    private void Clertxt()
    {
        txtName.Text = ""; txtEmailId.Text = ""; txtContactNo.Text = ""; txtCQuery.Text = "";
    }
   
}

No comments:

Post a Comment