sanchop22
sanchop22

Reputation: 2809

Gmail smtp authentication issue in windows service

I am sending emails with no problem through a windows service by using the code below:

    public bool Send()
    {
        bool RetSt = false;

        try
        {
            MailMessage mail = new MailMessage();
            mail.From = new MailAddress(_from);
            for (int i = 0; i < _to.Count; i++)
                mail.To.Add(_to[i]);
            mail.Subject = _subject;
            mail.Body = _body;
            for (int i = 0; i < _attachmentList.Count; i++)
                mail.Attachments.Add(_attachmentList[i]);

            SmtpClient smtp = new SmtpClient();
            smtp.Host = _smtpHost;
            smtp.Port = _smtpPort;
            smtp.Credentials = new NetworkCredential(_userName, _password);
            smtp.EnableSsl = true;
            smtp.Send(mail);

            for (int i = 0; i < _attachmentList.Count; i++)
                _ms[i].Dispose();

            RetSt = true;
        }
        catch (Exception ex)
        {
            Service.WriteEventLog(ex.ToString(), EventLogEntryType.Error);
        }

        return RetSt;
    }

For about 2-3 days after the start of service, service stops sending emails due to the authentication issue of Gmail. Here is the exception of that problem:

"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. Learn more at at System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response) at System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, MailAddress from, Boolean allowUnicode) at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, Boolean allowUnicode, SmtpFailedRecipientException& exception) at System.Net.Mail.SmtpClient.Send(MailMessage message) at MaintenanceService.Email.Send() in c:\Users\aydogan.ersoz\Desktop\maintenanceservice\trunk\MaintenanceServiceTest\Email.cs:line 75"

When i control Gmail account, Gmail asks for captcha. I enter captcha correctly and my windows service starts to work properly again.

I tried Google's solution but it didn't work.

What should i do to disable captcha protection or is there something to do to send emails programatically without entering captcha string from the web?

Upvotes: 0

Views: 692

Answers (1)

Xaqron
Xaqron

Reputation: 30837

GMail has many security considerations for spam filtering like amount of emails you send per hour, your emails being marked spam by receivers...

If you are going to use the GMail for bulk mailing you will encounter many problems. Use your own mail server in that case.

Upvotes: 1

Related Questions