Link to home
Start Free TrialLog in
Avatar of Murray Brown
Murray BrownFlag for United Kingdom of Great Britain and Northern Ireland

asked on

VB.net Windows Forms Failure Sending Mail SMTP Exception

I am getting the following error "Failure Sending Mail: SMTP Exception" in the following code

    Private Sub btnSendNetMail_Click(sender As System.Object, e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles btnSendNetMail.Click
        SendNetMail("murbro9@yahoo.com", "murbro9@yahoo.com", "Test Subject", "", "")
    End Sub


    Public Sub SendNetMail(ByVal MsgFrom As String, ByVal MsgTo As String, ByVal MsgSubject As String, ByVal MsgBody As String, ByVal oAttachment As String)

        Try

            '  Pass in the message information to a new MailMessage

            Dim msg As New Net.Mail.MailMessage(MsgFrom, MsgTo, MsgSubject, MsgBody)

            If oAttachment = "" Then
                'Do nothing
            Else
                msg.Attachments.Add(New System.Net.Mail.Attachment(oAttachment))
            End If



            ' Set the format of the mail message body as HTML
            msg.IsBodyHtml = True
            ' Set the priority of the mail message to normal
            msg.Priority = MailPriority.Normal

            '   Create an SmtpClient to send the e-mail

            ' Dim mailClient As New SmtpClient("105.236.251.36")  '  = local machine IP Address
            Dim mailClient As New SmtpClient(GetIPAddress)  '  = local machine IP Address

            '  Use the Windows credentials of the current User

            mailClient.UseDefaultCredentials = True



            ' Pass the message to the mail server

            mailClient.Send(msg)

            '  Optional user reassurance:

            'MsgBox(String.Format("Message Subject ' {0} ' successfully sent From {1} To {2}", MsgSubject, MsgFrom, MsgTo), "EMail", Windows.Forms.MessageBoxButtons.OK)

            '  Housekeeping

            msg.Dispose()

        Catch ex As FormatException

            MsgBox(ex.Message & " :Format Exception")

        Catch ex As SmtpException

            MsgBox(ex.Message & " :SMTP Exception")

        End Try
    End Sub

    Private Function GetIPAddress() As String
        Try
            Dim strHostName As String
            Dim strIPAddress As String

            strHostName = System.Net.Dns.GetHostName()
            strIPAddress = System.Net.Dns.GetHostByName(strHostName).AddressList(0).ToString()
            GetIPAddress = strIPAddress
        Catch
            MsgBox(Err.Description & " abc4")
        End Try

        '
    End Function
ASKER CERTIFIED SOLUTION
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Murray Brown

ASKER

thanks for the help