Solved

VB.net Windows Forms Failure Sending Mail SMTP Exception

Posted on 2014-04-14
2
1,438 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:Murray Brown
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 40001538
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:Murray Brown
ID: 40006179
thanks for the help
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

691 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