Search code examples
c#smtpgmailsmtpclient

How to send an e-mail with C# through Gmail


I am getting an error when trying to send an e-mail through my web service. I have tried enabling access to less secure apps disabling 2-step verification and logging into the account via a web browser. None of the solutions on SO have worked for me. I am still getting:

Error: System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.

What can I do to fix this issue?

namespace EmailService
{
    public class Service1 : IService1
    {    
        public string SendEmail(string inputEmail, string subject, string body)
        {
            string returnString = "";
            try
            {
                MailMessage email = new MailMessage();
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "smtp.gmail.com";

                // set up the Gmail server
                smtp.EnableSsl = true;
                smtp.Port = 587;
                smtp.Credentials = new System.Net.NetworkCredential("[email protected]", "mypassword");
                smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtp.UseDefaultCredentials = false;

                // draft the email
                MailAddress fromAddress = new MailAddress("[email protected]");
                email.From = fromAddress;
                email.To.Add(inputEmail);
                email.Subject = body;
                email.Body = body;

                smtp.Send(email);

                returnString = "Success! Please check your e-mail.";
            }
            catch(Exception ex)
            {
                returnString = "Error: " + ex.ToString();
            }
            return returnString;
        }
    }
}

Solution

  • Just Go here : Less secure apps , Log on using your Email and Password which use for sending mail in your c# code , and choose Turn On.

    Also please go to this link and click on Continue Allow access to your Google account

    also I edit it little bit :

    public string sendit(string ReciverMail)
    {
        MailMessage msg = new MailMessage();
    
        msg.From = new MailAddress("[email protected]");
        msg.To.Add(ReciverMail);
        msg.Subject = "Hello world! " + DateTime.Now.ToString();
        msg.Body = "hi to you ... :)";
        SmtpClient client = new SmtpClient();
        client.UseDefaultCredentials = true;
        client.Host = "smtp.gmail.com";
        client.Port = 587;
        client.EnableSsl = true;
        client.DeliveryMethod = SmtpDeliveryMethod.Network;
        client.Credentials = new NetworkCredential("[email protected]", "YourPassword");
        client.Timeout = 20000;
        try
        {
           client.Send(msg);
            return "Mail has been successfully sent!";
        }
        catch (Exception ex)
        {
            return "Fail Has error" + ex.Message;
        }
        finally
        {
           msg.Dispose();
        }
    }
    

    If the above code don't work , try to change it like the following code :

        SmtpClient client = new SmtpClient();
        client.Host = "smtp.gmail.com";
        client.Port = 587;
        client.EnableSsl = true;
        client.DeliveryMethod = SmtpDeliveryMethod.Network;
        client.UseDefaultCredentials = false;
        client.Credentials = new NetworkCredential("[email protected]", "YourPassword");