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?
 
Alfred A.Connect With a Mentor 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
 
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
All Courses

From novice to tech pro — start learning today.