When saving docx as MHTML to outputstream for use in Mailmessage &p in "&path=" displays as "new line ath="

Hi Aspose support,

I have a problem where when I convert a word doc to MHTML for use in a MailMessage object, and part of that Word doc has a url with &path= as part of the url in the body, it converts it to a new line on the email sent.

The output is like the following

url.com/foo?bar

ath=blah

Expected output:
url.com/foo?bar&path=blah

I have tried several escape codes but they come across as plain text. I assume this is because MHTML does not support html escape codes but does support html special characters, as &p is a special character denoting a pagebreak, but & and & do not work.

Here is how I generate the MailMessage Object to be sent. (I set the subject and to address in different methods) the doc parameter is a template that gets filled in with variable data so I can’t just pass it into the MailMessage Body as plain text.

private MailMessage generateEmailFromWordTemplate(Document doc) {

  ByteArrayOutputStream outStream = new ByteArrayOutputStream();
  try {
    doc.save(outStream, SaveFormat.MHTML);


    ByteArrayInputStream inStream = new ByteArrayInputStream(outStream.toByteArray());

    MailMessage message = MailMessage.load(inStream);


    outStream.close();
    inStream.close();
    return message;
  } catch (Exception e) {
    return null;
  }
}

Any ideas of how to display &p as plain text in MHTML

Hello @Ebaldwin174 ,

Welcome to the Aspose.Email support forum.
We will check your case and notify you of the decision ASAP.

Hello @Ebaldwin174 ,

We have prepared sample MHTML file to test
Body contains two url lines with “&” and “& amp;”

url.com/foo?bar&path
url.com/foo?bar&path

We sent email and check received mail, all lines without unexpected pagebreak (tested on Outlook with Exchange Server).
Could you provide the MHTML sample file to testing?

So thats the thing, we arent using an mhtml file to do this. We are using a word docx through aspose-words and using the saveFormat.MHTML to build the MHTML outputstream. I never have a mhtml file created just saved in an outputstream that eventually gets closed.

I actually found a workaround using the string escape code for a backspace \b after the & in &path.

It works like the following &\bpath, because we remove space inbetween the &and the p once it gets to the mhtml format it reads it as plain text since there was never a &p character in the output.

Thank you for the suggestions though.

Hello @Ebaldwin174,

We’re glad you were able to resolve the issue. Please feel free to contact us if you have any other questions.