MailMessageGetHtmlAndRelatedFilesInMemory Method |
Namespace: MailBee.Mime
Exception | Condition |
---|---|
MailBeeInvalidArgumentException | virtualPathPrefix is a null reference (Nothing in Visual Basic). |
This method is useful if you want to store inline attachments on an external storage or in database (i.e. not on the local filesystem). It does not save anything to disk, just replaces content-id references in the HTML body with a provided path and auto-incrementing index. However, for each inline attachment in Attachments collection, its SavedAs value is still set (you can use it to find out which value was assigned to each particular inline attachment). Non-inline attachments are not touched and their SavedAs remains an empty string.
You can then access binary content of each inline attachment with GetData method.
This method is a shortcut for GetHtmlAndSaveRelatedFiles(virtualPathPrefix, VirtualMappingType.StaticInMemory, MessageFolderBehavior.DoNotCreate).
using System; using MailBee.Mime; class Sample { static void UploadToExternalStorage(string webPath, byte[] bytes) { // This method must upload 'bytes' to some location which is also visible via web as 'webPath'. } static void Main(string[] args) { // Load the message from file. MailMessage msg = new MailMessage(); msg.LoadMessage(@"C:\Temp\test.eml"); // You'll then pass 'body' value to some kind of a browser or webview control. // 'body' will have links like <IMG SRC="cid:my_first_img"> and <IMG SRC="cid:my_second_img"> // look like <IMG SRC="https://domain.com/attach_1"> and <IMG SRC="https://domain.com/attach_2"> string body = msg.GetHtmlAndRelatedFilesInMemory("https://domain.com/attach_"); foreach (Attachment attach in msg.Attachments) { if (!string.IsNullOrEmpty(attach.SavedAs)) { string webPath = attach.SavedAs; // e.g. https://domain.com/attach_1 byte[] bytes = attach.GetData(); UploadToExternalStorage(webPath, bytes); } } } }