Friday 2 December 2011

How to send mail using Gmail in C#

How to send mail using .NET

Here is a simple code that sends mail from gmail account

I have used this code to get the details from contact page to my mail ID



The code uses System.Net Mail namespace


protected void btnSubmit_Click(object sender, EventArgs e) 
{  
    try
    {
        string sFrom = "telanganadeccan@gmail.com";
        string sTo = "info@rowsandcolumns.in";
        string sMessage = txtComments.Text + "\n" + txtName.Text;
        string sSubject = txtName.Text;
        string sMail = txtEmail.Text; 
        MailAddress oMailfrom = new MailAddress(sFrom);
        MailAddress oMailTo = new MailAddress( sTo);
        MailMessage message = new MailMessage(oMailfrom, oMailTo );
        message.Body = "From " + sSubject + "\n Mail ID " + sMail + "\n Comments :\n" + sMessage;
        SmtpClient client = new SmtpClient();
        client.Timeout = 20000;
        message.Subject = sSubject;
        client.EnableSsl = true;
        client.Send(message );
        
        Response.Redirect ("~/Thanks.aspx");
    }
    catch (Exception ex)
    {

        Response.Redirect("~/Thanks.aspx");
    }

0 comments:

Post a Comment