Using javascript inside an HTML Email?

I'm attempting to implement a copy URL function inside the body of an HTML email being sent by ASP.Net.

This is what I'm working with so far:

            Dim smtpClient As SmtpClient = New SmtpClient()
            Dim message As MailMessage = New MailMessage()
            Dim CopyPath As String = "onclick=javascript: window.clipboardData.setData(Text," & SavePath & ")"


            'Format email body, this is pretty self explanatory. Build one big string with HTML formatting and then pass it l8r
            Dim EmailBody As String = ""
            EmailBody &= "<html>"
            EmailBody &= "<body>"
            EmailBody &= "<head>"
            EmailBody &= "<span style=""font-size:14pt; font-face:arial; font-weight:bold"">"
            EmailBody &= "Workflow has saved a text file you can import into a LegalKey search."
            EmailBody &= "</span><br/><hr><br/>"
            EmailBody &= "<span style=""font-size:12pt; font-face:arial; font-weight:bold"">"
            EmailBody &= " To view this file <a href=" & SavePath & ">click here</a>"
            EmailBody &= "</span><br/><br/>"
            EmailBody &= "<span style=""font-size:12pt; font-face:arial; font-weight:bold"">"
            EmailBody &= " To copy the location of this file <a href=" & CopyPath & ">click here</a>"
            EmailBody &= "</span><br/><br/>"


I have messed around with a lot of different syntax options, but I either get errors or it just returns " & SavePath & "  into the clipboard.

Thanks.


LVL 8
ApexCoAsked:
Who is Participating?
 
Bob LearnedConnect With a Mentor Commented:
Yeah, I am not sure if that is going to work, but I am not 100% sure.

Bob
0
 
kraffayCommented:
Add a single quote around the href attribute:

EmailBody &= " To view this file <a href='" & SavePath & "'>click here</a>"
0
 
ApexCoAuthor Commented:
I'm sorry, I didn't specify where the problem was exactly. The viewing portion works fine, it's the attempt to copy the path to the clipboard that is giving me problems.

So this:

Dim CopyPath As String = "onclick=javascript: window.clipboardData.setData(Text," & SavePath & ")"

and this

EmailBody &= " To copy the location of this file <a href=" & CopyPath & ">click here</a>"



Thanks.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
ApexCoAuthor Commented:
You know, I just realized something.

This isn't opening in IE, it's opening in Outlook 2003. Is this even possible from inside of an email?

Thanks.
0
 
Bob LearnedCommented:
Are you setting BodyFormat to MailFormat.Html ?

Bob
0
 
ApexCoAuthor Commented:
Yes, I am.

'Body can be Html or text format, specify true if it is html message
            message.IsBodyHtml = True
0
 
Bob LearnedCommented:
Did you try this?

    message.BodyFormat = MailFormat.Html

Bob
0
 
ApexCoAuthor Commented:
No, but I just did.

It's telling me that message.bodyformat isn't part of 2.0

There is a message.BodyEncoding though, but nothing pertaining to HTML inside the bodyencoding.
0
 
ApexCoAuthor Commented:
FWIW, I have used this before to send HTML messages, without problems. It's just the whole javascript thing that has me hung up.
0
 
ApexCoAuthor Commented:
Man I wish you could edit...

This is as far as I have gotten with making the syntax as correct as I know how.

Dim CopyPath As String = """javascript:window.clipboardData.setData('Text','" & SavePath & "')"""

And now when I click on my link inside the email I get a "unspecified error".
0
 
Bob LearnedCommented:
Try:

String.Format("<script language='{0}'>window.clipboardData.setData('Text', '{1}'); </script> ", "JavaScript", SavePath)

Bob
0
 
ApexCoAuthor Commented:
Nope, but I see what you are trying to do with that, clever :) *makes note for future reference*

Now when you click on the link it opens a "locate link browser" window...

I'm wondering if the problem isn't the javascript, but how I'm trying to use it?

EmailBody &= " To copy the location of this file <a href=" & CopyPath & ">click here</a>"

That CopyPath variable represents the string.

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.