- This isn’t your case.
Details (for example):
- Create folder with
Contacts
name, container class is IPF.Contact
- Create child folder: with
Contacts\Companies
name, class: IPF.Contact.Company
There was an exception: The specified container class of the added folder (IPF.Contact.Company) does not match the container class of the parent folder (IPF.Contact).
- SaveResourceHandler was marked as obsolete, over 2 years ago. Now, this member has been removed from the API. Below is a simplified code example to use instead of deprecated one.
// MSG or EML filename
var msgFileName = Path.Combine(dir, "test.msg");
// target HTML
var htmlFileName = Path.Combine(dir, "test.html");
if (!Directory.Exists(resourceDir))
{
Directory.CreateDirectory(resourceDir);
}
// Load MSG or EML
var msg = MailMessage.Load(msgFileName);
// Create HTML save options
var htmlSaveOptions = new HtmlSaveOptions
{
ResourceRenderingMode = ResourceRenderingMode.SaveToFile
};
// add ResourceHtmlRendering event
htmlSaveOptions.ResourceHtmlRendering += HtmlSaveOptions_ResourceHtmlRendering;
msg.Save(htmlFileName, htmlSaveOptions);
// ResourceHtmlRendering event handler
private void HtmlSaveOptions_ResourceHtmlRendering(object sender, ResourceHtmlRenderingEventArgs e)
{
var attachment = (AttachmentBase)sender;
e.PathToResourceFile = Path.Combine(resourceDir, attachment.ContentType.Name);
}
1 Like
Hello and thanks, nice, for SaveResourceHandler, using the new mode, how to do the save without embedding resources to the html file, instead make a folder beside it and save all files to it and add the refrences inside html file to them?
See the example above, it does exactly that. Or please explain your question more precisely .
1 Like
Thanks,
Dim msgFileName = Path.Combine(dir, "test.msg")
Dim htmlFileName = Path.Combine(dir, "test.html")
If Not Directory.Exists(resourceDir) Then
Directory.CreateDirectory(resourceDir)
End If
Dim msg = MailMessage.Load(msgFileName)
Dim htmlSaveOptions = New HtmlSaveOptions With {
.ResourceRenderingMode = ResourceRenderingMode.SaveToFile
}
htmlSaveOptions.ResourceHtmlRendering += HtmlSaveOptions_ResourceHtmlRendering
msg.Save(htmlFileName, htmlSaveOptions)
Private Sub HtmlSaveOptions_ResourceHtmlRendering(ByVal sender As Object, ByVal e As ResourceHtmlRenderingEventArgs)
Dim attachment = CType(sender, AttachmentBase)
e.PathToResourceFile = Path.Combine(resourceDir, attachment.ContentType.Name)
End Sub
Thank you for your example.
1 Like
Hello,
Sorry not quite an example, my converted code won’t work, let’s just see my Html Save Options:
Friend Function ASPSaveSetHTM(Optional ByVal EmbedResources As Boolean = True) As Email.HtmlSaveOptions
ASPSaveSetHTM = New Email.HtmlSaveOptions
ASPSaveSetHTM.CheckBodyContentEncoding = True
If EmbedResources = True Then
ASPSaveSetHTM.ResourceRenderingMode = Email.ResourceRenderingMode.EmbedIntoHtml
Else
ASPSaveSetHTM.ResourceRenderingMode = Email.ResourceRenderingMode.SaveToFile
ASPSaveSetHTM.ResourceHtmlRendering = HtmlSaveOptions_ResourceHtmlRendering()
End If
End Function
Private Sub HtmlSaveOptions_ResourceHtmlRendering(ByVal sender As Object, ByVal e As Email.ResourceHtmlRenderingEventArgs)
Dim MyAttachmentBase = CType(sender, Email.AttachmentBase)
e.PathToResourceFile = Path.Combine(resourceDir, MyAttachmentBase.ContentType.Name)
End Sub
-
Public Event ResourceHtmlRendering(sender As Object, e As Aspose.Email.ResourceHtmlRenderingEventArgs) is an event, and cannot be called directly. Use a RaiseEvent statement to raise an event.
-
Argument not specified for parameter e and sender of Private Sub HtmlSaveOptions_ResourceHtmlRendering…
In the old way I had to keep a global variable of HtmlFileName, now, resourceDir, am I correct?
Thanks
Sorry, I thought it would be easy for you to create VB.NET code with an event handler similar to provided C# example.
Imports System.IO
Imports Aspose.Email
Module Module1
' Define your directory paths
Private Const Dir As String = "D:\"
ReadOnly ResourceDir As String = Path.Combine(Dir, "my message")
Sub Main()
Dim msgFileName As String = Path.Combine(Dir, "my message.msg")
Dim htmlFileName As String = Path.Combine(Dir, "my message.html")
' Create the resource directory if it doesn't exist
If Not Directory.Exists(ResourceDir) Then
Directory.CreateDirectory(ResourceDir)
End If
' Load the MailMessage from the MSG file
Dim msg As MailMessage = MailMessage.Load(msgFileName)
' Define HTML save options with resource rendering
Dim htmlSaveOptions As New HtmlSaveOptions With {
.ResourceRenderingMode = ResourceRenderingMode.SaveToFile
}
' Define the event handler for resource rendering
AddHandler htmlSaveOptions.ResourceHtmlRendering, AddressOf HtmlSaveOptions_ResourceHtmlRendering
' Save the MailMessage as an HTML file
msg.Save(htmlFileName, htmlSaveOptions)
End Sub
' Event handler for resource rendering
Private Sub HtmlSaveOptions_ResourceHtmlRendering(ByVal sender As Object, ByVal e As ResourceHtmlRenderingEventArgs)
Dim attachment As AttachmentBase = CType(sender, AttachmentBase)
e.PathToResourceFile = Path.Combine(ResourceDir, attachment.ContentType.Name)
End Sub
End Module
1 Like
Hi and thanks, I am sorry indeed, not familiar with C#, not at all
Hello
2 things:
-
Still need to check this?
If Not (TypeOf MyAttachmentBase Is Email.LinkedResource) Then Return
-
Same problem, inside the saved files:
src=“C:\Users\Admin\Documents\RE_ Trial(Aspose_Email Evaluation)_files\image001.png”>
How to force it to be:
src=“xx_files\image001.png”>
Reason? Firefox will NOT show those images if full path is inserted like that, it has to be:
src="file:///C:/Users/Admin/Documents/RE_ Trial(Aspose_Email Evaluation)_files/image001.png
or the shortest way:
src=“xx_files\image001.png”>
So kindly advise how to force it to be saved as:
src=“xx_files\image001.png”>
Best
-
You should check the type of an attachment. It depends on your specific context of your code.
-
It seems you want to change the full file paths to relative paths like “xx_files\image001.png” to ensure compatibility with Firefox. You can achieve this using string manipulation:
htmlContent = htmlContent.Replace("C:\Users\Admin\Documents\RE_ Trial(Aspose_Email Evaluation)_files\", "xx_files\")
1 Like
Thanks indeed for your continuous support and patience, appreciated.
If the new one is replacement of old one as spoken before here, it does not support relative paths which is the default preferred saving method of many html processor/editors.
The given method to replace the html content with post processing the document using string manipulation is not a safe method as we’re dealing with HTML documents, what if it is located in D:\blah and inside the email/HTML document is also a similar string? The contents of our document will be changed as well.
If you don’t mind, add a note to consider support it please.
We will add a ticket to fix this