.NET Framework Networking and Communication
The Trasnfer Encoding is different from the content type.
So for example
HTML Content Type -- text/html
You could transer on the wire the same Content type as
Quoted printable, base64, utf8 and ascii.
So the receiving side actually does two things.
Looks at the transfer encoding and does the reverse transfer encoding [decoding]
to get the entity.
Then it looks at the content type to decide what the best way to render it.
So once again the content type and the tranfer encoding are different and server two different puposes.
In your case the content type is text
The encoding being used is Quoted Printable.
You could set the MailMessage.BodyEncoding to Encoding.ASCII
and we should send this as ASCII
But then a few words of advise:
With System.Net.Mail we look at the body and kind of determine what the
best encoding to use. If your body has non ascii characters you should not
set the encoding as ASCII.
You should not really depend on preserving the body as it is on the wire.
On the receiving side you should look at the ContentTransferEncoding header
and then do the reverse encoding to get the body.
So in this case you should be really doing reverse QuotedPrintable Encoding to
get the body. It is not difficult to do this and there are plety of classes avaibale on the net.]
Thnaks for your response. I tried setting the body encoding to ASCII as in the below code fragment:
Message = New System.Net.Mail.MailMessage()
Message.From = New MailAddress(EmailString(From(), MessageFromEmailName))Message.Subject = MessageSubject
Message.Body = MessageBody
Message.IsBodyHtml = False
Message.BodyEncoding = System.Text.Encoding.ASCII
But it doesn't alter the results on the Pop server when the message is received. It's being changed to quoted-printable as below.
Content-type: text/plain; charset=us-ascii
PROCESS STRING: IMPORT FROM METEX=0D=0AExtension: NTZ=0D=0A=
I also tried the AlternateViews as suggested, but it made a bigger mess of the message.
The only recourse I see is to rewrite my code setting up a connection myself and using streamwriter to send the message components individually.
On reading your post one more time I realized that there are really two things involved.
1) The SMTP Server receving the message
2) The POP3 server sending you the message.
Just because you set the endoding while sending does not mean that the POP3 server is going to honor that. The POP3 server might decide to change the transfer encoding to fit its algorithm.
The right approach, once again, is to look at two things.
1) The Content Transfer Encoding and apply the revere encoding
2) Then Look at the content type and unterpret the data according to the
content type and the charset in the content type.
Seems all sevenbit bodyencoding specified will be defaulting to "Content-transfer-encoding: quoted-printable" as the mail header. And as http://msdn2.microsoft.com/en-us/library/system.net.mail.mailmessage.bodyencoding.aspx says, utf8 and some other eightbit bodyencoding will be defaulting to "Content-transfer-encoding: base64" in the header.
The problem is some mail server and client mailer can not recognize "quoted-printable", in result the mail gets garbled.
With framework 1.*, we can set something like message.Headers["content-transfer-encoding"] = "7bit" to specify the header to avoid "quoted-printable", but system.net.mail seems to set the "quoted-printable" inside without checking the code outside.
Is there a way to set "Content-transfer-encoding" in program or Is there a way to avoid the "quoted-printable" without setting bodyencoding a eightbit encoding (Ex. in the case of setting bodyencoding to "iso-2022-jp" which is a 7bit charset)
Some mail clients doesn't support Quoted Printable, so they need to change the transferencoding in program to 7bit, etc.
Seems the only way to set transferencoding is to use AlternateView.TransferEncoding property, but AlternateView.TransferEncoding = System.Net.Mime.TransferEncoding.SevenBit will generate a header like the following:
It is "SevenBit", not "7bit". "SevenBit" is not a defined value of RFC, and some mailer can not recognize it too, how to let "Content-Transfer-Encoding" header be "7bit"
I have the exact same problem now, and are looking for an solution. Did anyone find a solution to this
to make things worse, the quoted-printable implementation of MailMessage is totaly broken by allowing lines with more than 76 characters:
code:msg.IsBodyHtml = false;
content-type: text/plain; charset=iso-8859-1
mailmessage doesn't like veryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryvery=
correct would be:
content-type: text/plain; charset=iso-8859-1
mailmessage doesn't like veryveryveryveryveryveryveryveryveryveryveryveryve=
ryveryveryveryveryveryveryveryveryveryveryveryveryveryveryvery long strings
while most mail clients / servers do not care about the line width, some mail clients will mess up the body totaly (for example: MEMO Open Client)
Quoted Printable (aka QP) encoding is defined in the RFC 2045.
The current implementation has bugs which makes it non-rfc compliant (by a slight margin). This problem is mitigated by the fact that most mail servers are able to correctly parse these e-mails.
Currently FOLDs (e.g. the decision to break the encoded lines into lines <= 76 characters (exclusing the CRLF)) implementation is being revised.
This bug is known and being fixed for the next release.
Yesterday I read this post and I was surprised because I encountered your same problems. I also had to send a text mail to my RelayFax with single-line commands in the body. I solved using alternateview:
PublicShared Function InviaFaxStandard() As Integer Dim test As Integer = 0 Try 'create the mail message Dim mail As New MailMessage() 'set the addresses and specify a friendly 'from' name
mail.From =New MailAddress(myMittMail, myMittName)
mail.To.Add(New MailAddress(myDestMail, myDestName)) 'set the content
mail.Subject = mySubject'first we create the Plain Text part Dim plainView As AlternateView = AlternateView.CreateAlternateViewFromString(myCorpo, Encoding.GetEncoding("iso-8859-1"), "text/plain")
plainView.TransferEncoding = Mime.TransferEncoding.SevenBit 'VERY IMPORTANT
'add the views
mail.BodyEncoding = Encoding.GetEncoding("iso-8859-1") ' *******************************
test = 1Catch ex As Exception
test = 0End Try Return test End Function
In this way all stuff worked!!
I'm replaying you, with the hopeness to be useful for you.
Your (cosmetically modified) example did exactly what I wanted. Thanks!