Exception SmtpClient

G

Guest

Hi,

I have a problem sending mails with the SmtpClient class. It's strange -
after I boot the pc and start the program I can send mails many times. After
I close the program and start it again it sometimes works too, but often does
not. If it does not, he will never - until the next reboot. Then the "game"
starts again.
I get the exception "'No connection could be made because the target machine
actively refused it.". Telnet to port 25 will be refused too after this
happens the first time. After reboot telnet (and my program) works fine -
until the next error.
I use an internal mail server (ms exchange 2003) that is used by many other
programs - and they run fine. I tried it from diffrent clients but it is all
the same. My assumption is, that the connection is not closed properly and
the server refuses another (or too much) connections.
I tried to dispose the smtpclient after using it but it does not have a
..Dispose(). Because of that I cannot use using(). Is there another way to
check (or close) the connection?

Here the code:

SmtpClient smtpClient = new SmtpClient(config.MailServer);
smtpClient.UseDefaultCredentials = config.SmtpAuthentification;
string mailBody = "Sample Body";
foreach (string mailRecipient in config.MailRecipients)
{
using (MailMessage message = new
MailMessage(config.MailSender, mailRecipient))
{
message.Subject = "Sample Subject";
message.Body = mailBody;
smtpClient.Send(message);
}
}

Thank you for any hints!!

Torsten
 
P

Patrick Steele

Hi,

I have a problem sending mails with the SmtpClient class. It's strange -
after I boot the pc and start the program I can send mails many times. After
I close the program and start it again it sometimes works too, but often does
not. If it does not, he will never - until the next reboot. Then the "game"
starts again.
I get the exception "'No connection could be made because the target machine
actively refused it.". Telnet to port 25 will be refused too after this
happens the first time. After reboot telnet (and my program) works fine -
until the next error.
I use an internal mail server (ms exchange 2003) that is used by many other
programs - and they run fine. I tried it from diffrent clients but it is all
the same. My assumption is, that the connection is not closed properly and
the server refuses another (or too much) connections.
I tried to dispose the smtpclient after using it but it does not have a
.Dispose(). Because of that I cannot use using(). Is there another way to
check (or close) the connection?

Here the code:

SmtpClient smtpClient = new SmtpClient(config.MailServer);
smtpClient.UseDefaultCredentials = config.SmtpAuthentification;
string mailBody = "Sample Body";
foreach (string mailRecipient in config.MailRecipients)
{
using (MailMessage message = new
MailMessage(config.MailSender, mailRecipient))
{
message.Subject = "Sample Subject";
message.Body = mailBody;
smtpClient.Send(message);
}
}

Thank you for any hints!!

Hmmmm... I wonder if smtpClient.ServicePoint.CloseConnectionGroup()
would help? I don't know if that's even something you *should* do, even
if it does work. I was just poking around the docs...
 
G

Guest

Hi Patrick,

I found the CloseConnectionGroup method, too. But it needs an argument (name
of the con group). So it would be intresting if I could/should use this
method and where I can get the name of the group. Do you have further
information about this method?

Torsten
 
G

Guest

Hi Sloan,

Thanks for the link. I will have a look at it. But I don't think the problem
is about authentication. It works fine after rebooting (if there would be a
problem with authentication it should have never work). I tried the same
steps with the smtpClient.UseDefaultCredentials = true. The result was the
same. And I checked the exchange configuration - it does not need
authentication (it is a local server in the intranet and is not accessable
from the internet).

Torsten
 
P

Patrick Steele

I found the CloseConnectionGroup method, too. But it needs an argument (name
of the con group). So it would be intresting if I could/should use this
method and where I can get the name of the group. Do you have further
information about this method?

Sorry, no. I was just poking around the docs. I still think your code
is doing things correctly and I can't imagine why a port is being left
open.

Does this happen only when you're running in the debugger? What if you
run it outside the debugger? What happens if you run it in Release vs.
Debug mode?
 
G

Guest

Thanks for your help, Patrick.
I tested without the debugger - but I tried release and debug code. There is
no Visual Studio installed on the machines. Do you think I should test it
from VS? This is a little bit tricky, because my laptop is not part of this
domain.
My idea with the open port comes from the behavior of the smtp server.
Telnet to port 25 should work always afaik. But after the exception of my
program, telnet is also refused by the server till the next reboot.
I found another thing - the virus scanner. On the machines run Symantec anti
virus corp ed. There is a smtp scanner integrated, too. Is it possible that
the problem is not really program<->smtp-server, but
program<->antivirus<->smtp-server? Any suggestions?
 
P

Patrick Steele

I found another thing - the virus scanner. On the machines run Symantec anti
virus corp ed. There is a smtp scanner integrated, too. Is it possible that
the problem is not really program<->smtp-server, but
program<->antivirus<->smtp-server? Any suggestions?

It's definitely a possibility. I would disable the SMTP scanner and see
what happens.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top