We're sorry GroupDocs doesn't work properply without JavaScript enabled.

Free Support Forum - groupdocs.com

Convert Email with Attachments to PNG in C#

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)

1 Like

@Clemens_Pestuka

We are looking into this scenario. Your investigation ticket ID is CONVERSIONNET-4998.

1 Like

@Clemens_Pestuka

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);
}
1 Like

Hi @Atir_Tahir,

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)

1 Like

@Clemens_Pestuka

We are further looking into this scenario.

1 Like

@Clemens_Pestuka

Using API version 22.1, if we set emailLoadOptions.ConvertOwned to false, we don’t get any exception/error.

Email Load Options when Converting MSG to PNG using C#

Please have a look at the following code:

//C# code
// specify email loading options 
var emailLoadOptions = new EmailLoadOptions();
emailLoadOptions.ResourceLoadingTimeout = TimeSpan.FromSeconds(10);
//set convert owned to false
emailLoadOptions.ConvertOwned = false; 
//Pass MSG file to the API
using (var conv = new Converter(@"D:/TEST (1).msg", () => emailLoadOptions))
{
    //specify image conversion options 
    var imgOptions = new ImageConvertOptions()
    {
        //set output image format to PNG
        Format = GroupDocs.Conversion.FileTypes.ImageFileType.Png,
        //convert only the first page for thumbnail
        PageNumber = 1,
        PagesCount = 1,
        Width = 256,
    };
    string filePath = @"D:/outputConversion{0}.png";
    GroupDocs.Conversion.Contracts.SavePageStream getPageStream = page => new FileStream(string.Format(filePath, page), FileMode.Create);
    conv.Convert(getPageStream, imgOptions);
}

Please try the latest API version and let us know if issue persists.

Hi @Atir_Tahir,

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:

1 Like

@Clemens_Pestuka

You’re welcome.