I am using the Microsoft.Office.Interop.Outlook.Application to generate an email and display it on the screen before the user can send it. The application is a
also I have dealt with this topic for several hours. Finally I stumbled across a very interesting Microsoft support case.
https://support.microsoft.com/de-de/help/4020759/text-formatting-may-be-lost-when-editing-the-htmlbody-property-of-an
The real problem is buried somewhere else: Microsoft Outlook uses Microsoft Word as the editor. Loss of formatting may occur if the HTML source is verified by the Word HTML module when the item is sent.
To fix the problem, simply load Word.Interopt and use Word as the editor.
Translated with www.DeepL.com/Translator
public static void SendMail(string subject, string message, List attachments, string recipients)
{
try
{
Outlook.Application application = new Outlook.Application();
Outlook.MailItem mailItem = (Outlook.MailItem)application.CreateItem(Outlook.OlItemType.olMailItem);
Word.Document worDocument = mailItem.GetInspector.WordEditor as Word.Document;
Word.Range wordRange = worDocument.Range(0, 0);
wordRange.Text = message;
foreach (string attachment in attachments ?? Enumerable.Empty())
{
string displayName = GetFileName(attachment);
int position = (int)mailItem.Body.Length + 1;
int attachType = (int)Outlook.OlAttachmentType.olByValue;
Outlook.Attachment attachmentItem = mailItem.Attachments.Add
(attachment, attachType, position, displayName);
}
mailItem.Subject = subject;
Outlook.Recipients recipientsItems = (Outlook.Recipients)mailItem.Recipients;
Outlook.Recipient recipientsItem = (Outlook.Recipient)recipientsItems.Add(recipients);
recipientsItem.Resolve();
mailItem.Display();
recipientsItem = null;
recipientsItems = null;
mailItem = null;
application = null;
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
private static string GetFileName(string fullpath)
{
string fileName = Path.GetFileNameWithoutExtension(fullpath);
return fileName;
}
Have fun with it. Thomas