Click here to Skip to main content
15,065,324 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am using System.Net.Mail to send an email from a ASP.Net program. I am inserting 2 '&nbsp's after each period in my text. It works as it is supposed to in my testing environment, but the actual email being sent to a gmail account shows 2 '&nbsp's instead of 2 spaces.

My Code looks like this:
XML
strBody = "Forms Accepted</p></td></tr>" & _
"<tr><td>Your forms have been accepted." & "&nbsp" & "&nbsp" & "Please log in to your account " & _
"<a style='color:#7ABB00' href='" & url & "'>here</a> " & _
"to complete your audit. "


VB
With objEmail
	.From = New MailAddress(pstrSystemEmailAddress)
        .Body = strBody
        .IsBodyHtml = True
        Dim content As AlternateView
        Email = SetImage(strBody, Nothing, "text/html", content)
        .BodyEncoding = Encoding.UTF8
        .Bcc.Add("testing@gmail.com")
        .Priority = MailPriority.Normal
End With


And the result in the email (gmail) looks like this:

Your forms have been accepted.&nbsp&nbspPlease log in to your account here to complete your audit.

Is there any setting missing from my code, or should I use something different?

Thanks in advance.
Posted
Updated 15-Nov-12 12:18pm
v3
Comments
Sergey Alexandrovich Kryukov 15-Nov-12 18:16pm
   
For your information, your &nbsp; is not shown properly in the list of the questions.
OK, I changed it somehow, to me more clear. I was showing "Displays instead of a space" :-)
--SA
Meg Takahashi 16-Nov-12 14:21pm
   
Hi! Thanks for editing my question. It looks better!

First problem I can see is the wrong syntax of HTML character entity: you miss ';' at the end. Now, if you need to show the HTML source of the entity (for example, in HTML code sample), you also need to escape '&'. Like this:
HTML
<p>
If you want to use HTML entity in code, use &nbsp; will be rendered as a non-breakable space
</p>
<p>
If you want to show the HTML source code in HTML, use &amp;nbsp; will be rendered as &nbsp;
</p>


Are you getting the idea? And, of course, to show it on this page, I had to double escape the ampersand. :-)

—SA
   
v2
Comments
Espen Harlinn 15-Nov-12 19:37pm
   
Good catch :-D
Sergey Alexandrovich Kryukov 15-Nov-12 22:03pm
   
Thank you, Espen.
--SA
Meg Takahashi 16-Nov-12 14:22pm
   
Thanks SA! I will try your suggestion and update the post later!
Sergey Alexandrovich Kryukov 16-Nov-12 14:50pm
   
You are welcome. When you see you can sort it out, please accept the answer formally (green button) -- thank.
If something's wrong, don't hesitate to ask follow-up questions...
--SA
Meg Takahashi 20-Nov-12 10:27am
   
Hi SA! I tried your solution and it worked! Thanks a lot!!!
Sergey Alexandrovich Kryukov 20-Nov-12 11:19am
   
You are welcome. It's a pleasure to help someone who actually can get it and make things working.
Good like, call again.
--SA
Switch the body encoding to ASCII instead of UFT8
   
Comments
Sergey Alexandrovich Kryukov 15-Nov-12 18:20pm
   
Totally wrong answer, sorry! Do you have an idea how HTML entities and encoding work? Probably not. UTF always works, anyway. And encoding of the source HTML is totally irrelevant to character entities.
--SA
Meg Takahashi 16-Nov-12 14:23pm
   
I tried this before I posted the question, but it didn't work. Thanks anyways!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900