Friday, June 22, 2012

Sending mail using gmail in .net

vb.net
Import namespace " System.Net.Mail".And add following coding on button click
Dim m As MailMessage =  New MailMessage() 
  m.To.Add("Email id of receiver")
  m.From = New MailAddress("Add your email it here")
  m.Subject = "Subject line of your mail is kept here"

  String Body = "Add you mail body here" 
   m.Body = Body
 
  mail.IsBodyHtml = True
  Dim smtp As SmtpClient =  New SmtpClient() 
  smtp.Host = "smtp.gmail.com" 
  smtp.Credentials = New System.Net.NetworkCredential
       ("your gmail id","password of gmail")
  smtp.EnableSsl = True
  smtp.Send(mail)
 
C# 
 
MailMessage mail = new MailMessage();
mail.From = new System.Net.Mail.MailAddress("your email");
SmtpClient smtp = new SmtpClient();
smtp.Port = 587;   
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network; 
smtp.UseDefaultCredentials = false; 
smtp.Credentials = new NetworkCredential("your emailid","password_here");
smtp.Host = "smtp.gmail.com";           
mail.To.Add(new MailAddress("receiver email"));
mail.IsBodyHtml = true;
string subject = "Test Mail";
mail.Body = subject;
smtp.Send(mail);
 
You can also add email sending credentials in web.config file as and 
just write following code
 
<system.net> 
    <mailSettings>
      <smtp from="sender@yourdomai.com">
        <network host="smtpserver1" port="25" userName="username" password="secret" defaultCredentials="true" />
      </smtp>
    </mailSettings>
  </system.net>
 
 
MailMessage message = new MailMessage();
message.From = new MailAddress("your email address");

message.To.Add(new MailAddress("receivers email address"));


message.CC.Add(new MailAddress("any email id as cc"));
message.Subject = "Place subject of mail here";
message.Body = "enter your message here";

SmtpClient client = new SmtpClient();
client.Send(message);
 
 

No comments :

Post a Comment