MailMessageGetHtmlAndSaveRelatedFiles Method (String, VirtualMappingType, MessageFolderBehavior) |
Namespace: MailBee.Mime
public string GetHtmlAndSaveRelatedFiles( string virtualPath, VirtualMappingType mappingType, MessageFolderBehavior folderMode )
Exception | Condition |
---|---|
MailBeeInvalidArgumentException | virtualPath is a null reference (Nothing in Visual Basic) and mappingType is not NonWeb. |
MailBeeException | An error occurred and ThrowExceptions is true. |
Note |
---|
If folderMode is CreateOnly, the unique folder will be created and filled with the message files, but not deleted after the MailMessage object destruction. This is useful in web applications where all objects get automatically deleted once a response to the client has been generated. In this case, the message files/folder must retain after MailMessage destruction, and should only be deleted when the user has finished viewing the message (the user requested another page or the current session timed out). To remember which temporary folder to delete later, the application should save the names of the created unique folder in Session, database, or any other store which retains its data between round-trips to the server. To get this name, the developer can call MailMessage.Parser.GetMessageFolder method after the unique folder has been created during GetHtmlAndSaveRelatedFiles(String, VirtualMappingType, MessageFolderBehavior) method call. |
mappingType specifies how physical paths to embedded pictures (and other inline attachments related in an HTML body and saved into temporary location by this method) are mapped into URI's placed into SRC elements of corresponding HTML tags.
For instance, if there is an HTML message with the following properties:
mappingType | IMG SRC value |
---|---|
NonWeb | C:\Inetpub\wwwroot\picture.gif |
Static | http://www.domain.com/picture.gif |
Dynamic | See description below |
StaticInMemory | http://www.domain.com/1 (2, 3, etc) |
If mappingType is Dynamic, the virtualPath parameter must point to a downloader page. Downloader page is a dynamic web page (such as an ASP.NET script) which should accept message_id and file_id parameters, grab file_id file in message_id folder and send its binary contents to the client. This allows the developer to save all inline attachments in the folder which is not directly visible from the web, and use the downloader script to push the file contents to the client. The downloader script can also check user credentials or SessionID to validate the user and do not return the attachment content if the user does not pass the authentication or session check.
message_id folder is the name of a unique message folder where all the files of the mail message are saved. If folderMode is DoNotCreate, the message files are saved directly in WorkingFolder, and message_id is an empty string.
file_id is a Filename of the inline attachment being referenced.
Thus, in Dynamic mode, MailBee replaces Content-ID (CID) with:
virtualPath + "message_id=" + SHA1Digest(Message-ID) + "&file_id=" + InlineAttachmentFilename.
For instance, GetHtmlAndSaveRelatedFiles("http://www.domain.com/download.aspx?session_id=12345&", VirtualMappingType.Dynamic, MessageFolderBehavior.CreateOnly) method call will replace cid:pic1 with http://www.domain.com/download.aspx?session_id=12345&message_id=D41D8CD98F00B204E9800998ECF8427E&file_id=picture.gif.
For more info on , refer to method.
Note |
---|
You can also use SaveHtmlAndRelatedFiles(String) method to save messages as HTML pages. URI is a synonym of an URL. |
using System.IO; using MailBee; using MailBee.Mime; class Sample { static void Main(string[] args) { // Load the message from file. MailMessage msg = new MailMessage(); msg.LoadMessage(@"C:\Docs\TestMail.eml"); // Set the folder where all related files should be saved. msg.Parser.WorkingFolder = @"C:\Temp"; // Generate a web page at the specified location. using (StreamWriter sw = new StreamWriter(@"C:\Temp\web_page.htm", false)) { sw.Write(msg.GetHtmlAndSaveRelatedFiles(@"C:\Temp", VirtualMappingType.NonWeb, MessageFolderBehavior.CreateAndDelete)); } } }