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

Free Support Forum - aspose.com

Suggestion not bug: MapiMessage.SetMessageFlags() is limiting

When setting MapiMessage flags, I notice that the method SetMessageFlags() clears all previous flags and sets to whatever is passed in. This is limiting; there should be a way to apply a single flag or remove a single flag at a time.

Because of this, I have to set all the flags once at the end of all my MapiMessage setup code with extra logic, like so:
MapiMessageFlags.MSGFLAG_READ |
((sentDate <= DateTime.MinValue) ? MapiMessageFlags.MSGFLAG_UNSENT : 0) |
((msg.Attachments.Count > 0) ? MapiMessageFlags.MSGFLAG_HASATTACH : 0)

It is not possible to do something like this, because each call to SetMessageFlags() will clear the previous flags:

if (sentDate <= DateTime.MinValue) msg.SetMessageFlag(MapiMessageFlags.MSGFLAG_READ);


I noticed this problem when adding attachments to MapiMessage by calling MapiMessage.Attachments.Add(); Aspose.Email is calling SetMessageFlags() internally and therefore clears all my previously set flags, which is not nice :slight_smile:

I recommend one of two solutions:

1. Add a SetMessageFlag(flag, boolean) method.
This way we can set a single flag on or off.
msg.SetMessageFlag(MapiMessageFlags.MSGFLAG_UNSENT, true);
msg.SetMessageFlag(MapiMessageFlags.MSGFLAG_UNSENT, false);

2. Add a GetMessageFlags() method.
This way we can set a single flag be doing something like this:
msg.SetMessageFlags(msg.GetMessageFlags() | MapiMessageFlags.MSGFLAG_UNSENT);
msg.SetMessageFlags(msg.GetMessageFlags() & ~MapiMessageFlags.MSGFLAG_UNSENT);


Thank you very much for this suggestion and its highly appreciated when enhancements are suggested by customers. I will verify it and log a ticket for my developers to analyse and consider it in future versions if possible. As soon as I get some feedback, that will be notified to you.

Best Regards

Sorry to report, but we actually do need the functionality to get the message flags from a MapiMessage. This functionality is not currently exposed in the Aspose.Email API. We need to be able to check if a message is read or unread, etc.

So we need some functionality like this:
bool isread = msg.GetMessageFlag(MSGFLAG_READ);
bool issent = !msg.GetMessageFlag(MSGFLAG_UNSENT);
or this:
bool isread = msg.GetMessageFlags() & MSGFLAG_READ;

bool issent = !msg.GetMessageFlags() & MSGFLAG_UNSENT;

It would be nice if both GetMessageFlag(flag) and SetMessageFlag(flag, bool) are implemented. They are simple to add, and necessary functionality.


Actually, in the mean time, I guess we could just get the PR_MESSAGE_FLAGS property in MapiMessage, and then test the bits with the MSGFLAG_READ, etc.

But the GetMessageFlag() would be nice :slight_smile:

public static bool GetMessageFlag(MapiMessage msg, MapiMessageFlags flag)
bool? output = null;
long? flags = msg.GetPropertyLong(MapiPropertyTag.PR_MESSAGE_FLAGS);
if (flags != null && flags > 0)
output = (((long)flags & (long)flag) > 0);
catch (Exception ex)
log.Error(“Failed to GetMessageFlag for MapiMessage.”, ex);
return output ?? false;


Thank you very much for your suggestions for enhancements.

I have logged a ticket NETWORKNET-33306 for analysis and adding these features in the future versions if possible.

Keep on commenting for improvements.

Best Regards


Thank you for your patience.

We would like to confirm, will it satisfy your requirement if we add the following property to the MapiMessage class.

public MapiMessageFlags Flags

Below is the proposed usage,
msg.SetMessageFlags(msg.Flags | MapiMessageFlags.MSGFLAG_UNSENT);
bool isread = (msg.Flags & MapiMessageFlags.MSGFLAG_READ) == MapiMessageFlags.MSGFLAG_READ;
bool issent = (msg.Flags & MapiMessageFlags.MSGFLAG_UNSENT) != MapiMessageFlags.MSGFLAG_UNSENT;

Sure, it works.

Hopefully it is just a lightweight property wrapping an int or long, and not wrapping the entire MAPI property accessing. In general I try not to make heavyweight properties that can throw exceptions, because developers will access the property in a statement. If it can throw an exception, I usually make it a method or function.



Thank you for the confirmation on this.

I have attached your comments to the ticket already associated with this thread. Also I have intimated the development team regarding your concerns.

As soon as I receive more information, I will pass it on to you.

The issues you have found earlier (filed as NETWORKNET-33306) have been fixed in this update.

This message was posted using Notification2Forum from Downloads module by aspose.notifier.

Is there an analog to:

public MapiMessageFlags Flags

in Java? I did not find one.

Please refer to the following code for usage of setMessageFlag. It sets the messages flags to read/unread and then save them to a PST.

String pstFile = "TestPST.pst";
String folderName = "Issue";
PersonalStorage pst = PersonalStorage.create(pstFile, FileFormatVersion.Unicode);
FolderInfo outfolder = pst.getRootFolder().getSubFolder(folderName);

MapiMessage mapimsg = MapiMessage.fromFile("Subject1.msg");

mapimsg = MapiMessage.fromFile("Subject2.msg");

mapimsg = MapiMessage.fromFile("Subject3.msg");

Please feel free to write us back if you have any other query in this regard.