I use the following code to add an image to an RDOMessage and try to show this image inside the HTMLBody...
_message.BodyFormat = 2;
_message.HTMLBody = MailHelper.GetHtmlMessage(MailBody, "utf-8");
if (ImageSource != null && !string.IsNullOrWhiteSpace(ImageName))
{
PngBitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(ImageSource));
string id = "qr-code.png";
int index = _message.HTMLBody.IndexOf("%imageQr%");
if (index >= 0)
{
string tempDir = TempDirectory.Create();
string tmpFile = Path.Combine(tempDir, id);
using (FileStream fileStream = new FileStream(tmpFile, FileMode.Create))
{
encoder.Save(fileStream);
}
RDOAttachment attachment = _message.Attachments.Add(tmpFile, 1, null, ImageNage);
attachment.ContentID = id;
_message.HTMLBody = _message.HTMLBody.Insert(index, $"<img src=\"cid:{id}\">");
_message.HTMLBody = _message.HTMLBody.Replace("%imageQr%", string.Empty);
}
}
But the only thing I see in the message is this:
So the question is: What is not correct? (before and after the image are each three underlines, that is not the problem ;)) I used it in a slightly different version with the OOM version and that worked well...
Interesting to note is here, that with the OOM I could specify the id
as a Guid
without file name ending (i.e. without a .png), if I did that here with RDO the attachment itself was also not able to open...
Edit: When I send the email it arrives more or less correctly with this body tag:
<body lang=DE link="#0563C1" vlink="#954F72" style='word-wrap:break-word'>
<div class=WordSection1>
<p>
___<img width=32 height=32 style='width:.3333in;height:.3333in' id="_x0000_i1025" src="cid:8b2b3585-c611-4562-884c-915fc9f9b759">___
<o:p>
</o:p>
</p>
</div>
</body>
But the size is not correct and I didn't set it explicitly, also the style is not set by me...
What it should look like is this (and it does with OOM):
______
The image has 100x100 px, so the second would be correct...
But what even more is disturbing is, that it looks like below when I havn't send it yet. The user would think that the image is broken. (And I don't know how to see the HTML of a not sent email).
If I save the message and open it again it looks like this:
Shortly before I send the message the HTMLBody looks like this:
<!DOCTYPE html>
<html>
<head>
<meta charset=\"utf-8\">
</head>
<body>
<p>___<img src=\"cid:ab0c3c9a-9a9f-4fe7-a8e6-45c0735473a6\">___</p>
</body>
</html>
And BodyFormat is 2, so it should be used as HTML, shouldn't it? So, why does it not arrive at Outlook like this?
With the help of @Dmitry Streblechenko (thanks) I found the answer:
After adding the ID to the attachment I had to save the attachment explicitly. So adding a simple:
attachment.Save();
after
attachment.ContentID = id;
did the trick.