question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Mailkit is not sending proper html body

See original GitHub issue

I am trying to send a simple <ul><li> something </l></ul> html but any reciept is not showing proper html image

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:closed
  • Created 10 months ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
jstedfastcommented, Nov 8, 2022

If you print param.BodyHTML to a text file (or to the Console), you’ll notice that the value already encodes < as &lt; and > as &gt; which is what is causing your problem.

In other words, the param.BodyHTML string is already broken before you give it to MimeKit/MailKit.

1reaction
jstedfastcommented, Nov 8, 2022

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found