Email sending in C# gmail



using (MailMessage mm = new MailMessage("from@gmail.com", "to@gmail.com"))
            {
                mm.Subject = "Subject Text";
                mm.Body = "Body Text";

                mm.IsBodyHtml = false;
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "smtp.gmail.com";
                smtp.EnableSsl = true;

                NetworkCredential NetworkCred = new NetworkCredential("from@gmail.com", "pwm");
                ServicePointManager.ServerCertificateValidationCallback =
                delegate(object s, X509Certificate certificate,
                         X509Chain chain, SslPolicyErrors sslPolicyErrors)
                { return true; };
                smtp.UseDefaultCredentials = false;
                smtp.Credentials = NetworkCred;
                smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                smtp.Port = 587;
                smtp.Send(mm);
                //ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Email sent.');", true);
            }

4 comments: