Sending Emails (I'm sure you've seen this question before!)

NJDubois

Well-known member
Joined
May 15, 2008
Messages
84
Programming Experience
Beginner
This is really frustrating! I need to send emails from my visual basic.net 2008 project. I've gone through all of the google hits, and one strange thing got me. Why was every way of doing it different, and more, why didn't any of them work?

This code will run, and attempt to send the email but comes back failed.

VB.NET:
Dim Message As New MailMessage("njdubois@gmail.com", "njdubois@gmail.com", "A PROGRAM SENT EMAIL", "Does it work")
        Try
            Dim Smtp As New SmtpClient("smtp.google.com", 587)

            Smtp.Send(Message)
            MsgBox("Message was succesfully sent ")

        Catch ex As Exception
            MsgBox("Message Fail!")
        End Try

So I changed it to:

VB.NET:
        Dim Message As New MailMessage("njdubois@gmail.com", "njdubois@gmail.com", "A PROGRAM SENT EMAIL", "Does it work")
        Try
            Dim Smtp As New SmtpClient("smtp.google.com", 587)
            With Smtp
                .Credentials = New Net.NetworkCredential(user, pass)  ' USER and PASS are set to my gmail account.
                .UseDefaultCredentials = False
            End With

            Smtp.Send(Message)
            MsgBox("Message was succesfully sent ")

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

And now the message box comes back with "Failure sending message" Which is really helpful! .... lol!

How do I fix this? Its got to be an authentication problem, but I'm not sure how to address that. All the other samples I have worked with don't get past using smtp, IE the program wont run, errors...bang, boom...whats smtp it asks. This program runs, and tries! YAY!

Thanks for the assistance. And for reading this question...again... probably for the 100th time.

Nick
Hello World!
The Home of Why Not?
 
Thanks for the fast reply! Adding your line of code did not work!

VB.NET:
        Dim Message As New MailMessage("njdubois@gmail.com", "njdubois@gmail.com", "A PROGRAM SENT EMAIL", "Does it work")
        Try
            Dim Smtp As New SmtpClient("smtp.google.com", 587)
            With Smtp
                .Credentials = New Net.NetworkCredential("njdubois", "cat5ndog5")
                .UseDefaultCredentials = False
                .EnableSsl = True
            End With

            Smtp.Send(Message)
            MsgBox("Message was succesfully sent ")

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

Again, thanks for the fast reply!
 
I can't send from work to try it out but this is all you should need.

VB.NET:
        Dim smtp As New SmtpClient
        With smtp
            .Credentials = New Net.NetworkCredential("youraddress@gmail.com", "thepassword")
            .Port = 587
            .Host = "smtp.gmail.com"
            .EnableSsl = True
        End With

        Dim mm As New MailMessage() '...

        smtp.Send(mm)
 
VB.NET:
        Dim smtp As New SmtpClient
        With smtp
            .Credentials = New Net.NetworkCredential(username, password)
            .Port = 587
            .Host = "smtp.gmail.com"
            .EnableSsl = True
        End With

        Dim mm As New MailMessage("njdubois@gmail.com", "njdubois@gmail.com", "A PROGRAM SENT EMAIL", "Does it work")

        smtp.Send(mm)

Worked like a charm!!

Many thanks, and for all who find this post know that on 11/23/2010 I sent an email using the above code!
 
A big thank you here to the guys above The code above worked right away with the tweaking of my account details (note for the record that they are your google password and username and email address). I have spent 2 days trying to bypass the 'a program is trying to send an email' message associated with outlook.

As NJDubois says, its really frustrating that every example is different when you search the Google hits. Different or outdated, and this is like the 20th example on vbdotnet that I have come across and tried to get working. Trust google mail to do what Microsoft cant do simply with outlook (I tried dozens of example programs).
 
Back
Top