Problem sending emails using SmtpClient in C #

I have an ASP.Net/MVC application and I'm trying to send HTML emails. I do this by reading in the HTML file with tokens and then replacing the tokens. This part is beautiful and generates the HTML that I want, but when I send an email, what I get looks like

<style type=3D"text/css">= =0D=0A.styleTitles=0D=0A{=0D=0Afont-weight:=bold;=0D=0A}=0D=0A .style1=0D=0A {=0D=0A 

and should look like

  <style type="text/css"> .styleTitles { font-weight: bold; } .style1 { height: 15px; } 

I looked on the Internet and I cannot find the correct syntax for sending a message. I saw some solutions, but no one works.

My current test code is

 SmtpClient smtpclient = new SmtpClient(); MailMessage message = new MailMessage(); MailAddress SendFrom = new MailAddress(" xxxx@abc.com "); MailAddress SendTo = new MailAddress(" zzzz@gmail.com "); MailMessage MyMessage = new MailMessage(SendFrom, SendTo); var plainView = AlternateView.CreateAlternateViewFromString(msgBody,null,"text/html"); plainView.TransferEncoding = System.Net.Mime.TransferEncoding.SevenBit; MyMessage.AlternateViews.Add(plainView); MyMessage.IsBodyHtml = true; MyMessage.Subject = subjectLine; MyMessage.Body = msgBody; smtpclient.Send(MyMessage); 

Any suggestions?

+4
source share
6 answers

Maybe something like this:

 var plainView = AlternateView.CreateAlternateViewFromString(msgBody, new ContentType("text/plain; charset=UTF-8")); MyMessage.AlternateViews.Add(plainView); MyMessage.BodyEncoding = Encoding.UTF8; MyMessage.IsBodyHtml = true; MyMessage.Subject = subjectLine; MyMessage.Body = msgBody; 
+6
source

Try this change:

 plainView.TransferEncoding = System.Net.Mime.TransferEncoding.Base64; 
+3
source

To set the transmission encoding to 8 bits, taken from here , you need:

 message.Body = null; using (AlternateView body = AlternateView.CreateAlternateViewFromString( "Some Message Body", message.BodyEncoding, message.IsBodyHtml ? "text/html" : null)) { body.TransferEncoding = TransferEncoding.SevenBit; message.AlternateViews.Add(body); } 
+3
source

This may not be the answer you need, but have you considered using XSLT to translate your email messages? I am busy with a project that sends emails, and it's pretty nice to use XSLT as part of the solution. Also means that in the future the template can be easily customized in a standardized, standardized way, maybe you should consider changing it?

0
source

Strange, but a simpler code works for me:

 var message = new MailMessage(Email, mailTo); message.IsBodyHtml = true; message.SubjectEncoding = message.BodyEncoding = Encoding.UTF8; message.Subject = "Subject"; message.Body = msgBody; smtpclient.Send(message); 
0
source
 string emailMessage="a skjdhak kdkand"; MailMessage mail = new MailMessage(); mail.To.Add(obj_Artist.EmailAddress); mail.From = new MailAddress(EmailList[0].FromEmail, "Sentric Music - Rights Management"); mail.Subject = (EmailList[0].Subject); if (EmailList[0].BCC1 != null && EmailList[0].BCC1 != string.Empty) { mail.Bcc.Add(EmailList[0].BCC1); } if (EmailList[0].BCC2 != null && EmailList[0].BCC2 != string.Empty) { mail.Bcc.Add(EmailList[0].BCC2); } if (EmailList[0].CC1 != null && EmailList[0].CC1 != string.Empty) { mail.CC.Add(EmailList[0].CC1); } if (EmailList[0].CC2 != null && EmailList[0].CC2 != string.Empty) { mail.CC.Add(EmailList[0].CC2);`enter code here` } string Body = emailMessage; mail.Body = Body; mail.BodyEncoding = System.Text.Encoding.GetEncoding("utf-8"); mail.IsBodyHtml = true; AlternateView plainView = AlternateView.CreateAlternateViewFromString (System.Text.RegularExpressions.Regex.Replace(Body, @"<(.|\n)*?>", string.Empty), null, "text/plain"); System.Net.Mail.AlternateView htmlView = System.Net.Mail.AlternateView.CreateAlternateViewFromString(Body, null, "text/html"); mail.AlternateViews.Add(plainView); mail.AlternateViews.Add(htmlView); SmtpClient smtp = new SmtpClient(); smtp.EnableSsl = true; smtp.Send(mail); 
-one
source

All Articles