smtpClient.Send socket error

H

hazz

smtpClient.Send(message)

is causing me problems as per specifics in the trace below. Email is sent
but not without this error typically upon sending the second email, but
sometimes when running the app, even the first time. The application will
be required to be sending out repeated emails, about one every second or
two.
Must this be done asynchronously? Thank you. -Greg

I get the generic error messages;
Troubleshooting Exceptions: System.ObjectDisposedException 'Safe handle
has been closed'

An ObjectDisposedException exception is thrown when an operation is
attempted on a disposed object, such as a closed stream or registry key.

Make sure you have not released a resource before attempting to use it.
For example, if attempting to manipulate a stream, make sure it has not
been previously closed.




System.Net.Sockets Verbose: 0 : [2568] Exiting Socket#43667493::Send() ->
1024#1024
System.Net.Sockets Verbose: 0 : [2568] Socket#43667493::Send()
System.Net.Sockets Verbose: 0 : [2568] Data from Socket#43667493::Send
System.Net.Sockets Verbose: 0 : [2568] 00000000 : 52 65 63 4E 61 6D 65
7D-3C 42 52 3E 3D 30 44 3D : RecName}<BR>=0D=
System.Net.Sockets Verbose: 0 : [2568] 00000010 : 30 41 54 6F 74 61 6C
4D-6F 76 65 2E 63 6F 6D 3C : 0Axxxx.com<
System.Net.Sockets Verbose: 0 : [2568] 00000020 : 42 52 3E 3D 30 44 3D
30-41 28 38 37 37 29 20 39 : BR>=0D=0A(877) 9
System.Net.Sockets Verbose: 0 : [2568] 00000030 : 36 38 2D 4D 4F 56 45
20-28 36 36 38 33 29 3C 42 : 68-MOVE (6683)<B
System.Net.Sockets Verbose: 0 : [2568] 00000040 : 52 3E 3D 0D 0A 3D 30
44-3D 30 41 7B 52 65 63 45 : R>=..=0D=0A{RecE
System.Net.Sockets Verbose: 0 : [2568] 00000050 : 6D 61 69 6C 7D 3C 42
52-3E 3D 30 44 3D 30 41 3C : mail}<BR>=0D=0A<
System.Net.Sockets Verbose: 0 : [2568] 00000060 : 2F 66 6F 6E 74 3E 3C
2F-74 64 3E 3C 2F 74 72 3E : /font></td></tr>
System.Net.Sockets Verbose: 0 : [2568] 00000070 : 3C 2F 74 61 62 6C 65
3E-3C 2F 62 6F 64 79 3E 3C : </table></body><
System.Net.Sockets Verbose: 0 : [2568] 00000080 : 2F 68 74 6D 6C 3E :
/html>
System.Net.Sockets Verbose: 0 : [2568] Exiting Socket#43667493::Send() ->
134#134
System.Net.Sockets Verbose: 0 : [2568] Socket#43667493::Send()
System.Net.Sockets Verbose: 0 : [2568] Data from Socket#43667493::Send
System.Net.Sockets Verbose: 0 : [2568] 00000000 : 0D 0A : ..
System.Net.Sockets Verbose: 0 : [2568] Exiting Socket#43667493::Send() ->
2#2
System.Net.Sockets Verbose: 0 : [2568] Socket#43667493::Send()
System.Net.Sockets Verbose: 0 : [2568] Data from Socket#43667493::Send
System.Net.Sockets Verbose: 0 : [2568] 00000000 : 0D 0A 2E 0D 0A : .....
System.Net.Sockets Verbose: 0 : [2568] Exiting Socket#43667493::Send() ->
5#5
System.Net.Sockets Verbose: 0 : [2568] Socket#43667493::Receive()
System.Net.Sockets Verbose: 0 : [2568] Data from Socket#43667493::Receive
System.Net.Sockets Verbose: 0 : [2568] 00000000 : 32 35 30 20 4D 61 69
6C-20 71 75 65 75 65 64 20 : 250 Mail queued
System.Net.Sockets Verbose: 0 : [2568] 00000010 : 66 6F 72 20 64 65 6C
69-76 65 72 79 2E 0D 0A : for delivery...
System.Net.Sockets Verbose: 0 : [2568] Exiting
Socket#43667493::Receive() -> 31#31
System.Net Verbose: 0 : [2568] Exiting SmtpClient#25675301::Send()
System.Net.Sockets Verbose: 0 : [5560] Socket#57792633::Dispose()
System.Net.Sockets Verbose: 0 : [5560] Socket#57792633::Shutdown(Both#2)
System.Transactions Critical: 0 : <TraceRecord
xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord"
Severity="Critical"><TraceIdentifier>http://msdn.microsoft.com/TraceCodes/System/ActivityTracing/2004/07/Reliability/Exception/Unhandled</TraceIdentifier><Description>Unhandled
exception</Description><AppDomain>LeadProcessingServiceConsole.vshost.exe</AppDomain><Exception><ExceptionType>System.ObjectDisposedException,
mscorlib, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089</ExceptionType><Message>Safe handle has been
closed</Message><StackTrace> at
System.Net.UnsafeNclNativeMethods.OSSOCK.shutdown(SafeCloseSocket
socketHandle, Int32 how)
at System.Net.Sockets.Socket.Shutdown(SocketShutdown how)
at
?.Finalize()</StackTrace><ExceptionString>System.ObjectDisposedException:
Safe handle has been closed
at System.Net.UnsafeNclNativeMethods.OSSOCK.shutdown(SafeCloseSocket
socketHandle, Int32 how)
at System.Net.Sockets.Socket.Shutdown(SocketShutdown how)
at ?.Finalize()</ExceptionString></Exception></TraceRecord>
System.Net Error: 0 : [5560] Exception in the
#62476613::UnhandledExceptionHandler - Safe handle has been closed
System.Net Error: 0 : [5560] at
System.Net.UnsafeNclNativeMethods.OSSOCK.shutdown(SafeCloseSocket
socketHandle, Int32 how)
at System.Net.Sockets.Socket.Shutdown(SocketShutdown how)
at ?.Finalize()
 
P

Paul Henderson

smtpClient.Send(message)
I get the generic error messages;
Troubleshooting Exceptions: System.ObjectDisposedException 'Safe handle
has been closed'
Must this be done asynchronously? Thank you. -Greg

Apparently the SmtpClient is finalizing the underlying socket twice; it
might mean that you are using the SmtpClient when it is not in a
suitable state, though it's difficult to see why without knowing what
your code looks like. There should be no need to do it asynchronously,
though it is neater; in this context, one would expect sending
synchronously to be more reliable as you can be sure that the
SmtpClient has finished sending the previous message; if you 'doubled
up' sending because it was asynchronous, that would cause an exception.
It'd be helpful if you could post some code, though.
 
K

Kevin Spencer

No, it's not necessary to use threading, but without knowing how you *are*
doing this, I can't tell you more than that.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Hard work is a medication for which
there is no placebo.

hazz said:
smtpClient.Send(message)

is causing me problems as per specifics in the trace below. Email is sent
but not without this error typically upon sending the second email, but
sometimes when running the app, even the first time. The application
will be required to be sending out repeated emails, about one every second
or two.
Must this be done asynchronously? Thank you. -Greg

I get the generic error messages;
Troubleshooting Exceptions: System.ObjectDisposedException 'Safe
handle has been closed'

An ObjectDisposedException exception is thrown when an operation is
attempted on a disposed object, such as a closed stream or registry key.

Make sure you have not released a resource before attempting to use it.
For example, if attempting to manipulate a stream, make sure it has not
been previously closed.




System.Net.Sockets Verbose: 0 : [2568] Exiting Socket#43667493::Send() ->
1024#1024
System.Net.Sockets Verbose: 0 : [2568] Socket#43667493::Send()
System.Net.Sockets Verbose: 0 : [2568] Data from Socket#43667493::Send
System.Net.Sockets Verbose: 0 : [2568] 00000000 : 52 65 63 4E 61 6D 65
7D-3C 42 52 3E 3D 30 44 3D : RecName}<BR>=0D=
System.Net.Sockets Verbose: 0 : [2568] 00000010 : 30 41 54 6F 74 61 6C
4D-6F 76 65 2E 63 6F 6D 3C : 0Axxxx.com<
System.Net.Sockets Verbose: 0 : [2568] 00000020 : 42 52 3E 3D 30 44 3D
30-41 28 38 37 37 29 20 39 : BR>=0D=0A(877) 9
System.Net.Sockets Verbose: 0 : [2568] 00000030 : 36 38 2D 4D 4F 56 45
20-28 36 36 38 33 29 3C 42 : 68-MOVE (6683)<B
System.Net.Sockets Verbose: 0 : [2568] 00000040 : 52 3E 3D 0D 0A 3D 30
44-3D 30 41 7B 52 65 63 45 : R>=..=0D=0A{RecE
System.Net.Sockets Verbose: 0 : [2568] 00000050 : 6D 61 69 6C 7D 3C 42
52-3E 3D 30 44 3D 30 41 3C : mail}<BR>=0D=0A<
System.Net.Sockets Verbose: 0 : [2568] 00000060 : 2F 66 6F 6E 74 3E 3C
2F-74 64 3E 3C 2F 74 72 3E : /font></td></tr>
System.Net.Sockets Verbose: 0 : [2568] 00000070 : 3C 2F 74 61 62 6C 65
3E-3C 2F 62 6F 64 79 3E 3C : </table></body><
System.Net.Sockets Verbose: 0 : [2568] 00000080 : 2F 68 74 6D 6C 3E :
/html>
System.Net.Sockets Verbose: 0 : [2568] Exiting Socket#43667493::Send() ->
134#134
System.Net.Sockets Verbose: 0 : [2568] Socket#43667493::Send()
System.Net.Sockets Verbose: 0 : [2568] Data from Socket#43667493::Send
System.Net.Sockets Verbose: 0 : [2568] 00000000 : 0D 0A : ..
System.Net.Sockets Verbose: 0 : [2568] Exiting Socket#43667493::Send() ->
2#2
System.Net.Sockets Verbose: 0 : [2568] Socket#43667493::Send()
System.Net.Sockets Verbose: 0 : [2568] Data from Socket#43667493::Send
System.Net.Sockets Verbose: 0 : [2568] 00000000 : 0D 0A 2E 0D 0A : .....
System.Net.Sockets Verbose: 0 : [2568] Exiting Socket#43667493::Send() ->
5#5
System.Net.Sockets Verbose: 0 : [2568] Socket#43667493::Receive()
System.Net.Sockets Verbose: 0 : [2568] Data from Socket#43667493::Receive
System.Net.Sockets Verbose: 0 : [2568] 00000000 : 32 35 30 20 4D 61 69
6C-20 71 75 65 75 65 64 20 : 250 Mail queued
System.Net.Sockets Verbose: 0 : [2568] 00000010 : 66 6F 72 20 64 65 6C
69-76 65 72 79 2E 0D 0A : for delivery...
System.Net.Sockets Verbose: 0 : [2568] Exiting
Socket#43667493::Receive() -> 31#31
System.Net Verbose: 0 : [2568] Exiting SmtpClient#25675301::Send()
System.Net.Sockets Verbose: 0 : [5560] Socket#57792633::Dispose()
System.Net.Sockets Verbose: 0 : [5560] Socket#57792633::Shutdown(Both#2)
System.Transactions Critical: 0 : <TraceRecord
xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord"
Severity="Critical"><TraceIdentifier>http://msdn.microsoft.com/TraceCodes/System/ActivityTracing/2004/07/Reliability/Exception/Unhandled</TraceIdentifier><Description>Unhandled
exception</Description><AppDomain>LeadProcessingServiceConsole.vshost.exe</AppDomain><Exception><ExceptionType>System.ObjectDisposedException,
mscorlib, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089</ExceptionType><Message>Safe handle has
been closed</Message><StackTrace> at
System.Net.UnsafeNclNativeMethods.OSSOCK.shutdown(SafeCloseSocket
socketHandle, Int32 how)
at System.Net.Sockets.Socket.Shutdown(SocketShutdown how)
at
?.Finalize()</StackTrace><ExceptionString>System.ObjectDisposedException:
Safe handle has been closed
at System.Net.UnsafeNclNativeMethods.OSSOCK.shutdown(SafeCloseSocket
socketHandle, Int32 how)
at System.Net.Sockets.Socket.Shutdown(SocketShutdown how)
at ?.Finalize()</ExceptionString></Exception></TraceRecord>
System.Net Error: 0 : [5560] Exception in the
#62476613::UnhandledExceptionHandler - Safe handle has been closed
System.Net Error: 0 : [5560] at
System.Net.UnsafeNclNativeMethods.OSSOCK.shutdown(SafeCloseSocket
socketHandle, Int32 how)
at System.Net.Sockets.Socket.Shutdown(SocketShutdown how)
at ?.Finalize()
 
H

hazz

Oh, sorrry Paul. And doubly sorry this is vb.net. I am doing both C# and
vb.net projects. Hopefully the smtpClient code logic flow will be enough to
help out here.

Function Send1(ByVal strFrom As String, ByVal strTo As String, ByVal
strSubject As String, ByVal strBody As String, Optional ByVal Format As
String = "") As Boolean
Try
Using message As New MailMessage(strFrom , strTo , strSubject , strBody)
Dim smtpClientAs SmtpClient = New SmtpClient()
Dim fromAddress As MailAddress = New MailAddress(fromAddress1,
"Sender Name")
smtpClient.Host =
System.Configuration.ConfigurationManager.AppSettings("MxServer")
smtpClient.Port = 25
smtpClient.Send(message)
End Using
End Try
 
K

Kevin Spencer

I can only guess here. A lot of the SmtpClient class is a black box, but
I've used it successfully for a long time without this issue. The first
question that pops into my mind is whether or not the SMTP server requires
authentication. I see you're not passing any credentials, and have not
specified UseDefaultCredentials. Most SMTP servers do require authentication
of some sort. I'm not sure what this might have to do with the exception,
other than the possibility that another exception may be triggering it. Does
the exception contain an InnerException? Are you catching all exceptions?

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Hard work is a medication for which
there is no placebo.
 
H

hazz

Thank you for your questions Kevin. It sends email but the exception is
intermittent (the worse kind) so it isn't credentials.
The innerexception is null. It is sort of a black box. Is it a COM wrapper
or is it an all .net from the ground up system? I have been told to try
http://www.devmail.net/ by a colleague and see if that works.
Thanks again. -Greg
 
K

Kevin Spencer

Hi Greg,

Glad you got it sorted out. But I'm curious now as to how you came to that
conclusion, and where you got that information from? I haven't seen it in
any of the SDK documentation. In fact, there is precious little mention at
all of whether the SmtpClient class uses SMTP or ESMTP. To employ chunking,
it would have to be using ESMTP.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Hard work is a medication for which
there is no placebo.
 
H

hazz

Kevin,
Please excuse my really paint by number approach here. I still really don't
understand the underpinnings.

When I tried the 3rd party email utility out, I came up with an error that
was solved in a support group. It reminded me of some trace log data that I
noticed during the course of troubleshooting. Here is the support answer
which is specific to that product; all I can say is that after I added the
line of code they suggested,
(msg.SMTPServer.UseChunking = false; ) I was sending email without the
persistent random exceptions.

It seems you are using Microsoft SMTP Service which has a bug with
"chunking" (BDAT command) implementation.

Basically, devMail.Net tries to take advantage of the chunking feature
whenever it is available on the mail server to improve performance.
Microsoft SMTP Service claims that it supports chunking but it
sometimes fails when a BDAT command is sent.

To fix this, please add the following line before calling msg.Send()

msg.SMTPServer.UseChunking = false;


And your problem will be gone.

http://dev.devbiz.com/forums/721/ShowPost.aspx

My question would be at this point, where does this chunking take place, on
some remote server....and what is it? What does chunking accomplish?

Thanks for your help, support and questions.
-Greg
************************************************************
 
K

Kevin Spencer

The System.Net.SmtpClient class is an SMTP client, and does not employ any
internal SMTP server. The devMail.Net component which you apparently bought
does. So, that really has nothing to do with the problem you were
experiencing with the System.Net.SmtpClient. At any rate, glad you got
something working!

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Hard work is a medication for which
there is no placebo.
 
H

hazz

Yes, I just discovered there is yet another layer to the issue. Yes I did
get it working and yes, it appears the chunking had nothing to do with the
problem. My solution in VS2003 app is running and a VS2005 solution with the
same code does not.
Thanks Kevin for not allowing me to follow along with that illusion.
The discovery process continues.
Greg
 

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