Solved

VB.net local machine IP Address

Posted on 2014-04-09
5
303 Views
Last Modified: 2014-04-14
Hi

I am trying to use the following code to send an email using System.Net.Mail
It asks for the local machine IP Address
How do I find this programmatically?

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

        Try

            '  Pass in the message information to a new MailMessage

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



            '   Create an SmtpClient to send the e-mail

            Dim mailClient As New SmtpClient("127.0.0.1")  '  = 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

Open in new window

0
Comment
Question by:murbro
  • 2
  • 2
5 Comments
 
LVL 27

Expert Comment

by:MacroShadow
ID: 39990787
Private Sub GetIPAddress()

    Dim strHostName As String
    Dim strIPAddress As String

    strHostName = System.Net.Dns.GetHostName()
    strIPAddress = System.Net.Dns.GetHostByName(strHostName).AddressList(0).ToString()

    MessageBox.Show ("Host Name: " & strHostName & "; IP Address: " & strIPAddress)

End Sub

Open in new window


http://stackoverflow.com/questions/2234757/how-do-i-get-a-computers-name-and-ip-address-using-vb-net
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39991208
>Dim mailClient As New SmtpClient("127.0.0.1")

This needs to be the IP address of your email server. Do you have an SMTP server running on the local machine? If not, do you have access to a server? If yes, use that. If not, use a public server like gmail.
0
 

Author Comment

by:murbro
ID: 39991370
Hi MacroShadow
I get 'GetHostByName' is obsolete for the line
 System.Net.Dns.GetHostByName(strHostName).AddressList(0).ToString()
0
 
LVL 27

Accepted Solution

by:
MacroShadow earned 500 total points
ID: 39993353
But it works...
0
 

Author Closing Comment

by:murbro
ID: 39998789
Thanks
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Put window Form inside tab page 10 33
Advice in Xamarin 21 79
how to wrap a merged cells of datagridview column in vs2010 (vb.net) 8 34
SQL LINE CONTINUATION ISSUE 12 33
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
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 …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

773 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