locked
Can SmtpClient be configured to work with a self signed certificate? RRS feed

  • Question

  • Hi,

    I'm attempting to relay an email through my company's emails server. The server uses SSL with a self signed certificate. When I attempt to send with SmtpClient I get an exception of:

    "Unable to read data from the transport connection: net_io_connectionclosed."

    I've tried hooking the ServicePointManager.ServerCertificateValidationCallback event but I don't get a callback.

    Regards,

    Aaron
    Thursday, January 28, 2010 11:05 AM

All replies

  • I'd have thought it should be ok.

    Looks like the remote end is just closing the connection when it gets something it doesn't like.  Can you enable System.Net tracing so that we can see what is going on.  See see http://msdn2.microsoft.com/en-us/library/ty48b824.aspx and/or http://blogs.msdn.com/dgorti/archive/2005/09/18/471003.aspx
    http://www.alanjmcf.me.uk/ Please follow-up in the newsgroup. If I help, mark the question answered
    Thursday, January 28, 2010 2:56 PM
  • The trace doesn't seem to give away too many hints.

    System.Net Information: 0 : [6584] Associating MailMessage#35320229 with Message#17653682
    System.Net Verbose: 0 : [6584] SmtpClient::.ctor(host=213.200.x.x, port=465)
    System.Net Information: 0 : [6584] Associating SmtpClient#42194754 with SmtpTransport#15688314
    System.Net Verbose: 0 : [6584] Exiting SmtpClient::.ctor()  -> SmtpClient#42194754
    System.Net Verbose: 0 : [6584] SmtpClient#42194754::Send(MailMessage#35320229)
    System.Net Information: 0 : [6584] SmtpClient#42194754::Send(DeliveryMethod=Network)
    System.Net Information: 0 : [6584] Associating SmtpClient#42194754 with MailMessage#35320229
    System.Net Information: 0 : [6584] Associating SmtpTransport#15688314 with SmtpConnection#52307948
    System.Net Information: 0 : [6584] Associating SmtpConnection#52307948 with ServicePoint#40535505
    System.Net.Sockets Verbose: 0 : [6584] Socket#34678979::Socket(InterNetwork#2)
    System.Net.Sockets Verbose: 0 : [6584] Exiting Socket#34678979::Socket()
    System.Net.Sockets Verbose: 0 : [6584] Socket#66166301::Socket(InterNetworkV6#23)
    System.Net.Sockets Verbose: 0 : [6584] Exiting Socket#66166301::Socket()
    System.Net.Sockets Verbose: 0 : [6584] Socket#34678979::Connect(186:465#-1168193276)
    System.Net.Sockets Verbose: 0 : [6584] Exiting Socket#34678979::Connect()
    System.Net.Sockets Verbose: 0 : [6584] Socket#66166301::Close()
    System.Net.Sockets Verbose: 0 : [6584] Socket#66166301::Dispose()
    System.Net.Sockets Verbose: 0 : [6584] Exiting Socket#66166301::Close()
    System.Net Information: 0 : [6584] Associating SmtpConnection#52307948 with SmtpPooledStream#39774547
    System.Net.Sockets Verbose: 0 : [6584] Socket#34678979::Receive()
    System.Net.Sockets Verbose: 0 : [6584] Data from Socket#34678979::Receive
    System.Net.Sockets Verbose: 0 : [6584] 00000000 :                                                 :
    System.Net.Sockets Verbose: 0 : [6584] Exiting Socket#34678979::Receive()  -> 0#0
    System.Net Error: 0 : [6584] Exception in the SmtpClient#42194754::Send - Unable to read data from the transport connection: net_io_connectionclosed.
    System.Net Error: 0 : [6584]    at System.Net.Mail.SmtpReplyReaderFactory.ProcessRead(Byte[] buffer, Int32 offset, Int32 read, Boolean readLine)
       at System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader caller, Boolean oneLine)
       at System.Net.Mail.SmtpReplyReaderFactory.ReadLine(SmtpReplyReader caller)
       at System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port)
       at System.Net.Mail.SmtpTransport.GetConnection(String host, Int32 port)
       at System.Net.Mail.SmtpClient.GetConnection()
       at System.Net.Mail.SmtpClient.Send(MailMessage message)
    System.Net.Sockets Verbose: 0 : [6584] Socket#34678979::Dispose()
    A first chance exception of type 'System.Net.Mail.SmtpException' occurred in System.dll
    System.Net Verbose: 0 : [6584] Exiting SmtpClient#42194754::Send()

    Regards,

    Aaron
    Thursday, January 28, 2010 10:11 PM
  • So it looks like the remote server closes the connection immediately.  (There doesn't seem to be any data sent as far as I can see).  Can you confirm that with a sniffer or similar.  Or turn on datetime/timestamp to see how long it waits.

    Also it seems that maybe SmtpClient doesn't support the port 465 form of SMTP+TLS.  Some posts report that it uses the "explict" form, that is connect to normal port 25 and configure secure mode there...  Do you know if your server supports that mode?
    http://www.alanjmcf.me.uk/ Please follow-up in the newsgroup. If I help, mark the question answered
    • Proposed as answer by azaclauson Saturday, February 6, 2010 11:49 AM
    Monday, February 1, 2010 4:58 PM
  • You're correct on the no data theory. The SYN-ACK sequence completes and then the client just sits there. 27 seconds later the client sends a FIN and the connection closes. There's no Client Hello being sent as part of the SSL initialisation so maybe it is that the SmtpClient doesn't like the use of port 465.

    I'd been told the mail server I need to use only supported relaying via port 465 but I tried via port 25 and it works and the same self signed SSL certificate is used so that's the answer to my issue, port 465 doesn't work with SmtpClient.
    Saturday, February 6, 2010 11:49 AM