How To Send Plaintext Email (System.Net.Mail.MailMessage)?

  • Thread starter Thread starter Jeffrey Walton
  • Start date Start date
J

Jeffrey Walton

Hi All,

How does one send a plain text email using System.Net.Mail?

In the code below, I am *not* able to stop the MIME encoding. When I
examine the received message, the string is "Test 1=0D=0ATest 2". I
verified the message *was* MIME encoded when leaving my machine using
Wireshark.

Thanks,
Jeff

Dim mail As New MailMessage()
mail.BodyEncoding = Text.Encoding.ASCII
mail.SubjectEncoding = Text.Encoding.ASCII
mail.IsBodyHtml = False

'set the addresses
mail.From = New MailAddress("(e-mail address removed)")
mail.ReplyTo = New MailAddress("(e-mail address removed)")
mail.To.Add("(e-mail address removed)")

'set the subject and body
mail.Subject = "Test"
mail.Body = "Test 1" & vbCrLf & "Test 2"

'send the message
Dim smtp As New SmtpClient()
smtp.Send(mail)
 
Jeffrey said:
Hi All,

How does one send a plain text email using System.Net.Mail?

In the code below, I am *not* able to stop the MIME encoding. When I
examine the received message, the string is "Test 1=0D=0ATest 2". I
verified the message *was* MIME encoded when leaving my machine using
Wireshark.

Thanks,
Jeff

Dim mail As New MailMessage()
mail.BodyEncoding = Text.Encoding.ASCII
mail.SubjectEncoding = Text.Encoding.ASCII
mail.IsBodyHtml = False

'set the addresses
mail.From = New MailAddress("(e-mail address removed)")
mail.ReplyTo = New MailAddress("(e-mail address removed)")
mail.To.Add("(e-mail address removed)")

'set the subject and body
mail.Subject = "Test"
mail.Body = "Test 1"& vbCrLf& "Test 2"

'send the message
Dim smtp As New SmtpClient()
smtp.Send(mail)


Your code is not very different from this example:
http://www.systemnetmail.com/faq/3.1.1.aspx

HTH
 
Hi Jason,

Jeffrey said:
How does one send a plain text email using System.Net.Mail?

[SNIP]

Your code is not very different from this example:http://www.systemnetmail.com/faq/3.1.1.aspx
Yes - its looks like nearly every 'plain text' email sample I've seen.
Unfortunately, .Net sneaks a MIME encoding in, so I believe nearly all
the examples of 'plain text' email are wrong.

Any ideas how to turn off the hidden feature?

Jeff
 
"Jeffrey Walton" <[email protected]> a écrit dans le message de groupe
de discussion :
(e-mail address removed)...
Hi All,

How does one send a plain text email using System.Net.Mail?

In the code below, I am *not* able to stop the MIME encoding. When I
examine the received message, the string is "Test 1=0D=0ATest 2". I
verified the message *was* MIME encoded when leaving my machine using
Wireshark.

I don't think it is a MIME issue but just transfer encoding that is set
to Quoted Printable by default.
Use an alternate view for which you can specify the transfer encoding
property.
Try this simple example :

Dim m As New MailMessage
m.To.Add(New MailAddress("#####@###.##"))
m.From = New MailAddress("#####@###.##")
m.Subject = "transfer encoding"
Dim plainText As AlternateView =
AlternateView.CreateAlternateViewFromString("seven bits transfer
encoding test", Encoding.ASCII, "text/plain")
plainText.TransferEncoding = Net.Mime.TransferEncoding.SevenBit
m.AlternateViews.Add(plainText)
 
Hi Fred,
I don't think it is a MIME issue but just transfer encoding that is set
to Quoted Printable by default.
Use an alternate view for which you can specify the transfer encoding
property.
Try this simple example :

        Dim m As New MailMessage
        m.To.Add(New MailAddress("#####@###.##"))
        m.From = New MailAddress("#####@###.##")
        m.Subject = "transfer encoding"
        Dim plainText As AlternateView =
AlternateView.CreateAlternateViewFromString("seven bits transfer
encoding test", Encoding.ASCII, "text/plain")
        plainText.TransferEncoding = Net.Mime.TransferEncoding.SevenBit
        m.AlternateViews.Add(plainText)
That was it. According to the headers (and inspection of the sent/
received body), 'Content-Transfer-Encoding: quoted-printable' was
changed to 'Content-Transfer-Encoding: 7bit' and those damn =0D=0A
disappeared.

Its too bad Microsoft did not offer TransferEncoding directly off of
MailMessage or SmtpClient. I don't see how adding alternate views to
get a plain text email is intuitive. Heck, I would settle for
'mail.BodyEncoding = Text.Encoding.ASCII7' or 'mail.BodyEncoding =
Text.Encoding.NVT_ASCII'.

Thanks,
Jeff
 
Back
Top