Srikanth Technologies

Sending Mail In ASP.NET 2.0 Using Gmail

I have written a small program to send a mail from my Gmail account to Yahoo account. The program uses ASP.NET 2.0 and uses SMTP server of Gmail. We have to enable SSL and find out the port number of SMTP of GMAIL, which happens to be 587. But try 465 als also. I have tasted success with 465 also in the past. But I could not send with 465 at times and program aborted with operation timed out> error. 

As iAs it is a simple program to test a simple mail, I am not taking any input from user. I have used simple body and subject. I have run this and got a mail into my inbox of yahoo.

Just create a new ASP.NET page and place a button on it. Write code for ong>click event of that button. To test it, just run the page and click on the button. If you see no errors, it means message is sent successfully.

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Net;

public partial class SendMail : System.Web.UI.Page
{
    protected void Button1_Click(object sender, EventArgs e)
    {
        MailMessage m = new MailMessage("srikanthpragada@gmail.com", "srikanthpragada@yahoo.com");
        m.Body = "Testing Mail From Gmail.com";
        m.IsBodyHtml = false;
        m.Subject = "Testing";
        m.Priority = MailPriority.High;
        
            // SMTP settings
        SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);  // server name and port number
        smtp.UseDefaultCredentials = false;
        smtp.EnableSsl = true;
        smtp.Credentials = new NetworkCredential ("srikanthpragada@gmail.com","password");
        smtp.Send(m);
    }
}pre>