If we convert email with attachments to PNG in C#, we get System.IO.IOException, because the code tries to overwrite already existing files.
This happens because GroupDocs conversion passes “1” as page number twice when requesting the page stream. As only one page should be rendered, there shouldn’t even be a 2nd call, but a second call with the same page number is even more strange.
Convert Email to PNG using C#
//C# code
using (var conv = new Converter(documentPath))
{
var imgOptions = new ImageConvertOptions()
{
Format = Conv.FileTypes.ImageFileType.Png,
//convert only the first page for thumbnail
PageNumber = 1,
PagesCount = 1,
Width = 256
};
SavePageStream getPageStream = page => new FileStream(string.Format("output{0}.png", page), FileMode.CreateNew);
conv.Convert(getPageStream, imgOptions);
}
Looks like this behavior is new in GroupDocs 21.10 or 21.11.
It only happens with some emails, I’ve attached two for testing: sample emails.zip (215.0 KB)
There’s some issue with the code you shared because if email has attachments, they are also converted to PNG. You can also convert email and each attachment based on the file type to different format.
Please try the following approach and let us know:
using (var conv = new Converter(documentPath))
{
var imgOptions = new ImageConvertOptions()
{
Format = GroupDocs.Conversion.FileTypes.ImageFileType.Png,
//convert only the first page for thumbnail
PageNumber = 1,
PagesCount = 1,
Width = 256
};
var counter = 1;
SavePageStream getPageStream = page => new FileStream($"output-{counter++}-{page}.png", FileMode.CreateNew);
conv.Convert(getPageStream, imgOptions);
}
I’m sorry for the late reply.
Unfortunately I skipped some part of the code for simplifying the sample.
We are actually using EmailLoadOptions with “ConvertOwned” set to false.
I understand that attachments are converted in my sample, but with the missing “ConvertOwned” flag set to false, this should no longer be the case.
But this flag is somehow ignored now and the attachment is converted as well, leading to the described error. This has changed with a recent version change (I think 21.10 or 21.11)
Yes it seems like the issue has been fixed, thanks for that.
I also cannot reproduce it any longer with the latest version.
If “ConvertOwned” is set to true, something strange still happens, but I’ve reported this behavior there already: