none
How to display images into RichTextBox from Outlook message file(.MSG)? RRS feed

  • Question

  • Hi,

    I have an outlook message file , converting into stream using below code

    Stream messageStream = File.Open(MessageFileName, FileMode.Open, FileAccess.Read);

    Taking this stream into OutlookStorage.Message object as shown below.

    OutlookStorage.Message message = new OutlookStorage.Message(messageStream);

    I am displaying this message object body into RichTextBox.

    RichTextBox1.Rtf = message.BodyRTF;


    In RichTextBox1 am able to see only Text and formatted data wherin images are not displaying in richtextbox

    Instead of images displaying Image names like "Description: cid:image002.png@01CD86A2.E62E9990" .
    I have all images as outlook attachments. In message.BodyRTF i can see image  tags as
    <img width=229 height=262 id="Picture_x0020_2" src="cid:image002.png@01CD86A2.E62E9990"> .

    I want to display images as it is instead of image names. How can i?
    can any have source code for the implementing the same? or any suggestion would be greatly helpfull .


    Thanx and Regards, Ramesh Babu Morasa.

    • Moved by Chester Hong Tuesday, September 4, 2012 7:33 AM related to Outlook (From:Windows Forms General)
    Monday, September 3, 2012 6:23 AM

Answers

  • You cannot display images like that in in an RTF control unless you somehow convert them into embedded OLE objects.

    For you HTML message, the image is stored as a separate attachment and the HTML body refers to it by its content id (<img src="cid:...">).

    The easiest workaround would be to save the message in the MHTML format (MailItem.SaveAS(///, olMHTML)) and display the file in a browser control.


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

    • Proposed as answer by Leo_GaoModerator Wednesday, September 5, 2012 1:40 AM
    • Marked as answer by Ramesh Morasa Wednesday, September 5, 2012 5:15 AM
    Tuesday, September 4, 2012 5:42 PM
  • Do you mean attachments other than the images?

    They will not be saved.

    If you want to save the whole message, save it in the MSG format. You can then try to display it using the Windows preview handler (I have not tried that, but some people have).


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

    • Marked as answer by Ramesh Morasa Wednesday, September 5, 2012 6:47 AM
    Wednesday, September 5, 2012 6:28 AM

All replies

  • Hi,

    I have an outlook message file , converting into stream using below code

    Stream messageStream = File.Open(MessageFileName, FileMode.Open, FileAccess.Read);

    Taking this stream into OutlookStorage.Message object as shown below.

    OutlookStorage.Message message = new OutlookStorage.Message(messageStream);

    I am displaying this message object body into RichTextBox.

    RichTextBox1.Rtf = message.BodyRTF;


    In RichTextBox1 am able to see only Text and formatted data wherin images are not displaying in richtextbox

    Instead of images displaying Image names like "Description: cid:image002.png@01CD86A2.E62E9990" .
    I have all images as outlook attachments. In message.BodyRTF i can see image  tags as
    <img width=229 height=262 id="Picture_x0020_2" src="cid:image002.png@01CD86A2.E62E9990"> .

    I want to display images as it is instead of image names. How can i?
    can any have source code for the implementing the same? or any suggestion would be greatly helpfull .

    • Merged by Mike Feng Tuesday, September 4, 2012 7:49 AM duplicate
    Monday, September 3, 2012 6:14 AM
  • Hi Ramesh,

    According to your question, I would like to move your thread to Outlook for Developers forum.

    It may take some time to involve community members to your thread.

    Thanks for your understanding.

    Best regards,


    Chester Hong
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, September 4, 2012 7:31 AM
  • You cannot display images like that in in an RTF control unless you somehow convert them into embedded OLE objects.

    For you HTML message, the image is stored as a separate attachment and the HTML body refers to it by its content id (<img src="cid:...">).

    The easiest workaround would be to save the message in the MHTML format (MailItem.SaveAS(///, olMHTML)) and display the file in a browser control.


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

    • Proposed as answer by Leo_GaoModerator Wednesday, September 5, 2012 1:40 AM
    • Marked as answer by Ramesh Morasa Wednesday, September 5, 2012 5:15 AM
    Tuesday, September 4, 2012 5:42 PM
  • Hi Dmitry,

    Thank you for your valuable  reply.

    I am trying to save message in the MHTML format as you said (MailItem.SaveAS(///, olMHTML)) . please find below code for your reference.

    Microsoft.Office.Interop.Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application();

    Microsoft.Office.Interop.Outlook.MailItem item = (Microsoft.Office.Interop.Outlook.MailItem)app.Session.OpenSharedItem(@"D:\DragMe.MSG");

     item.SaveAs(@"D:\DragMe.MHTML", "olMHTML");

    While saving i am getting exception like "Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))"

    In SaveAs(...) method second parameter Type i had mentioned as string "olMHTML" am i going in right direction?

    Please help in this regard. If u have source code for the same that wouldbe greatly helpful to me.


    Thanx and Regards, Ramesh Babu Morasa.

    Wednesday, September 5, 2012 5:29 AM
  • Hi Dmitry,

    please ignore my earlier reply. I have modified code shown below.

    Microsoft.Office.Interop.Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application();

    Microsoft.Office.Interop.Outlook.MailItem item = (Microsoft.Office.Interop.Outlook.MailItem)app.Session.OpenSharedItem(@"D:\DragMe.MSG");

     item.SaveAs(@"D:\DragMe.MHTML",  Microsoft.Office.Interop.Outlook.OlSaveAsType.olMHTML);

    It is working fine but attachments in my mailitem displaying in MHTML like normal text. I want to display them as links and add attachments also to my MHTML file. How can i do this?


    Thanx and Regards, Ramesh Babu Morasa.

    Wednesday, September 5, 2012 5:46 AM
  • Do you mean attachments other than the images?

    They will not be saved.

    If you want to save the whole message, save it in the MSG format. You can then try to display it using the Windows preview handler (I have not tried that, but some people have).


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

    • Marked as answer by Ramesh Morasa Wednesday, September 5, 2012 6:47 AM
    Wednesday, September 5, 2012 6:28 AM
  • Thank you for your suggestion. When i save it in MSG format i can see the whole message as it is and when i opened that MSG file, it is opening in outlook new message window (COMPOSE MAIL) there am seeing SEND button, I don't want to show send button as i could not provide an option to send a mail instead i just need to show message content the user that is why am trying to convert into MHTML format.

    is it possible to disable SEND button only when i opened MSG file programatically without using Outlook add-ins?


    Thanx and Regards, Ramesh Babu Morasa.

    Wednesday, September 5, 2012 6:47 AM
  • Outlook Object Model would not let you reset the Sent bit on a standalone MSG file.

    You can either do it using Extended MAPI (C++ or Delphi only) or <plug> you can use Redemption - something along the lines below</plug>

    set Session = CreateObject("Redemption.RDOSession")
    Session.MAPIOBJECT = Application.Session.MAPIOBJECT
    set Msg = Session.GetMessageFromMsgFile("c:\temp\temp.msg")
    Msg.Sent = true
    Msg.save
    


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

    Friday, September 7, 2012 6:46 AM