1

Pretty new to programming here.

The program I'm currently working on needs to send an email with logs. Working great if I'm using the right server host but when I'm trying with a "false" server host my program sure can't connect, but it immediatly crash, I can't raise any exception, can't tell the user he's doing something wrong, nothing.

So I guess I have to test the connection before SMTPClient.Send but I can't seem to find how...

How can I test a SMTP Server connection in VB.NET ?

That's what I'm using :

    Try
        Dim SmtpServer As New SmtpClient()
        With SmtpServer
            .EnableSsl = False
            .UseDefaultCredentials = False
            .Credentials = New Net.NetworkCredential(MailUser, MailPassword)
            .Port = 25
            .Host = ServerAdress
        End With

        Dim mail As New MailMessage()
        With mail
            .From = New MailAddress(MailSender)
            .To.Add(MailReceiver)
            .CC.Add(MailCC)
            .Subject = MailObject
            .Body = MailBody
        End With

        SmtpServer.Send(mail)

    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
Muce
  • 11
  • 4
  • There might be a better option, but the most simple way would be to wrap it in a [`Try/Catch` statement](https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/try-catch-finally-statement) and send a test e-mail. – Visual Vincent Dec 12 '18 at 16:10
  • Just edited to add what I'm currently using. Because it's wrapped in a Try/Catch statement and still instantly crash. – Muce Dec 12 '18 at 16:13
  • Does it crash when running it live or just while running it in Visual Studio? Because in VS you can enable certain exceptions to always break the debugger. – Visual Vincent Dec 12 '18 at 16:18
  • In VS so yeah that might be it. I guess we can disable it ? – Muce Dec 12 '18 at 16:20
  • Of course! :) - See: [Managing exceptions with the debugger](https://learn.microsoft.com/en-us/visualstudio/debugger/managing-exceptions-with-the-debugger) - Just untick all that you don't want breaking execution _**if**_ they're already handled by a `Try/Catch`. – Visual Vincent Dec 12 '18 at 16:21
  • I can't seem to find something like this in Visual Basic 2010 Express (what I'm using) but thanks a lot I guess that's the issue. – Muce Dec 12 '18 at 16:41
  • Oh, well in that case just go to `Debug > Exceptions` and untick `Thrown` for `Common Language Runtime Exceptions` (or just press `Reset all`). For more info: https://stackoverflow.com/a/23087989 – Visual Vincent Dec 12 '18 at 16:46
  • I thought I was one of the last people to still use VS 2010 ;). Well, _clearly_ they must've done something wrong with the newer versions, seeing as 2010 users still show up every now and then. – Visual Vincent Dec 12 '18 at 16:48
  • I'm confused, is Visual BASIC 2010 Express the same thing as Visual STUDIO 2010 or what ? In what I'm using I can't seem to find any exceptions settings... – Muce Dec 12 '18 at 16:53
  • Most of it should be the same, yes. Visual Basic Express is just a slightly stripped down version of Visual Studio with only VB.NET as the supported programming language (VS additionally supports C#, F#, C++, plus a couple of other technologies). Try using the shortcut key combination `CTRL + ALT + E` to open it. **EDIT:** Apparently the menu item should exist, but for some reason doesn't. The key combination appears to work, though. See the answers to the following question: https://stackoverflow.com/q/24775293 – Visual Vincent Dec 12 '18 at 17:34
  • Thanks a lot ! Managed to open the exception menu but ticking or unticking Thrown Common Language Runtime Exceptions doesn't change anything same for System.Net.Mail. Still instantly crash even if I'm running it live. – Muce Dec 13 '18 at 08:59
  • But... That can't be?! Unless it's a fatal exception like the `AccessViolationException`, this shouldn't be possible. What happens if you try rebuilding your project (`Build` menu > `Rebuild `)? Make sure you have no errors in it. – Visual Vincent Dec 13 '18 at 20:37
  • One month later, almost everything on my program is working except this, I tried rebuilding it, tried with OnError GoTo, Try/Catch... but it still either works if I can connect to mail server ou instantly crash if I can't – Muce Jan 07 '19 at 14:09
  • Odd behaviour indeed... It should not even be possible. The last thing I can suggest is that your repair your .NET Framework installation. Also try it on a different PC. – Visual Vincent Jan 07 '19 at 15:04

1 Answers1

0

1- I think you should try something like this:

Using tcp As New TcpClient
    Try
      tcp.Connect(ip, 25)
      ' server found
    Catch ex As Exception
      ' server not found
      End Try
    tcp.Close()
  End Using ' tcpclient

2- For test your smtp server this article can be usefull: https://www.port25.com/how-to-check-an-smtp-connection-with-a-manual-telnet-session-2/

3- and also there is a smtp test class in this question's answers .Net TcpClient and SmtpClient won't connect to my Smtp server

Hej.Ag
  • 39
  • 5