Free Support Forum - aspose.com

Need to save email as "sent" .msg file during ItemSend

Hello Aspose…

We have written an Outlook Add-in that processes an email while it is being Sent. If you have ever worked with add-in express you may recognise the event adxOutlookEvents_ItemSend

Within the context of this event, the MailItem has not yet been sent. The idea being that, based on certain criteria, you can set e.Cancel to True and the email does not actually get Sent.

At the point where we are happy with the email and we would like to allow Sending to proceed, we can save the email as a .msg file using Outlook.MailItem.SaveAs

This is all well and good, but if you subsequently open the resulting .msg file, it’s presented to the user in “compose” mode. In other words, all the fields are editable. There is no “sent” date/time, there is no “from”.

None of the above has anything to do with Aspose, of course, but now we’re getting to the point where Aspose may be of help to us.

I am looking for a complete example of how to open such an “unsent” .msg file, and save it as a “sent” one.

I found examples such as this https://docs.aspose.com/display/emailnet/Working+with+MAPI+Properties#WorkingwithMAPIProperties-SettingMAPIProperties but I’m not making much progress with them. For example I see it setting a messageflag MSGFLAG_UNSENT but I see no flag like MSGFLAG_SENT. I tried MSGFLAG_ZERO but that still gives me a .msg file with a “null” SentOn date and no sender. I’m looking at outlook specific examples such as https://social.msdn.microsoft.com/Forums/office/en-US/741ea4f1-32cd-4ef5-800f-eda3a3212046/-c-set-a-mailitem-to-sent-true?forum=outlookdev but I can’t see the woods for the trees…

So I was hoping somebody in Aspose could cook me up a nice, self-contained example setting all the properties, values, etc. that I need to set in order for my Outlook to treat the .msg file as a Sent message with all the relevant data present.

@rozeboosje,

The following is possible option using Aspose.Email that you may try on your end. You can use Message flag MSGFLAG_UNSENT to serve the purpose.

            DateTime sentDate = new DateTime(2009, 12, 01);
            DateTime receivedDate = new DateTime(2011, 11, 02);
            MailMessage msg = new MailMessage();
            msg.To = "to@domain.com";
            msg.CC = "cc@domain.com";
            msg.Subject = "Subject";
            msg.HtmlBody = "<h3>Html Heading 3</h3> <u>This is underlined text</u>";            
            MapiMessage mapiMessage = MapiMessage.FromMailMessage(msg);
            mapiMessage.ClientSubmitTime = sentDate;
            mapiMessage.DeliveryTime = receivedDate;
            mapiMessage.SetMessageFlags(mapiMessage.Flags & ~MapiMessageFlags.MSGFLAG_UNSENT); //Set it as sent
            mapiMessage.SenderName = "Rok";
            mapiMessage.SenderEmailAddress = "rok.povodnik@gmail.com";
            mapiMessage.Save("c:\\Downloads\\1.msg");
1 Like

That did the job! Thanks a million.

I simplified the code a little bit as there is no need for me to set Subject, body, To, CC or whatnot, so I don’t actually need the MailMessage object. Also, I work in VB.NET so I’m a bit wary of its Bit operations, but that’s easily adapted. So all I need then is a Sender name, a Sender email address and the “sent date” and…

        oMAPI = Aspose.Email.Mapi.MapiMessage.FromFile(path:=sFile)
        Dim nFlags As Int32
        Dim nUnsent As Int32
        nFlags = Convert.ToInt32(oMAPI.Flags)
        nUnsent = Convert.ToInt32(Aspose.Email.Mapi.MapiMessageFlags.MSGFLAG_UNSENT)
        If (nFlags And nUnsent) = nUnsent Then
            nFlags = nFlags - nUnsent
        End If
        oMAPI.SetMessageFlags(nFlags)
        oMAPI.ClientSubmitTime = dSent
        oMAPI.DeliveryTime = dSent
        oMAPI.SenderName = sFromName
        oMAPI.SenderEmailAddress = sFrom
        oMAPI.Save(fileName:=sFile)

This worked perfectly. Many thanks. You have no idea how many years we have tried to code around this problem. Now we don’t need to, anymore :smiley:

@rozeboosje,

That really pleasing to hear that suggested approach has worked on your end. Please feel free to share if you have any issue related to API.