-1

This is my code :

MailMessage mail = new MailMessage();
mail.From = new MailAddress(emailFrom);
mail.To.Add(new MailAddress(emailTo));
mail.IsBodyHtml = true;
mail.Subject = subject;
mail.Body = body.ToString();

System.Net.ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(RemoteServerCertificateValidationCallback);
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(body, Encoding.UTF8, "text/html");
mail.AlternateViews.Add(htmlView);
SmtpClient smtp = new SmtpClient();
smtp.Host = "mail.alpc.ir";
smtp.Port = 25;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential(emailFrom, emailFromPassword);
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(mail);

Email is sent successfully, but it's sent as plain text.

Received email :

p{margin-top: 0;margin-bottom: 0.5rem;}:root{--w1:600px;}:root{--d2:0.5rem;}:root{--fs:12px}@font-face {font-family: iransans;font-style: normal;font-weight: normal;src: url('https://sadidgostaran.ir/Content/IRANSansWeb.woff2') format('woff2');}@media only screen and (max-width:630px){:root{--w1:100%;}}@media only screen and (max-width:420px){:root{--fs:10px}.table-responsive{display: block;width: 100%;overflow-x: auto;-webkit-overflow-scrolling: touch;}}
 گیرنده گرامی  

werqwefsdvfasdf asfweqfwqr werwqreqwf werwqrqwefswadf qwerewrwerqwefsdvfasdf asfweqfwqr werwqreqwf werwqrqwefswadf qwerewrwerqwefsdvfasdf asfweqfwqr werwqreqwf werwqrqwefswadf qwerewrwerqwefsdvfasdf asfweqfwqr werwqreqwf werwqrqwefswadf qwerewrwerqwefsdvfasdf asfweqfwqr werwqreqwf werwqrqwefswadf qwerewrwerqwefsdvfasdf asfweqfwqr werwqreqwf werwqrqwefswadf qwerewrwerqwefsdvfasdf asfweqfwqr werwqreqwf werwqrqwefswadf qwerewr

 ما را در شبکه های اجتماعی دنبال کنید سدید گستران در گوگلمحمد مهدی نعمتیسمت شخصسدید گستران امین 021-54942 - 09123456789 info@sadidgostaran.ir www.sadidgostaran.ir میدان ولیعصر - ساختمان تجاری ایرانیان - طبقه 4 - واحد 5
Makyen
  • 31,849
  • 12
  • 86
  • 121
  • Well - the most obvious question is: what's the value of `ishtml` that you assign to the `mail.IsBodyHtml` property? – marc_s May 27 '23 at 05:47
  • whats the value of `ishtml` on `mail.IsBodyHtml = ishtml` is it have the correct value? that being said, even if you tell the client its html some may ignore it and show it as plain text for **security**. also, make sure to use **inlined css** and avoid custom fonts for maximum compatibility. – Bagus Tesa May 27 '23 at 05:47
  • oh, don't forget to provide the plain text version using [`AlternateView`](https://learn.microsoft.com/en-us/dotnet/api/system.net.mail.mailmessage?view=net-7.0) to avoid giving user gibberish html in case their client is refusing html. most of your problem will be on the client instead of the c# smtp API, fyi. – Bagus Tesa May 27 '23 at 05:51
  • ishtml in this code is true – meysam rahpeyma May 27 '23 at 06:01
  • Add that to your code and try it, and when it doesn't work, update your original question with the change to the code and he fact that even with it set to `true` it does not work. As written, it's not possible to see whether it's true or not. And honestly, other programmers aren't going to trust you ;) – Steve May 27 '23 at 06:11

1 Answers1

1

You are creating a MailMessage object and then you don't use it. In the code you posted, the sixth line is the last mention of that MailMessage object. The line where you actually send the email is here:

smtp.Send(emailFrom, emailTo, subject, body);

How is the IsBodyHtml property of that MailMessage supposed to affect that? You need to use the other overload of Send; the one that takes a MailMessage object as an argument:

smtp.Send(mail);
jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
  • i try this way but html email sent as plain text – meysam rahpeyma May 27 '23 at 08:41
  • @meysamrahpeyma, update your question with your new code and make sure that it includes an actual message body that you've tested and claim doesn't work. We need to be able to see exactly what you are. – jmcilhinney May 27 '23 at 10:25