Is it possible to create a message in a PST that has different plain body and html body content?
For example, HTML body might be “The HTML body” and the plain body might be “The plain body”
Is it possible to create a message in a PST that has different plain body and html body content?
For example, HTML body might be “The HTML body” and the plain body might be “The plain body”
@weswt
Cause
Yes, it is possible to create a message in a PST file with different body content for plain text and HTML formats. The MailMessage class in Aspose.Email allows you to set both the plain text and HTML body separately.
Solution
To achieve this, you can use the HtmlBody property to set the HTML content and the Body property for the plain text content. When you save the message, both formats will be included in the PST file.
Code
Here’s a simple example demonstrating how to create a MailMessage with both HTML and plain text bodies:
using Aspose.Email;
using Aspose.Email.Mail;
using Aspose.Email.Storage.Pst;
// Create a new instance of MailMessage
MailMessage mailMessage = new MailMessage();
// Set the sender and recipient addresses
mailMessage.From = "sender@example.com";
mailMessage.To.Add("recipient@example.com");
// Set the subject
mailMessage.Subject = "Test Email";
// Set the HTML body
mailMessage.HtmlBody = "<b>The HTML body</b>";
// Set the plain text body
mailMessage.Body = "The plain body";
// Create a PST file and save the message
using (PersonalStorage pst = PersonalStorage.Create("output.pst", FileFormatVersion.Unicode))
{
// Create a folder in the PST
pst.RootFolder.AddSubFolder("Emails");
// Save the message to the PST
pst.RootFolder.GetSubFolder("Emails").AddMessage(mailMessage);
}
Next Steps
If you have any further questions or need additional assistance, feel free to ask!
Hi, thanks for the reply. That does not seem to work me.
*** Note that I’m using the evaluation license. Maybe this problem does not exist with a full license. ***
I had to change your code to convert the MailMessage to a MapiMessage as I found no overload of FolderInfo.AddMessage that took a MailMessage instance. Here’s the modified code.
using Aspose.Email;
//using Aspose.Email.Mail;
using Aspose.Email.Mapi;
using Aspose.Email.Storage.Pst;
// Create a new instance of MailMessage
MailMessage mailMessage = new MailMessage();
// Set the sender and recipient addresses
mailMessage.From = "sender@example.com";
mailMessage.To.Add("recipient@example.com");
// Set the subject
mailMessage.Subject = "Test Email";
// Set the HTML body
mailMessage.HtmlBody = "<b>The HTML body</b>";
// Set the plain text body
mailMessage.Body = "The plain body";
// Create a PST file and save the message
using (PersonalStorage pst = PersonalStorage.Create("output.pst", FileFormatVersion.Unicode))
{
// Create a folder in the PST
pst.RootFolder.AddSubFolder("Emails");
// Save the message to the PST
//pst.RootFolder.GetSubFolder("Emails").AddMessage(mailMessage);
// *** must change to MapiMessage first ***
MapiMessage mapiMessage = MapiMessage.FromMailMessage(mailMessage);
pst.RootFolder.GetSubFolder("Emails").AddMessage(mapiMessage);
}
I can set a breakpoint after the bodies are set on the MailMessage and see that the HTML and plain bodies are different.
image.png (47.9 KB)
But after the conversion to MapiMessage I can see that the plain body text (“The plain body.”) is lost and only contains the “Evaluation Only” message. The HTML body also contains the “Evaluation” message but it also contains the text that I set.
I can then use MFCMapi to confirm that even in the PST the plain body only contains the Evaluation message.
This seems to do what I want:
using (PersonalStorage pst = PersonalStorage.Create(Utils.GetPstFileName("test"), FileFormatVersion.Unicode))
{
pst.Store.ChangeDisplayName("TEST!!!");
var folder = pst.RootFolder.AddSubFolder("Messages");
MapiMessage mapiMessage = new(OutlookMessageFormat.Unicode)
{
SenderName = "Sender",
SenderSmtpAddress = "sender@sender.sender",
Subject = "the subject",
ClientSubmitTime = DateTime.Now
};
mapiMessage.Recipients.Add("recip@recip.recip", "Recip", MapiRecipientType.MAPI_TO);
string htmlBody = """
<b>THE HTML BODY!</b>
""";
string plainBody = """
THE PLAIN BODY!
""";
var htmlBodyProp = new MapiProperty(MapiPropertyTag.PR_HTML, Encoding.UTF8.GetBytes(htmlBody));
var plainBodyProp = new MapiProperty(MapiPropertyTag.PR_BODY_W, Encoding.Unicode.GetBytes(plainBody));
mapiMessage.SetProperty(htmlBodyProp);
mapiMessage.SetProperty(plainBodyProp);
mapiMessage.SetMessageFlags(MapiMessageFlags.MSGFLAG_SUBMIT);
folder.AddMessage(mapiMessage);
}
I find the body-related methods/properties on MapiMessage to be confusing.
Hello @weswes,
The body-related methods and properties on MapiMessage can be a bit confusing at first. The class supports multiple formats for message content (plain text, HTML, and RTF), and you can set them using either high-level properties or by directly assigning MAPI properties using SetProperty.
In your example, you’re correctly assigning both the plain text body (PR_BODY_W) and the HTML body (PR_HTML) using MapiProperty and SetProperty, which gives you full control over the encoding and formatting.