0

I am trying to run this code but I am getting the following exception: Failure sending mail.

And my Code is given below:

try
        {
            MailMessage mail = new MailMessage("from@gmail.com", "to@gmail.com");
            SmtpClient client = new SmtpClient
            {
                Port = 465,
                UseDefaultCredentials = false,
                Host = "smtp.gmail.com",
                EnableSsl = true,
                Credentials = new NetworkCredential("from@gmail.com", "xxxxxxx")
            };
            mail.Subject = "Hello";
            mail.Body = "This is test Message";
            client.Send(mail);
        }
        catch (Exception _ex)
        {
            Debug.Write(_ex.Message);
        }

Even I turn on the 'Less secure App '

  • Possible duplicate of [Sending email in .NET through Gmail](https://stackoverflow.com/questions/32260/sending-email-in-net-through-gmail) – VDWWD Jan 25 '19 at 07:41

1 Answers1

0

Assuming your credential is correct, then could be something wrong initialise.

Here is my snippet

            SmtpClient client = new SmtpClient();
            MailMessage mailMessage = new MailMessage();

            client = new SmtpClient("smtp.xxx.com", port_number);
            client.Credentials = new NetworkCredential("xxx@xxx.xx", "xxx");
            client.EnableSsl = true;
            mailMessage.From = new MailAddress("from@xxx.co", "xx xx", Encoding.UTF8);
            mailMessage.Subject = $"mail subject";

            mailMessage.Body = emailHtml;
            mailMessage.IsBodyHtml = true;

            mailMessage.To.Add("to@xx.com");
            mailMessage.Bcc.Add("bcc@xx.com");
            await client.SendMailAsync(mailMessage);
Kira Hao
  • 1,077
  • 11
  • 17