Switch outlook email from editable to not editable (C# .NET)

Hello,

I’ve an email stored as .msg. This email is not sent, so when the .msg file is opened, the user can change the content of the mail. I want to forbid this.

I’ve found another thread that just asked the opposite, but I didn’t suceed to make it works my way.

Here is the code I tried:

var email = MailMessage.Load(@“Test.msg”, MessageFormat.Msg);
MapiMessage mapiMsg = MapiMessage.FromMailMessage(email);
mapiMsg.SetMessageFlags(mapiMsg.Flags & ~MapiMessageFlags.MSGFLAG_UNSENT);
email.Save(@“Test2.msg”, MailMessageSaveType.OutlookMessageFormat);

when I open Test2.msg, it’s still in edit mode.

Do you know how to “clear” the unsent flag ?

Thank you,
Fab

Hi Fab,


Thank you for writing to us.

The MailMessage class considers a message as UnSent if it contains the header X-Unsent and its value is 1. In order to mark a message as sent/non-editible, you can remove this header from the loaded message and then save it. Please have a look at the following code for your reference and let us know if you need further assistance in this regard.

Also, please use the latest version of Aspose.Email for .NET 2.8.0 in your applications.

Code Sample:

//Load an Unsent message
MailMessage mailMsg = MailMessage.Load(“UnsentMsg.msg”);

//Remove the header that determines the message as Unsent
mailMsg.Headers.Remove(“X-Unsent”);

//Save the message

mailMsg.Save(“TestOut.msg”, MailMessageSaveType.OutlookMessageFormat);

I didn’t know it was based on an header

Thank you for your very quick answer, it works perfectly !

Hi Fab,

Thanks for the feedback and its good to know that your requirements are met now.

In addition, I tried the following code sample and it worked as well for me for an Unsent/Editable message.

Sample Code:

MapiMessage mapi = MapiMessage.FromFile("Unsent.msg");
mapi.SetMessageFlags(mapi.Flags & ~MapiMessageFlags.MSGFLAG_UNSENT);
mapi.Save("UnEditible.msg");
Hi Fab,

Fabske:

Here is the code I tried:

var email = MailMessage.Load(@"Test.msg", MessageFormat.Msg);
MapiMessage mapiMsg = MapiMessage.FromMailMessage(email);
mapiMsg.SetMessageFlags(mapiMsg.Flags & ~MapiMessageFlags.MSGFLAG_UNSENT);
email.Save(@"Test2.msg", MailMessageSaveType.OutlookMessageFormat);

When I open Test2.msg, it's still in edit mode.


After observing your code sample, I found a mistake in your code sample. After setting the MSGFLAG_UNSENT, you should save the the mapiMsg instead of saving email in this case. Please let us know if you have any other query/inquiry related to Aspose.Email. We'll be glad to assist you further.

Aah ok
Thank you for solving this problem using 2 different ways :wink:

Hi Fab,


You are welcome and feel free to write to us for any further query/inquiry related to Aspose.Email. We’ll be glad to assist you further.


pls can i have full code in vb.net

Thanks

hi
i tried this code, still i get editable msg file, here the code
Dim mapi As Mapi.MapiMessage = MapiMessage.FromFile(“UnsentMsg.msg”)
mapi.SetMessageFlags(MapiMessageFlags.MSGFLAG_UNSENT & MapiMessageFlags.MSGFLAG_FROMME)
mapi.Headers.Remove(“X-Unsent”)
mapi.Save(UnsentMsg1.msg)

@bpanchu,

Does the sample code worked on your end?

hi , Yes it working.

Thanks

@bpanchu,

Its good to know things are fine on your end.