SMTP mail, Unable to connect

Loin75

Member
Joined
Jun 5, 2013
Messages
6
Programming Experience
1-3
Hi,

I am new to VB.net, and new to the forum - so hi to all...

I am starting from the beginning and learning the basics, and at the moment I am testing the feature of sending an email via the pressing of a button on a form. The app is simple and internal to my network, and would use my Outlook client and my Exchange server.

I have seen many examples on the web, but all of them generate errors.

The following seems to be the simplest, and generates only one error: {"Unable to connect to the remote server"}

VB.NET:
Imports System.Net.Mail


Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click


        Dim Mail As New MailMessage
        Mail.Subject = "test email"
        Mail.To.Add("me@me.com")
        Mail.From = New MailAddress("me@me.com")
        Mail.Body = "Test email"


        Dim SMTP As New SmtpClient("192.168.0.100")
        SMTP.EnableSsl = True
        SMTP.Credentials = New System.Net.NetworkCredential("myusername", "mypassword")
        SMTP.Port = "25"
        SMTP.Send(Mail)


        MessageBox.Show("Your message was successfully sent.")


    End Sub
End Class

Can anyone please guide me. I also think that I need to insert some references, which I haven't learnt yet - such as SMTPclient, or MailMessage. Do I need to "add" something?

Many thanks for all your patience and future help...

Loin
 
Try commenting out the SMPT.Credentials line in your code.
And you should wrap the SMTP.Send line in a Try/Catch block too.
 
Thanks Juggalo.. i don't understand the use of the Try catch function. Can you please give me an example for my context?

Cheers
 
Try
  SMTP.Send(Mail)
Catch ex As Exception
  MessageBox.Show(ex.Message)
End Try
 
Thanks. I had actually just tried that exact code shortly before you posted. I also commented out the credentials like you suggested.

The message box shows up and says "Failure sending email" - but I get no errors appear in the designer window, so cannot identify why it failed.
 
Change the ex.Message part to ex.ToString to see the full error message.
I suspect that credentials will need to be provided, in your code snippet it looks like you have a random username and password from the example site you got the code from, change it to an actual username and password for your exchange server.
 
I did use my own credentials... just didn't want to publish my username and password on the forum, hence the defaults..

I tried the change you suggested and it says "Unable to connect to remote server". It didn't respond after a period of time. Which is actually the same message I was getting before with other styles of code.

I am assuming that for internal (Exchange Server) use, this should be straight forward and just work? I don't understand what I am missing...

Thanks for your help..
 
Sending an email isn't something that's overly complicated, all you need is the smtp server address, which I'm assuming the one you posted here is correct, then if credentials are needed you create the NetworkCredentials which you already have. If it's not working it's probably because the Exchange server isn't configured correctly, but I'm not familiar with exchange servers.
 
I'm rather embarrassed to say that I got my server IP address wrong. It worked perfectly when I spotted this glaring mistake.

So your changes helped me, thank you very much. And sorry for being a numpty...
 
I'm rather embarrassed to say that I got my server IP address wrong. It worked perfectly when I spotted this glaring mistake.

So your changes helped me, thank you very much. And sorry for being a numpty...
Glad to hear you got it working
 
Back
Top