-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

[email protected]

www.sadidgostaran.ir

میدان ولیعصر - ساختمان تجاری ایرانیان - طبقه 4 - واحد 5

5
  • Well - the most obvious question is: what's the value of ishtml that you assign to the mail.IsBodyHtml property? Commented May 27, 2023 at 5: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. Commented May 27, 2023 at 5:47
  • oh, don't forget to provide the plain text version using AlternateView 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. Commented May 27, 2023 at 5:51
  • ishtml in this code is true Commented May 27, 2023 at 6: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 ;) Commented May 27, 2023 at 6:11

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

2 Comments

i try this way but html email sent as plain text
@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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.