jkrakowsky

How can I set the Content-Type-Encoding  in a System.Net.Mail.MailMessage


Re: .NET Framework Networking and Communication set Content-Type-Encoding in System.Net.Mail,MailMessage

Durgaprasad Gorti

There is no Content-Type property on the mail message.
You could use AlternateViews. Create an AlternateView and then
set the content type on the Alternate View.

If you are sending HTML, you could use IsBodyHtml property without having to set the content type.

Please tell me exactly what your scenario is




Re: .NET Framework Networking and Communication set Content-Type-Encoding in System.Net.Mail,MailMessage

jkrakowsky

I need to create a text message where the body is parsed line by line by the receiving application to pick up processing information for an attachment. The problem is that when I send out the message, the body seems to be defaulting to "Content-transfer-encoding: quoted-printable".

This is combining a multi-line body into a single line with the crLf's embedded within the text.

I'll check into creating HTML with an AlternateView as plain text.

Thanks.




Re: .NET Framework Networking and Communication set Content-Type-Encoding in System.Net.Mail,MailMessage

Durgaprasad Gorti

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.]

 

 






Re: .NET Framework Networking and Communication set Content-Type-Encoding in System.Net.Mail,MailMessage

jkrakowsky

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
Content-transfer-encoding: quoted-printable

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.






Re: .NET Framework Networking and Communication set Content-Type-Encoding in System.Net.Mail,MailMessage

Durgaprasad Gorti

No Your approach is not correct.
I said in my earlier post that you should not depend on a particular
Transfer-Encoding be used. Quoted printable is being used probably because
that is the default transfer encoding.

You should do the reverse encodin on the receiving side.
There are already tons of code samples to do this.
I don't understand why you have to resort to sending data yourself






Re: .NET Framework Networking and Communication set Content-Type-Encoding in System.Net.Mail,MailMessage

Durgaprasad Gorti

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.






Re: .NET Framework Networking and Communication set Content-Type-Encoding in System.Net.Mail,MailMessage

Ryan Lee

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)





Re: .NET Framework Networking and Communication set Content-Type-Encoding in System.Net.Mail,MailMessage

Durgaprasad Gorti

It is very hard to believe that there is an SMTP server out there that does not support Quoted Printable. Are you sure




Re: .NET Framework Networking and Communication set Content-Type-Encoding in System.Net.Mail,MailMessage

Ryan Lee

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:

"Content-Transfer-Encoding:SevenBit"

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"





Re: .NET Framework Networking and Communication set Content-Type-Encoding in System.Net.Mail,MailMessage

objecta

Hi

I have the exact same problem now, and are looking for an solution. Did anyone find a solution to this

Objecta






Re: .NET Framework Networking and Communication set Content-Type-Encoding in System.Net.Mail,MailMessage

heldchen

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;
msg.Body = "mailmessage doesn't like veryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryvery long strings";

result:

content-type: text/plain; charset=iso-8859-1
content-transfer-encoding: quoted-printable

mailmessage doesn't like veryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryvery=
long strings

correct would be:

content-type: text/plain; charset=iso-8859-1
content-transfer-encoding: quoted-printable

mailmessage doesn't like veryveryveryveryveryveryveryveryveryveryveryveryve=
ryveryveryveryveryveryveryveryveryveryveryveryveryveryveryvery long strings

reported as bug:
http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx FeedbackID=156052

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)





Re: .NET Framework Networking and Communication set Content-Type-Encoding in System.Net.Mail,MailMessage

Savas Guven

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.






Re: .NET Framework Networking and Communication set Content-Type-Encoding in System.Net.Mail,MailMessage

Silvia Elena

Hi!

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:

Public Shared 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.AlternateViews.Add(plainView)

mail.BodyEncoding = Encoding.GetEncoding("iso-8859-1")

' *******************************

PickupDirectory(mail)

test = 1

Catch ex As Exception

test = 0

End Try

Return test

End Function

In this way all stuff worked!!

I'm replaying you, with the hopeness to be useful for you.

Cheers,

Silvia






Re: .NET Framework Networking and Communication set Content-Type-Encoding in System.Net.Mail,MailMessage

RHodnett

Silvia,

Your (cosmetically modified) example did exactly what I wanted. Thanks!

Randy