0
MailMessage msg = new MailMessage("teunenrichard@gmail.com",        "ipadcraze@hotmail.com", "Movies this month", "Hello this is a test mail");
msg.IsBodyHtml = false;
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.UseDefaultCredentials = false;
NetworkCredential xre = new System.Net.NetworkCredential("teunenrichard@gmail.com", "Password");
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Credentials = xre;
smtp.EnableSsl = true;
smtp.Send(msg);

This is the code i run in a form . load to do a test email but it will not rund and says operation timed out. ive tried everything please help MessageBox.Show("mail sent");

DhavalR
  • 1,409
  • 3
  • 29
  • 57
Richard Teunen
  • 79
  • 1
  • 1
  • 5

3 Answers3

0

Use the "Timeout" property for your Smtp client . I think 0 is max

 smtp.Timeout = 0;
rmehra76
  • 404
  • 2
  • 10
0

For better Understanding of the error message try putting your code in a Try-Catch Block and then see the inner exception of the catch in MessageBox.Show(). It might provide you some more information regarding the error and might help/guide you in the right direction to resolve it. Something like below:-

try
{
  //your email sending logic
}
catch(Exception ex)
{
  MessageBox.Show(ex.InnerException.ToString());
}
Gaurav Shah
  • 322
  • 3
  • 7
0

You should send your mails using background thread, so as to make sure that your UI thread does not block and returns immediately. You can do something like this

 private async void sendButton_Click(object sender, EventArgs e)
        {
            var result = await SendMail();
            if (result)
            {
                MessageBox.Show("Mail sent");
            }
        }

        private Task<bool> SendMail()
        {
            var task = Task.Run<bool>(() =>
            {
                MailMessage msg = new MailMessage("sendermail@gmail.com", "recievermail@gmail.com", "Movies this month", "Hello this is a test mail");
                msg.IsBodyHtml = false;

                using(SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587))
                {
                    smtp.UseDefaultCredentials = false;
                    NetworkCredential xre = new NetworkCredential("sendermail@gmail.com", "Password");
                    smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                    smtp.Credentials = xre;
                    smtp.EnableSsl = true;
                    smtp.Send(msg);

                    return true;
                }

            });
            return task;
        }
Cizaphil
  • 490
  • 1
  • 6
  • 23