0

I'm trying to send an email from web service that located at the DMZ using pineApp server that suppose to move the message to the exchange server inside the LAN. The IIS server which the ASMX is on, is defined as relay at the pineApp server.

I'm getting the

An existing connection was forcibly closed by the remote host

and I'm not sure why. If I test it from inside the LAN and call directly to the exchange, it works fine.

This is the code I use:

 MailMessage mailMessage = new MailMessage();
                mailMessage.From = new MailAddress("mail@stam.co.il");
                MailAddress mailMessageTo = new MailAddress(Recipients);
                mailMessage.To.Add(mailMessageTo);
                mailMessage.Subject = Subject;
                mailMessage.Body = Body;
                mailMessage.IsBodyHtml = true;
                SmtpClient smtpClient = new SmtpClient(SmtpClientName,25);
                smtpClient.EnableSsl = false;
                smtpClient.UseDefaultCredentials = false;
                smtpClient.Send(mailMessage);

I hope I include all the information.

Wosi
  • 41,986
  • 17
  • 75
  • 82
Dror T
  • 221
  • 2
  • 9
  • Is there an inner exception and if so what is it? Where are you defining the username and password for the SMTP? – user1666620 Oct 10 '16 at 08:26
  • http://stackoverflow.com/questions/5420656/unable-to-read-data-from-the-transport-connection-an-existing-connection-was-f Check this question, it is good practise to search the exception when you have problems. – mybirthname Oct 10 '16 at 08:37

1 Answers1

0

What may cause this is: Send email. Wait for a long time, for example 10 minutes. Send another mail using the same connection. In this situation your .net client thinks the connection is still open. However, the SMTP server killed the connection on the server side.

What we did about this: if the innerexception is a IOException, we dispose the SmtpClient object, create a new one and retry to send the e-mail

JanO
  • 81
  • 5