Sending email using SMTP in C #

I am using the following code to send email via MS Exchange Server on Windows Domain:

SmtpClient client = new SmtpClient();
client.Port = 25;
client.Host = "mail.mydomain.com";
client.EnableSsl = false;
client.Timeout = 60000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Credentials = new System.Net.NetworkCredential("sender@mydomain.om", "password");
client.UseDefaultCredentials = false;

MailMessage mm = new MailMessage("sender@mydomain.com", "receiver@anotherdomain.com", "test", "test");
mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;

try
{
      client.Send(mm);
      MessageBox.Show("Success");
}
catch (Exception ex)
{
      MessageBox.Show("Faild: "+ex.Message);
}

      

Most of the time this works fine and sends email without issue, but after every 2 or 3 successful submissions, it fails 1 or 2 times with a timeout error (time expires after 10 seconds, which is much less than the 60 seconds set for the client timeout in my code ).
My mail server is MS Exchange and is in the same domain that is connected to the client computer over the local network, so I think there is no network problem and this should lead to a configuration mismatch on the client or server.
Any idea?

+3


source to share


2 answers


The SmtpClient class does not have a Finalize method, so the application must call Dispose to explicitly release resources. If there is no help, you can check the connection limitation setting on the exchange server.



0


source


SmtpClient.TimeOut

has a default value 100 seconds(10000MS)

. but it can also be installed through the program, since this property has set

and get

.

public int Timeout { get; set; }

      



It Gets or sets a value that indicates the amount of time that the sync call forwarding times out. This way you can try increasing

the timeout value as your method fails (throws an exception) in some scenarios. Link

0


source







All Articles