Translate

Saturday, 10 January 2015

Send Mail in c# through gmail

using System.Configuration;
using System.Net;
using System.IO;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
----------------
 protected void btnSendMail1_Click(object sender, EventArgs e)
      {
          string ToWhomYouMail = "pabitrakiims@gmail.com";//Send to mail
          string Subject = "Test Mail";
          string mailbody = "<html><body><div>Dear Sir ,Good Morning</div></body></html>";
          bool m = send_mail(ToWhomYouMail, Subject, mailbody);
          if (m)
          {
              ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('Mail Send Sucessfully !');", true);
          }
          else
          {
              ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('An Error Occurs !');", true);
          }
      }



 public bool send_mail(string ToMail,string subject,string body)
       {
           bool k = false;
           try
           {
               MailMessage msg = new MailMessage("pabitra.best28@gmail.com", ToMail);
               msg.To.Add(ToMail);
            msg.Subject = subject;
            msg.Body = body;
            msg.IsBodyHtml = true;
            AlternateView view;
            SmtpClient client;
            msg.IsBodyHtml = true;
            client = new SmtpClient();
            client.Host = "smtp.gmail.com";
            client.Port = 587;
            client.Credentials = new System.Net.NetworkCredential("pabitra.best28@gmail.com", "password@123");
            client.EnableSsl = true; //Gmail works on Server Secured Layer
            client.Send(msg);
            k = true;
            //return k;
        }catch(Exception ex)
        {
            k = false;
        }
        return k;
     
    }

No comments:

Post a Comment