Ryan Gray
Ryan Gray

Reputation: 71

Email Timeout C#

I have a program that sends an email with attachments, it works fine at home with a good internet connection but times out when I use a slower connection.

Does anyone know if I can extend the time out so it will send over a slower network.

The code I use is

Cursor.Current = Cursors.WaitCursor;

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

mail.From = new MailAddress(FilePaths.Default.SquadronEmailAddress);
mail.To.Add(FilePaths.Default.Email);
mail.Subject = FilePaths.Default.SquadronNumber + " Gliding Training Return";
mail.Body = "Please find attached todays Training Return from " + FilePaths.Default.SquadronNumber +
    ". Please do not reply to this email address as its unmonitored. " +
    "If you have any questions or require further information please contact the Adj on [email protected]";
mail.Attachments.Add(new Attachment(FilePaths.Default.GlidingTrainingReturnFolder + ("\\Gliding Training Return" + date.ToString("dd-MMM-yyyy") + ".pdf")));

SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential(FilePaths.Default.SquadronEmailAddress, FilePaths.Default.DatabasePassword);
SmtpServer.EnableSsl = true;

SmtpServer.Send(mail);
MessageBox.Show("Email Sent");

Upvotes: 0

Views: 1048

Answers (2)

Mauricio Gracia Gutierrez
Mauricio Gracia Gutierrez

Reputation: 10844

Use the timeout property of the SmtpClient class

SmtpServer.timeout = 200000 ; //change it as needed

specifies the time-out value in milliseconds. The default value is 100,000 (100 seconds).

By the way calling SmtpServer a variable that is actually a SmtpClient is very bad practice

For more details see this link

Upvotes: 1

poke
poke

Reputation: 387687

You can change the timeout by modifying the SmtpClient.Timeout property. It defaults to 100 seconds, which is already a lot, so if you are really exceeding that, you might want to look for a different solution instead. You could for example upload the attachment somewhere and send a link instead—the recipient will thank you too.

Upvotes: 1

Related Questions