0 votes
by (420 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
by (420 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
by (73.6k points)
selected by
 
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);
}
...