Search This Blog

Wednesday, November 18, 2009

send mail from asp.net to gmail

using System;
using System.Collections;
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.Mail;

// Now open your .aspx page replace the page_load with below code

protected void Page_Load(object sender, EventArgs e)
{
MailMessage message = new MailMessage();

try
{
MailAddress fromAddress = new MailAddress("pallelokanathareddy@gmail.com", "Lokanatha");
message.From = fromAddress;
message.To.Add("pallelokanathareddy@gmail.com");
message.Subject = "Feedback";
message.IsBodyHtml = false;
message.Body = "Hello Lokanatha";
sendmails(message);
Label1.Text = "Email successfully sent.";
}
catch (Exception ex)
{
Label1.Text = "Send Email Failed." + ex.Message;
}
}

public void sendmails(MailMessage mm)
{
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
NetworkCred.UserName = "pallelokanathareddy@gmail.com";
NetworkCred.Password = "youe actual gmail password for the mail id mentionaed in the above code"; ;
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.Send(mm);

}


Smtp settings:

note: smtp server should be installed in your computer.your system must have internet connection
1.go to start>run
2.type "inetmgr" in the run press ok
3.in the newly opened internet service manager navigate to "default smtp virtual server"
4.go to domain right click new>domain
5.select remote and click next"smtp.gmail.com" and click finish.
6.now go to "default smtp virtual server" right click choose "Properties" and click.
7.in the ip address dropdown choose your system ip.
8.go to "access" tab click on "Relay"
9.selectradio buton "only the list below" and also check "allow all the computers which success fully authenticate to relay.regardless of the list above" click ok.
10.in the connection select "only the list below" click ok.
11.click on "Authentication" button choose "Anonymous Acess" click ok.
12.close your iis window.


Now run the code it sends email to the to address.