Sending Email from VB.NET

Howdy!

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 vb.net 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("sven@myemail.com", "steve@myemail.com", "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("sven@myserver.com", "mypassword")
        'Create the SMTP Client 
        Dim client As New SmtpClient()
        client.Host = "mail.myserver.com"
        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.To.Add(StrMailTo)
        email.Subject = StrMailSubj
        email.IsBodyHtml = True
        email.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure

        email.Body = StrMailBody
        email.Sender = New MailAddress(StrMailfrom)
        'Send email 
        Try
            client.Send(email)
        Catch e As Exception
            MsgBox(e.Message)
            Console.WriteLine(e.Message)
        End Try

    End Sub

Open in new window

LVL 1
slightlyoffAsked:
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:
Hi,

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("mail.myserver.com")

        Msg.From = FromAddress
        Msg.To.Add(StrMailTo)
        Msg.Subject = StrMailSubj
        Msg.Priority = System.Net.Mail.MailPriority.High
        Msg.Body = StrMailBody
        Msg.IsBodyHtml = True

        Try
            SMTPSrv.Send(Msg)

        Catch ex As Exception
            ''SendMail Error Occured

            Return False
        Finally
            Msg.Dispose()
            SMTPSrv = Nothing
        End Try

        Return True
    End Function

Open in new window

0

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:
Hi,

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.  :-)
0
slightlyoffAuthor Commented:
Outstanding!  Thank you :)
0
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.