Mailkit is not sending proper html body
See original GitHub issueI am trying to send a simple <ul><li> something </l></ul>
html but any reciept is not showing proper html
here is my code
string MainMailTo = "";
//************
MimeMessage message = new MimeMessage();
//************
var SMTP = Configuration.GetSection("EmailConfiguration:SMTPServer");
var Port = Configuration.GetSection("EmailConfiguration:SMTPPort");
var MailSender = Configuration.GetSection("EmailConfiguration:FromEmail");
var MailPass = Configuration.GetSection("EmailConfiguration:Password");
var SSlEnabled = Configuration.GetSection("EmailConfiguration:IsSSL");
var MailUser = Configuration.GetSection("EmailConfiguration:MailUser");
//************
var MailTo = param.ToEmail;
//************
MailboxAddress from = new MailboxAddress("noreply", MailSender.Value);
message.From.Add(from);
//************
string[] mailToList = MainMailTo.Split(new String[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
foreach (var item in MailTo.Split(new String[] { ";" }, StringSplitOptions.RemoveEmptyEntries))
{
mailToList.Append(item);
}
MailboxAddress to = new MailboxAddress("User", MailTo);
message.To.Add(to);
//************
message.Subject = param.Subject;
//************
BodyBuilder bodyBuilder = new BodyBuilder();
bodyBuilder.HtmlBody = param.BodyHTML;
bodyBuilder.TextBody = "-";
if (param.FileIds?.Count > 0)
{
foreach (int id in param.FileIds)
{
var fileObj = dBContext.Documents.Where(x => x.DocumentId == id).FirstOrDefault();
if (fileObj != null)
{
var pathCombine = System.IO.Path.Combine(this.environment.WebRootPath, fileObj.Path);
bodyBuilder.Attachments.Add(pathCombine);
}
}
}
try
{
message.Body = bodyBuilder.ToMessageBody();
SmtpClient client = new SmtpClient();
client.Connect(SMTP.Value, Convert.ToInt32(Port.Value), Convert.ToBoolean(SSlEnabled.Value));
client.Authenticate(MailUser.Value, MailPass.Value);
//************
client.Send(message);
client.Disconnect(true);
client.Dispose();
return "true";
}
catch (Exception exp)
{
return exp.Message;
}
Issue Analytics
- State:
- Created 10 months ago
- Comments:9 (5 by maintainers)
Top Results From Across the Web
C# MailKit HTML email received as plain text
I am using the below code to send HTML email via MailKit : var message = new MimeMessage(); var bodyBuilder = new BodyBuilder();...
Read more >HTML not converted · Issue #640 · jstedfast/MailKit
While replying to mail using mailkit the mail body is not send properly, HTML content is not forwarded as original, but for GMAIL...
Read more >Common Errors When Sending Email With Mailkit
Common Errors When Sending Email With Mailkit · Default Ports · Incorrect SMTP Host · Incorrect SMTP Port · Forcing Non-SSL On An...
Read more >MailKit HTMLBody is rendered as plain-text! · Issue #447
I am using the below code to send the email : var message = new MimeMessage(); var bodyBuilder = new BodyBuilder(); bodyBuilder.HtmlBody =...
Read more >How to Send and Receive Emails in C# [2023 Tutorial]
Find out how to send plain text or HTML emails with attachments in C# using the built-in .NET SMTP library, MailKit library, ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
If you print
param.BodyHTML
to a text file (or to the Console), you’ll notice that the value already encodes<
as<
and>
as>
which is what is causing your problem.In other words, the
param.BodyHTML
string is already broken before you give it to MimeKit/MailKit.I need to know what the raw message looks like so I can see the MIME and MIME headers.
You can use
message.WriteTo("raw-mime.txt")
to see what it looks like