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

Free Support Forum - aspose.com

Modify MAPI Message with Inline Image in HTML Body

Hello,

I was having an issue updating HTML body text in an mapi message with an inline image. The following code worked fine for a message without any inline images but when there was an inline image I had to use this line: msgModded.Attachments.Clear(); in order for the body to actually update, but I would then be left with a red x in place of the image.

        if (msgModded.BodyType == Aspose.Email.Mapi.BodyContentType.Html)
        {
            string bodyHtml = msg.BodyHtml;
            Console.WriteLine(bodyHtml);
            bodyHtml = bodyHtml.Replace("word1", "word2");
            msgModded.SetBodyContent(bodyHtml, Aspose.Email.Mapi.BodyContentType.Html);
        }
        else if (msgModded.BodyType == Aspose.Email.Mapi.BodyContentType.PlainText)
        {
            string bodyPlain = msg.Body;
            bodyPlain = bodyPlain.Replace("word1", "word2");
            msgModded.SetBodyContent(bodyPlain, Aspose.Email.Mapi.BodyContentType.PlainText);
        }

Also, I wanted to change the From, BCC/CC, Thread-Topic, and Return-Path headers. Is it normal that it seemed like the only way to do this was to convert from mapi to mailmessage? Otherwise some of those headers did not seem to update properly.

Thanks

@awalker1,

I have checked the sample code using one of my sample file and observed little different behaviour as mentioned by you. When this code is executed, it modifies the html text regardless of inline image presence. However inline image is gone and no X sign shown if inline image is present. Could you please give it a try using latest version and share the feedback?

If you have some different observation, please share the sample message for our analysis.

Once the feedback is received, we will log a bug where inline image is removed while updating the html body.

It seems appropriate to convert MapiMessage to MailMessage for converting these headers as in MapiMessage, you may require updating many properties to keep the MapiMessage intact.

I am using the latest 18.5.1 NuGet version and here is the sample message with the issue I mentioned. Even writing the body html to console seemed to work, but not saving it or adding it to a .pst.
msgWithHTMLIssue.zip (24.0 KB)

@awalker1,

I have tried this sample EML file in the below mentioned code but could not observe any issue as text is updated and image is also intact. The resultant MSG file is attached here for your reference. Could you please give it a try and share the feedback?

static void Email_178126_User_Sample()
{
    string path = @"C:\msgWithHTMLIssue";
    MapiMessage msgModded = MapiMessage.FromMailMessage(path + ".eml");
    if (msgModded.BodyType == Aspose.Email.Mapi.BodyContentType.Html)
    {
        string bodyHtml = msgModded.BodyHtml;
        Console.WriteLine(bodyHtml);
        bodyHtml = bodyHtml.Replace("Test", "Test it again");
        msgModded.SetBodyContent(bodyHtml, Aspose.Email.Mapi.BodyContentType.Html);
        //msgModded.Attachments.Clear();
    }
    else if (msgModded.BodyType == Aspose.Email.Mapi.BodyContentType.PlainText)
    {
        string bodyPlain = msgModded.Body;
        bodyPlain = bodyPlain.Replace("my", "not my");
        msgModded.SetBodyContent(bodyPlain, Aspose.Email.Mapi.BodyContentType.PlainText);
    }
    msgModded.Save(path + "-Modified.msg", MsgSaveOptions.DefaultMsgUnicode);
}

msgWithHTMLIssue-Modified.zip (24.6 KB)

I tried the sample code and it seemed to be working, thank you.

The difference with mine is I originally cloned an MapiMessage from inbox and tried to edit the body.

I tried loading as an MapiMessage, saving then reloading from the .eml file as another MapiMessage, and that seemed to work better for some reason.

If I wanted to remove the image, would I have to remove attachments when it is an MapiMessage and then remove the lines of HTML that refer to the image since as a Mailmessage inline images stay on attachments.clear()?

@awalker1,

You may please remove the inline image while EML is loaded into MailMessage. Once the inline image is removed, you may convert it to MapiMessage and then modify the body. Please give a try to the following sample code and share the feedback.

string path = @"C:\msgWithHTMLIssue\msgWithHTMLIssue";
MailMessage mail = MailMessage.Load(path + ".eml");
mail.LinkedResources.RemoveAt(0, true);

//MapiMessage msgModded = MapiMessage.FromMailMessage(path + ".eml");
MapiMessage msgModded = MapiMessage.FromMailMessage(mail);
if (msgModded.BodyType == Aspose.Email.Mapi.BodyContentType.Html)
{
    string bodyHtml = msgModded.BodyHtml;
    Console.WriteLine(bodyHtml);
    bodyHtml = bodyHtml.Replace("Test", "Test it again");
    msgModded.SetBodyContent(bodyHtml, Aspose.Email.Mapi.BodyContentType.Html);
}
else if (msgModded.BodyType == Aspose.Email.Mapi.BodyContentType.PlainText)
{
    string bodyPlain = msgModded.Body;
    bodyPlain = bodyPlain.Replace("my", "not my");
    msgModded.SetBodyContent(bodyPlain, Aspose.Email.Mapi.BodyContentType.PlainText);
}
msgModded.Save(path + "-Modified.msg", MsgSaveOptions.DefaultMsgUnicode);

This seemed to work for me, and I did not have to manually take the img tag out, thank you for your help.

@awalker1,

You are welcome.