'MailKit - How can bodybuilder help create a complex email body with many images interspersed with text?
For example, what if you need to create an email body like this: Text ... Image ... Text ... Image ... Text
Here is one of the examples that works for one text and one image:
var builder = new BodyBuilder ();
var pathImage = Path.Combine (Misc.GetPathOfExecutingAssembly (), "Image.png");
var image = builder.LinkedResources.Add (pathLogoFile);
image.ContentId = MimeUtils.GenerateMessageId ();
builder.HtmlBody = string.Format (@"<p>Hey!</p><img src=""cid:{0}"">", image.ContentId);
message.Body = builder.ToMessageBody ();
Can we do something like builder.HtmlBody +=
to just keep adding more and more texts and images?
Solution 1:[1]
The BodyBuilder class is designed to constructing typically message structures, not the type of thing you are doing.
You will need to construct your message manually, not using BodyBuilder.
Solution 2:[2]
After quite a bit of trial/error/testing, I discovered that you can indeed keep adding text and images to the HtmlBody object, as my question speculated, by using builder.HtmlBody +=
In response to the increasingly widespread use of TLS instead of SSL, and therefore the need to abandon the use of Microsoft's obsolete SmtpClient component, I have developed a comprehensive emailing test component, in Visual Basic, using the wonderful MailKit from JStedfast.
As my question suggested, I wanted to give my users the ability to compose a handsome email body using text interspersed with images as needed. If any VB developers would like to benefit from this work, just let me know.
@jstedfast - I just saw your answer after posting this. For my production version, I need to add images from a blob field in a SQLServer table. I intend to use the manual method, as you stated, to do that. But for images I was loading into my sample program, I was able to make a fairly complex email body using src=file for each image, and adding them with builder.HtmlBody +=
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | jstedfast |
Solution 2 |