Will
Will

Reputation: 1164

How to put a hyperlink into the email body using vb.net

What im trying to do is add a hyperlink to the body of an email in vb.net. What im getting when i send the email is the link is text. Here is what I doing so far below. Any help would be very much appreciated.

    'Accepts two parameters - the username and password for the email client
    Dim credentials As New System.Net.NetworkCredential("[email protected]", "test")

    smtpClient.Credentials = credentials
    Dim body, link As String

    link = "http://localhost:" & partUrl & "/test.aspx?autoNum=" & autoNum
    body = "Test email." & "<br/><br/>"

    body += link

    Dim email As New MailMessage
    email.From = New MailAddress("[email protected]")
    email.Body = body
    email.Subject = "test Change/Request Password"
    email.To.Add(New MailAddress(toAddress))

    smtpClient.Send(email)

Upvotes: 3

Views: 23710

Answers (4)

chrissie1
chrissie1

Reputation: 5029

You will need to enclose it in a tags.

link = "<a href=""http://localhost:" & partUrl & "/test.aspx?autoNum=" & autoNum & """>Click here</a>"

And you need to set

email.IsBodyHtml = true

Upvotes: 6

JHolyhead
JHolyhead

Reputation: 984

You haven't identified the body as HTML.

Add:

email.IsBodyHtml = true

Upvotes: 0

Rob P.
Rob P.

Reputation: 15091

I believe you need to set

IsBodyHtml = True

Then you can use plain HTML in the body of the e-mail. It's still up to the mail client to display it correctly. I've had a few cases where valid HTML that looked create in my browser was messy in my e-mail.

Upvotes: 0

Marco
Marco

Reputation: 57593

Try this:

link = "<a href=""http://localhost:" & partUrl & "/test.aspx?autoNum=" & autoNum & """>Link</a>"
body = "Test email." & "<br/><br/>"
body += link

The idea (I can't test it now, sorry) is you have to add not the url itself, but the HTML code used to create link.
Remember to set mail body to html with email.IsBodyHtml = True

Upvotes: 0

Related Questions