StreamWriter, StringWriter and encoding...

G

Guest

Hello,

i have a big problem with my email component, because each time i write the
generated message directly to the spooler, the streamwriter will write in
Unicode.

The file starts with a FF FE and is double byte.

This is a problem because when a mail server like QMAIL receive the message,
will reject for malformed LF.

Infact, the file is Unicode and the normal CR/LF is (0D 00 0A 00) and QMAIL
reading 00 0A fails.

I tried everithing...

The code:

StringWriter SW = new StringWriter(new StringBuilder());
SW.WriteLine("x-sender: <" + this._From.Address + ">");
[...]
string message = SW.ToString();
using (StreamWriter sw = new StreamWriter(filename,true,Encoding.ASCII))
{
sw.Write(message);
}

Please help,
Regards
Stefano
 
J

Joerg Jooss

kinekine@digita said:
Hello,

i have a big problem with my email component, because each time i
write the generated message directly to the spooler, the streamwriter
will write in Unicode.

The file starts with a FF FE and is double byte.

This is a problem because when a mail server like QMAIL receive the
message, will reject for malformed LF.

Infact, the file is Unicode and the normal CR/LF is (0D 00 0A 00) and
QMAIL reading 00 0A fails.

I tried everithing...

The code:

StringWriter SW = new StringWriter(new StringBuilder());
SW.WriteLine("x-sender: <" + this._From.Address + ">");
[...]
string message = SW.ToString();
using (StreamWriter sw = new
StreamWriter(filename,true,Encoding.ASCII)) {
sw.Write(message);
}

Now I'm confused. This StreamWriter writes ASCII, which will drop any
non-7-bit character...
 
G

Guest

Already tried, with o without an encoding specified, will always save to
Unicode.

please note that the content is fully in ascii range.

Exists a function or a trick to save in single byte?

Stefano
 
J

Jon Skeet [C# MVP]

kinekine@digita said:
Already tried, with o without an encoding specified, will always save to
Unicode.

In that case your code is wrong, or you're misdiagnosing the problem.
With no encoding specified, it should be using UTF-8.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
G

Guest

Hello Jon,

i can't post the whole class to the newsgroup because is very big, and
nothing can be stripped out or the result will be different.

I will send you the class privately, please take a look.

Regards,

Stefano
 
J

Jon Skeet [C# MVP]

kinekine@digita said:
i can't post the whole class to the newsgroup because is very big, and
nothing can be stripped out or the result will be different.

I find that hard to believe, I'm afraid.
I will send you the class privately, please take a look.

I'll have a brief look, but if it's that big, it will probably take
longer than I've got time spare. As the person who understands the code
best, you're in the best position to cut it down (or start from scratch
to build up a short but complete program).
 

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

Similar Threads


Top