Link to home
Start Free TrialLog in
Avatar of MyName1
MyName1

asked on

Display Subject field of an eml file

I have eml files which are QP encoded and charset is ISO 8859-2. I want to display the Subject and Body of the message as message box on a Polish desktop. How can acheive this using CDO ?
If I use GetDecodedContentStream().ReadText. The text that is displayed is not proper and some of the characters look like control characters.
Basically my application is a POP3 application which downloads messages as .eml and then displays only the subject and Body of message as a text box.

Any help is appreciated.
Avatar of zahaby
zahaby

try to use
ContentTransferEncoding
A message body that contains content other than plain US-ASCII text is subdivided into parts. Examples of body parts include text, attachments, inline Hypertext Markup Language (HTML) pages, alternative representations of HTML pages, and so on. The IBodyPart interface defines a set of abstract methods and properties that you can use to manipulate message body parts.

Body parts are arranged hierarchically in a message that is formatted in MIME; for example, a message that contains only text and attachments has a two-level hierarchy—the first level includes the message content and the second level contains each attachment. The IBodyPart interface supports such hierarchies by exposing the BodyParts property. Navigating down one level from an implementing object returns a collection of objects, each of which exposes the IBodyPart interface.

Each object that exposes the IBodyPart interface provides access to content in serialized format using the GetDecodedContentStream or GetEncodedContentStream methods. Both methods return an ADO Stream object (exposing a _Stream interface) that contains the content in decoded or encoded format, respectively. Content encoding depends on whether the message body is formatted using MIME or Uuencode. This operation is controlled at the Message object level using the IMessage.MIMEFormatted property.

You can also use the GetStream method to return the entire body part and all sub-parts in serialized, encoded format. For messages formatted in MIME, this stream contains the mail headers (such as Content-Type) and the content encoded using the mechanism specified by the IBodyPart.ContentTransferEncoding property.
ASKER CERTIFIED SOLUTION
Avatar of zahaby
zahaby

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MyName1

ASKER

This is what im doing in a sample vbs file

    Set Stm = CreateObject("ADODB.Stream")
    Stm.Open
    Stm.LoadFromFile "C:\\test.eml"
    Set iMsg = CreateObject("CDO.Message")
      
    Set iDsrc = iMsg.DataSource
    iDsrc.OpenObject Stm, "_Stream"
'MsgBox "Subject : " + iMsg.Subject
'MsgBox "Body : " + iMsg.BodyPart.GetDecodedContentStream().ReadText()
Set fso = CreateObject("Scripting.FileSystemObject")
Set f1 = fso.CreateTextFile("c:\testfile.txt", True, True)
f1.WriteLine("Subject :" + iMsg.Subject)
llngLength = LenB(iMsg.Subject)
     For llngIndex = 1 To llngLength
          llngAscii = AscB(MidB(iMsg.Subject, llngIndex, 1))
          lstrUnicode = lstrUnicode + Chr(lLngAscii)
     Next
f1.WriteLine(lstrUnicode )
f1.Close()




The eml file has this as the subject
test ąćęłńóśźż ĄĆĘŁŃÓŚŹŻ test

and in my output i get it as
test ¹æê³ñ󜟿 ¥ÆÊ£ÑÓŒ¯ test

and unicode output as
t e s t   ¹ æ ê ³ ñ ó Sx¿   ¥ Æ Ê £ Ñ Ó R ¯   t e s t



What is going on ?

Avatar of MyName1

ASKER

the eml file has subject as "test ąćęłńóśźż ĄĆĘŁŃÓŚŹŻ test"
Avatar of MyName1

ASKER

Ok I got the vbscript to work. I had to use the fields property and then urn:schemas:httpmail:normalizedSubject. I dont like it.

Now I tried to use the same in C++. Im trying to display the From field which should be in unicode because im using
"urn:schemas:httpmail:From" field. Do you know if there is any bug in CDO ? As per documentation all these fields should be in unicode. Only the normalizedSubject and TextDescription fields are in Unicode the rest of it in ASCII.

I dont get it why and this is the code im using..

CoInitialize(NULL);
  _StreamPtr  pStm(__uuidof(Stream));

      _variant_t varOptional(DISP_E_PARAMNOTFOUND,VT_ERROR);

      try {

            pStm->Open(

                            varOptional,

                            adModeUnknown,

                            adOpenStreamUnspecified,

                            _bstr_t(""),

                            _bstr_t(""));

                                    pStm->Charset = "UTF-8";

                                   

            pStm->LoadFromFile("C:\\test.eml");

      }

      catch(_com_error e)

      {

                          AfxMessageBox(e.ErrorMessage());

      }

 

      IMessagePtr iMsg(__uuidof(Message));

      IDataSourcePtr iDsrc;

      iDsrc = iMsg;

 

      try {

            iDsrc->OpenObject(pStm,_bstr_t("_Stream"));

      }

      catch(_com_error e)

      {

                          AfxMessageBox(e.ErrorMessage());

      }

 
                                    Field* p;

                                    iMsg->Fields->get_Item(_variant_t("urn:schemas:httpmail:From"), &p);

                         _variant_t t;

                         p->get_Value(&t);


                         ::MessageBoxW(NULL,t.bstrVal, t.bstrVal, MB_OK);

:) great
thanks for informations
Avatar of MyName1

ASKER

No man. you got it wrong. it only works in VBScript but in C++ never gives unicode although MSDN documentation says if you use
urn:schemas:httpmail
it should always return unicode..

any ideas ?
Avatar of MyName1

ASKER

Ok. The problem was with the CDOSys.dll I had on that machine. I installed SP4 and everything works fine now.
zahaby , just for being the only one who was interested in answering I want to give you 200 points. But i dont know how to do it. Moderator ??

Thanks...
Avatar of MyName1

ASKER

I already added this comment
"Ok. The problem was with the CDOSys.dll I had on that machine. I installed SP4 and everything works fine now.
zahaby , just for being the only one who was interested in answering I want to give you 200 points. But i dont know how to do it. Moderator ??
"
thanks very much