0

I am trying to send email using ASP.NET through Gmail server. This is my code, can you help me how to resolve the timeout issue.

        try
        {
            MailMessage mail = new MailMessage();
            SmtpClient smtp = new SmtpClient("smtp.gmail.com"); 

            mail.IsBodyHtml = true;
            mail.Priority = System.Net.Mail.MailPriority.High;
            mail.From = new MailAddress("FromEmail@gmail.com");
            mail.To.Add("ToEmail@gmail.com");
            mail.Subject = "Fees Reminder";                
            mail.Body = "Please, Student ID: " + username + " pay your fees " + sqlFeesValue + ".";
            mail.Body += " <html>";
            mail.Body += "<body>";
            mail.Body += "<table>";
            mail.Body += "<tr>";
            mail.Body += "<td>User Name : </td><td>" + username + "</td>";
            mail.Body += "</tr>";
            mail.Body += "<tr>";
            mail.Body += "<td>Fees : </td><td> " + sqlFeesValue.ToString() +"</td>";
            mail.Body += "</tr>";
            mail.Body += "</table>";
            mail.Body += "</body>";
            mail.Body += "</html>";

            smtp.Port = 465;
            smtp.Credentials = new System.Net.NetworkCredential("email@gmail.com", "passwd");
            smtp.EnableSsl = true;                
            smtp.UseDefaultCredentials = true;
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtp.Send(mail);
        }
        catch (Exception error)
        {
            lblMessage.Visible = true;
            lblMessage.Text = error.Message;
        }

Thanks in advance

Badar
  • 1,430
  • 1
  • 15
  • 19
  • 1
    See http://stackoverflow.com/questions/32260/sending-email-in-net-through-gmail – Patrick Hofman Sep 19 '14 at 08:52
  • I think you need to create body in a string variable assign into body, Then try. Or may be you are using wrong port number – Amit Sep 19 '14 at 08:54
  • Try port 587. Are you sure you don't get the gmail timeout because of the new security settings in gmail? – Alexander Sep 19 '14 at 08:55
  • As I know SSL uses 465. What are the new security setting: Alexander. – Badar Sep 19 '14 at 08:57
  • Have you tried debugging it? – Izzy Sep 19 '14 at 09:09
  • When debugging I can see full message in Send(mail). The mail variable can show me a complete packet. – Badar Sep 19 '14 at 09:18
  • I changed code a little bit and it shows this message: "The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required." – Badar Sep 19 '14 at 09:24
  • Login to your Gmail account and see the recent activity. From there you can select "Yes it was me" and then re-run the code and `server response was: 5.5.1 Authentication Required` error should go away – Izzy Sep 19 '14 at 09:30
  • Thanks Izzy! Now it works. I had got message from Google but didn't pay my heed to it. But thanks – Badar Sep 19 '14 at 09:36

1 Answers1

0

Try using a different port number. Gmail will accept port 25 or 587 when sending mail but times out using port 465.

Also make sure you have UseDefaultCredentials = False also.

mrsrizan
  • 301
  • 1
  • 8