How to convert linked resources to attachments

0 votes
asked Jun 8 by dsouzac (360 points)

Hi,

I am trying to show inbound email in an application wherein inline (embedded) image content wanted to show as an attachments.

So here need to convert message.Resources content to AttachmentCollection.

here is an attachment collection in which Resouces content we have to add.

could you help me in the casting Resources into AttachmentCollection. (LinkedResource to Attachment)

Applies to: Rebex Secure Mail
commented Jun 8 by dsouzac (360 points)
I am trying this way, is there any other way in Rebex? optimal or proper?


LinkedResourceCollection linkedCollection = validatedMessage.Resources;

if(linkedCollection != null)
{
    foreach (LinkedResource lr in linkedCollection)
    {
        if (lr.ContentId != null)
        {
            Stream stream = lr.GetContentStream();
            Attachment at = new Attachment(stream, lr.FileName);
            attachmentCollection.Add(at);
        }
    }
}

1 Answer

0 votes
answered Jun 8 by Lukas Matyska (39,900 points)
selected Jun 8 by dsouzac
 
Best answer

Actually, you should copy MediaType and other properties as well. So your code should look like this:

foreach (var res in mail.Resources)
{
    if (res.ContentId == null)
        continue;

    var att = new Attachment(res.GetContentStream(), res.FileName, res.MediaType);
    att.ContentId = res.ContentId;
    if (res.ContentDescription != null)
        att.ContentDescription = res.ContentDescription;
    if (res.ContentLocation != null)
        att.ContentLocation = res.ContentLocation;
    mail.Attachments.Add(att);
}
...