Sending Email from VB.NET


I'm trying to set up a procedure in my program that will send me an email if the program encounters an error.

I found the code online, but can't get it to work.  I went to microsoft's site, but what appeared to be the relevant section, did not have examples, just c#, which I am less than efficient in.

I have these things set up at the begining of my program:
Imports System
Imports System.Net
Imports System.Net.Mail

But when i run the code I get an "SMTPException Occurred.  Failure sending mail"

This is the call i'm making to the procedure:
SendMail("", "", "Test Email", "This is a test!", True)

Any ideas where i might be going astray?

Thanks for your help!!!

Sub SendMail(ByVal StrMailfrom As String, ByVal StrMailTo As String, ByVal StrMailSubj As String, ByVal StrMailBody As String, ByVal FlagMailformat As Boolean)

        'Obtain the Network Credentials from the mailSettings section 
        Dim credential As New System.Net.NetworkCredential("", "mypassword")
        'Create the SMTP Client 
        Dim client As New SmtpClient()
        client.Host = ""
        client.DeliveryMethod = SmtpDeliveryMethod.Network
        'client.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis
        client.Credentials = credential
        client.Port = 366
        'Build Email Message 
        Dim email As New MailMessage
        email.From = New MailAddress(StrMailfrom)
        email.Subject = StrMailSubj
        email.IsBodyHtml = True
        email.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure

        email.Body = StrMailBody
        email.Sender = New MailAddress(StrMailfrom)
        'Send email 
        Catch e As Exception
        End Try

    End Sub

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Alfred A.Commented:

Try This,

Public Function SendEmail(ByVal StrMailfrom As String, ByVal StrMailTo As String, ByVal StrMailSubj As String, ByVal StrMailBody As String) As Boolean
        Dim Msg As New System.Net.Mail.MailMessage
        Dim FromAddress As New MailAddress(StrMailfrom)
        Dim ToAddress As New MailAddress(StrMailTo)

        Dim SMTPSrv As New SmtpClient("")

        Msg.From = FromAddress
        Msg.Subject = StrMailSubj
        Msg.Priority = System.Net.Mail.MailPriority.High
        Msg.Body = StrMailBody
        Msg.IsBodyHtml = True


        Catch ex As Exception
            ''SendMail Error Occured

            Return False
            SMTPSrv = Nothing
        End Try

        Return True
    End Function

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Alfred A.Commented:

Just additional comment from my previous post, you can remove line 5 (Dim ToAddress As New MailAddress(StrMailTo)
) in the code I posted.  It is not needed anymore.  :-)
slightlyoffAuthor Commented:
Outstanding!  Thank you :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.