Sending mail message with unicode text

D

David Dvali

Hello.
I have a problem with sending Unicode text in mail message.
So what I do:

First of all I have some template file like this:
=================================
<html>
<head><title>Test Message</title></head>
<body>
<p>Hello {0}</p>
</body>
</html>
=================================

This text will include Unicode text too.
Late I want to load this text and replace "{0}" with some value and send it
by mail.
then I do following:
=================================
// Read message text from predefined file
System.IO.TextReader textReader = new System.IO.StreamReader(fileName);
text = textReader.ReadToEnd();
textReader.Close();

// Assign message
MailMessage mailMessage = new MailMessage();
mailMessage.From = "(e-mail address removed)";
mailMessage.To = "(e-mail address removed)";
mailMessage.Subject = "Test";
mailMessage.Body = String.Format(text, "some UNICODE text");
mailMessage.BodyFormat = MailFormat.Html;
mailMessage.BodyEncoding = Encoding.Unicode;

// Send message
SmtpMail.SmtpServer = Settings.GetValue("SmtpServer");
SmtpMail.Send(mailMessage);
=================================

Now about the problem:
when I got message sent by this code it looks something like this: (actually
this is an anothe mail)
=================================
< h t m l > < h e a d > < t i t l e > R e g i s t e r i n g p r o p e r t y
< / t i t l e > < / h e a d > < b o d y > < p > < b > T h a n k y o u f o r
r e g i s t e r i n g y o u r p r o p e r t y w i t h R i g h t M o v i e ..
g e < / b > < / p > < p > Y o u r r e g i s t r a t i o n n u m b e r i s 1
9 3 3 9 6 a n d y o u s h o u l d q u o t e t h i s o n a n y c o r r e s p
o n d e n c e .. < / p > < p > Y o u r p r o p e r t y w i l l b e d i s p l
a y e d o n t h e R i g h t M o v i e .. g e w e b s i t e u n t i l 0 3 ..
1 2 .. 2 0 0 5 .. Y o u m a y r e n e w o r u p d a t e y o u r r e g i s t
r a t i o n a t a n y t i m e b y c l i c k i n g o n < a h r e f = " h t t
p : / / 2 1 3 .. 1 5 7 .. 2 0 1 .. 2 3 4 / R i g h t M o v e / P r o p e r t
y R e n e w .. a s p x ? r e g n u m = 1 9 3 3 9 6 " > h t t p : / / 2 1 3
... 1 5 7 .. 2 0 1 .. 2 3 4 / R i g h t M o v e / P r o p e r t y R e n e w
... a s p x ? r e g n u m = 1 9 3 3 9 6 < / a > .. < / p > < p > T h i s s e
r v i c e i s p r o v i d e d F R E E O F C H A R G E , s o p l e a s e t e
l l e v e r y o n e a b o u t R i g h t M o v i e .. g e .. < / p > < p > <
u > < b > D e t a i l s o f P r o p e r t y : < / b > < / u > < / p > < p >
R o o m s : < / b > 4 ? ? ? ? ? < b r > < b > R e c e p t i o n r o o m s
: said:
9 < b r > < b > S t r e e t : < / b > ? ? ? 9 9 9 < b r > < b > D i s t r
i c t a n d T o w n : < / b > ? ? ? ? ? ? ? < b r > < b > C o n t a c t : <
/ b > 9 9 9 < b r > < b > T e l e p h o n e : < / b > 9 9 9 < b r > < b > F
a x : < / b > < b r > < b > M o b i l e : < / b > < b r > < b > E - m a i l
: < / b > < a h r e f = " m a i l t o : d a v i d @ d s l .. g e " > d a v i
d @ d s l .. g e < / a > < b r > < b > E s t a t e A g e n t : < / b > ? ? ?
< b > E n t r y t y p e : < / b > ? ? ? ? ? ? ? ? ? ? ? < / p > < p > R i
g h t M o v i e .. g e a c c e p t s n o r e s p o n s i b i l i t y f o r t
h e a c c u r a c y o r a v a i l a b i l i t y o f t h e i n f o r m a t i
o n d i s p l a y e d o n i t s w e b s i t e a n d a c c e p t s n o l i a
b i l i t y f o r l o s s o f e a r n i n g s o r l o s s o f c a p i t a l
g a i n s t h r o u g h u s e o f i t s s e r v i c e s .. < / p > < / b o d
y > < / h t m l >
=================================

When I don't use mailMessage.BodyEncoding = Encoding.Unicode; it send normal
mail but with "?" character instead Unicode symbols.

So how can I load from file then replace some values and send HTML format
Unicode text?

Thank you.
 
H

heybrakywacky

I think it's the way you're reading in the file. Try using the
StreamReader constructor that specifies the encoding of the file as
well. E.g.:

System.IO.TextReader textReader = new System.IO.StreamReader(fileName,
System.Text.Encoding.Unicode);

I think that will do the trick.

HTH,
Kevin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top