?
Solved

VB.net Windows Forms Failure Sending Mail SMTP Exception

Posted on 2014-04-14
2
Medium Priority
?
1,512 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 2000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

650 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