send plain/text email and getting =0D=0A in email

2019-06-19 00:50发布

问题:

When i send a email from my site to Uniform http://co.za registrar and cc myself in the mail i get an email returned from them that they received the email, but cannot find some of the information as it has those funny characters added to it. I saw that there was a previous post on the site but none of the answers seem to work for me, and i tried alot of post and blog on google.

The entire body of the email is constructed of the template you get from the registrar site and i used etc as the detail i need to populate and replace those, then send the body of the email off to the registrar.

EX: 
1a. Complete domain name: < domainname >
gets replaces with the domain name exmaple.co.za

My Code:

    MailMessage emailmsg = new MailMessage("myemail@domain.co.za", "coza-admin@co.za");
            emailmsg.Subject = "N domain domain.co.za";
            emailmsg.Bcc.Add("myemail@domain.co.za");
            emailmsg.Body = Body;
            emailmsg.IsBodyHtml = false;

            var plainView = AlternateView.CreateAlternateViewFromString(Body, new     ContentType("text/plain; charset=iso-8859-1"));
            plainView.TransferEncoding = TransferEncoding.SevenBit;
            emailmsg.AlternateViews.Add(plainView);

            SmtpClient sSmtp = new SmtpClient();
            sSmtp.Send(emailmsg);

This code send a bcc copy to myself and from the looks of it through outlook all seems fine and i should be but when they receive it the characters gets added. it seems to randomly add those characters. then obviously the request fails as the seconds name server cannot be found.

Returned Email:

Administrative Info
Contact:   Administrator=0D=0A 4b. Title/posi=   (Administrator)
Company:   CompanyName Ltd
Postal:    P.O Box 12841, Centrahill, 6006
Phone:     +27.00000000=0D=0A  4f. Fax Number: +27.00000000=

Provided Nameserver information
Primary Server  : ns1.nameserver.co.za 
Secondary 1     : ns2.nameserver.co.za=0d=0a,

Any help would be appreciated.

note: the registrar requires the the email be in plain text and not base64 or html whatsoever as their system picks up plain/text email and faxes and automatically processes them.

Thanks

回答1:

I figured it out, changed my code to the following:

MailMessage emailmsg = new MailMessage("from@address.co.za", "to@address.co.za")
emailmsg.Subject = "Subject";
emailmsg.IsBodyHtml = false;
emailmsg.ReplyToList.Add("from@address.co.za");
emailmsg.BodyEncoding = System.Text.Encoding.UTF8;
emailmsg.HeadersEncoding = System.Text.Encoding.UTF8;
emailmsg.SubjectEncoding = System.Text.Encoding.UTF8;
emailmsg.Body = null;

var plainView = AlternateView.CreateAlternateViewFromString(EmailBody, emailmsg.BodyEncoding, "text/plain");
plainView.TransferEncoding = TransferEncoding.SevenBit;
emailmsg.AlternateViews.Add(plainView);

SmtpClient sSmtp = new SmtpClient();
sSmtp.Send(emailmsg);

All characters spaces are kept like they should be in the email template. Hope this will help someone aswell.



回答2:

=OD=OA is quoted-printable representation of CrLf.

If you want to use the iso-8859-1 characterset, then you will want to get rid of this line:

plainView.TransferEncoding = TransferEncoding.SevenBit;

If you insist on using 7 bit encoding, then you can only use ASCII characters in your email, so change the is0-8859-1 characterset to "us-ascii"