views:

268

answers:

2

I have an ASP.Net app that allows a user to write text into a Telerik RadEditor control and then send an email.

For some reason I'm sometimes getting strange characters showing up in the email that is generated.

For example if I put the word Test’s into the RadEditor box and send it... the email shows up with the text changed to: Test’s.

It seems as though the character was used in place of ' because if I use the later, the text would show up just fine. If I pull up the saved record within the ASP.Net apps interface it looks just fine. It also appears just fine when I view the text within the recorded of the MS SQL table it was stored in.

I'm using MailMessage to create the email. I’ve check the string being sent at the point just before I use SmtpClient to send the message and it looks just fine at his point. Once the email message shows up however I get the strange text (Test’s).

I’m guessing that I have an encode/decoding issue but I’m not sure how I would go about fixing this.

Anyone have any ideas?

Thank you!.

Continued--->

I have tried to add it to the constructor of my email class with/without mybase but that had no effect.

  Public Sub New(ByVal EmailDate As DateTime, ByVal LogoPath As String)
        MyBase.New()
        MyBase.BodyEncoding = Encoding.GetEncoding("iso-8859-1")

        ‘BodyEncoding = Encoding.GetEncoding("iso-8859-1")
        Me.EmailDate = EmailDate
        Me.LogoPath = LogoPath
    End Sub

I also tried adding it to the code behind of the form that calls the email class just before I create a new SmtpClient but that did not seem to be correct either.

Try
    returnEmail.BodyEncoding = Encoding.GetEncoding("iso-8859-1")
    Dim smtpCli As New SmtpClient
    smtpCli.Send(returnEmail)
Catch ex As Exception
    ScriptManager.RegisterStartupScript(Me, Me.GetType, "smtpError", "alert('There was an error sending the email:\n* " & ex.Message & "');", True)
End Try
+2  A: 

Hi,

The problem you are running into, is that you are using non ascii characters. That quote isn't a single quote (which you've already determined), it's usually a back quote, or some other character (most commonly used in MS Word environment). These are also usually found in the ISO-8859-1 characterset.

Try setting the BodyEncoding property on the MailMessage object.

For example: msg.BodyEncoding = Encoding.GetEncoding("iso-8859-1")

Cheers!

Dave

dave wanta
Thank you both very much for your responses. I added to my original post above because I did not see a way to include the example code in this comment field. Again, I’m sure it’s just something I’m doing wrong since I’m not sure I fully understand the encoding issue…any suggestions?Thanks!
dc
+1  A: 

Building on dave wanta's answer, you should either set the BodyEncoding property of the MailMessage to the encoding used by your control (might be System.Text.Encoding.Unicode), or convert your text to the encoding used by your MailMessage object using System.Text.Encoding.Convert

M.A. Hanin