Solved

VB.net Windows Forms Failure Sending Mail SMTP Exception

Posted on 2014-04-14
2
1,304 Views
Last Modified: 2014-04-17
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
0
Comment
Question by:murbro
2 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
Comment Utility
Do you have an SMTP installed and running on the machine where the code is executing? If not, you'll need one, or you'll need to point it at a server that does have one.
0
 

Author Closing Comment

by:murbro
Comment Utility
thanks for the help
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now