Saturday 28 March 2020

C# Send Email


Use the following Code to send Email
protected void SendMail()
        {

            MailAddress from = new MailAddress("your_email@gmail.com");
            MailAddress toMail = new MailAddress(to.Text);
            MailMessage message = new MailMessage(from, toMail);
            message.Subject = subject.Text;
            message.Body = body.Text;
            message.IsBodyHtml = true;
            // Add a carbon copy recipient.
            var smtp = new System.Net.Mail.SmtpClient();
            {
                smtp.Host = "smtp.gmail.com";
                smtp.Port = 587;
                smtp.UseDefaultCredentials = false;
                smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                smtp.Credentials = new NetworkCredential("your_email@gmail.com", " YourPassword");
                smtp.TargetName = "STARTTLS/smtp.gmail.com";
                smtp.EnableSsl = true;
                smtp.Timeout = 100000;
            }
            try
            {
                smtp.Send(message);
                MessageBox.Show("Mail Sent");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error  :  "+ex.StackTrace);
            }
        }