Search code examples
c#smtpgmail

SMTP Gmail timing out


Not sure why this is happening. Every where I've search tells me that i'm doing this right. But every time I try and send the mail, it times out on the smtpserver.Send(mail)

private void emailReport(string email_address,int begDatabaseCount, int endDatabaseCount)
        {
            SmtpClient smtpserver = new SmtpClient();
            MailMessage mail = new MailMessage();
            smtpserver.EnableSsl = true;
            smtpserver.Port = 465;
            smtpserver.Host = "smtp.gmail.com";           
            smtpserver.Credentials = new NetworkCredential("[email protected]", "password");
            smtpserver.UseDefaultCredentials = false;
            mail = new MailMessage();
            mail.From = new System.Net.Mail.MailAddress("[email protected]", "ATR Reports");
            mail.To.Add(email_address);
            mail.Subject = "FNAS Report - " + DateTime.Now;
            mail.Body += "<u><b>FNAS Report for " + DateTime.Now + "</u></b>" + "\r\n \r\n";
            mail.Body += "Beginning Database Count - " + begDatabaseCount + "\r\n" + "\r\n";
            mail.Body += "End Database Count - " + endDatabaseCount + "\r\n" + "\r\n";
            mail.Body += "<b>Total Imported Orders = " + (endDatabaseCount - begDatabaseCount) + "<b>" + "\r\n" + "\r\n";
            mail.IsBodyHtml = true;

            smtpserver.Send(mail);
        }

Port 465 = Time Out after 1 minute

Port 587 = "The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. "


Solution

  • This thread helped me. I'm not sure why this code worked and mine wasn't.

    Sending email in .NET through Gmail

    using System.Net;
    using System.Net.Mail;
    
    var fromAddress = new MailAddress("[email protected]", "From Name");
    var toAddress = new MailAddress("[email protected]", "To Name");
    const string fromPassword = "fromPassword";
    const string subject = "Subject";
    const string body = "Body";
    
    var smtp = new SmtpClient
               {
                   Host = "smtp.gmail.com",
                   Port = 587,
                   EnableSsl = true,
                   DeliveryMethod = SmtpDeliveryMethod.Network,
                   UseDefaultCredentials = false,
                   Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
               };
    using (var message = new MailMessage(fromAddress, toAddress)
                         {
                             Subject = subject,
                             Body = body
                         })
    {
        smtp.Send(message);
    }