none
Create mail from existing MimeSource in C# VSTO add on RRS feed

  • Question

  • HI All,

    I have created a mime source from an existing mail. Now in vsto add on, I am trying to implement a reverse function which will convert mail from mime source. I want this behavior when I try to open any mail, I want it to replaced by the mail, for which mime source is available to me. I have handled Open event of ItemEvents_10_Event (of Office.Interop.Outlook namespace).

    Now I have Implemented function GetMailFromMime like below :--

    public MailItem GetMailFromMime(string mimeSource, MailItem oldMail)

    {                    var mimeStream = new COMMemoryStream(Encoding.ASCII.GetBytes(mimeSource));

                        Type converter = Type.GetTypeFromCLSID(MAPIMethods.CLSID_IConverterSession);

                        object obj = Activator.CreateInstance(converter);

                        MAPIMethods.IConverterSession session = (MAPIMethods.IConverterSession)obj;

                        if (session != null)

                        {

                            uint hr = session.SetEncoding(MAPIMethods.ENCODINGTYPE.IET_QP);//IET_UNICODE

                            hr = session.SetSaveFormat(MAPIMethods.MIMESAVETYPE.SAVE_RFC1521);

                            hr = session.MIMEToMAPI(mimeStream, (MAPIMethods.IMessage)oldMail.MAPIOBJECT, null, MAPIMethods.MAPITOMIMEFLAGS.CCSF_SMTP);

                            if (hr != 0)

                                throw new ArgumentException("There are some invalid COM arguments");

                            oldMail.Save();

                            string entryID = oldMail.EntryID;

                            Marshal.ReleaseComObject(oldMail);

                            oldMail = null;

                            //reload object

                            Microsoft.Office.Interop.Outlook.NameSpace olNamespace = Globals.DarkMail.Application.GetNamespace("MAPI");

                            oldMail = (MailItem)olNamespace.GetItemFromID(entryID);

                            string subject = oldMail.Subject;

                            string body = oldMail.HTMLBody;

                        }

                        return oldMail;

    }

    Here I have passed current mail item as oldMail. When I check the content (even after restarting of outlook), there is no impact on existing mail. If instead of using an existing mail, I create new mail and save it, I can see in Draft that there is new mail rightly created from mime source which I have passed. Now I dont want to use dirty trick, like modify opened mail from mail of draft (content by content) and then delete mail which is present in draft. Do we have any good work around ? or if I am doing any thing wrong, please provide me right way. I will appreciate if I can get a small example as well.

    Thanks

    Dev

    Monday, March 6, 2017 12:05 PM

All replies

  • You should not do that from the Open event. The best you can do is create a new message, import the MIME stream, save it and pray that Outlook does not overwrite the data created by MIME import. You can then completely release the item (Marshal.ReleaseComObject) and reopen it using Namespace.GetItemfromID.

    Another alternative is to avoid using the Outlook Object Model at all and either use Extended MAPI (C++ or Delphi) or Redemption (its RDO family of objects) to create the message and import the MIME data. Since the item would never be touched by OOM, opening it using Namespace.GetItemfromID after you are done should show all the imported data.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Monday, March 6, 2017 3:22 PM
  • HI Dmitry,

    Thanks for your reply. As I have explained in my question, Creating a new message will end me a new mail in draft item. It will not replace existing mail in inbox.

    Do you have any example how we can do same in Extended MAPI (C++) as by my vsto add in, I can easily call c++ function / dll using managed c++.

    I would say, I am bit disappointed by Microsoft people as we are using outlook and I have posted question here because I am expecting that I will get some solution of problem (and if solution is only extended MAPI (c++) then some link from where we can see the example for importing mime source and such related stuff). 

    Please have a look.

    Tuesday, March 7, 2017 12:13 PM
  • You cannot use Extended MPAI from managed C++. It has to be unmanaged C++.

    To avoid duplicating the discussion, see my comments at http://stackoverflow.com/questions/42533672/how-to-create-outlook-mail-item-from-mapi-stream#comment72423844_42533672


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Tuesday, March 7, 2017 4:40 PM
  • Hi Dmitry,

    We can make un-managed class in Managed C++ project. However even in worst case, I can wrap them in unmanaged dll and call from managed c# code. I agree that some good developers have already done this like you but somehow I dont have to use 3rd party lib. 

    If you can provide me example of Extended MAPI for importing mime source and other stuff (or if you have any link) even in C++, I will manage rest of stuff.

    Thanks for your help.

    Tuesday, March 7, 2017 4:48 PM
  • Take a look at the MFCMAPI source code (MAPIMime.cpp file).

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Tuesday, March 7, 2017 4:59 PM
  • If you don't mind me asking, why are you trying to avoid using third-party libraries? It can't be the cost given that you already spend at least a few days on trying to make it work.

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Tuesday, March 7, 2017 5:01 PM
  • Hi Dmitry,

    Sorry for late reply. It had worked for me. Believe me I agree with you to reuse someone's effort. My client want to make some secure desktop mail and thats why he need everything in house. Some times, you need to follow the requirement. But thanks a lot for your help.


    Sunday, March 26, 2017 8:17 AM