Solved

VB.net local machine IP Address

Posted on 2014-04-09
5
301 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 26

Expert Comment

by:MacroShadow
Comment Utility
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
Comment Utility
>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
Comment Utility
Hi MacroShadow
I get 'GetHostByName' is obsolete for the line
 System.Net.Dns.GetHostByName(strHostName).AddressList(0).ToString()
0
 
LVL 26

Accepted Solution

by:
MacroShadow earned 500 total points
Comment Utility
But it works...
0
 

Author Closing Comment

by:murbro
Comment Utility
Thanks
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
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

11 Experts available now in Live!

Get 1:1 Help Now