Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

parsing address fields

Help
2005-04-19
2013-04-25
  • Hi,

        I am parsing a received email using the getHeader()->From() method, and I am getting a result something like this:

    From: =?us-ascii?Q?John_Doe?= <jdoe@example.com>

        I am wondering if there is a way with vmime that I can parse out the name and the email address ?  I looked at the mailboxField object but it doesn't seem to have any methods for that.

       Also, why am I getting the "?us-ascii?" stuff ? Is it because I set the charset improperly ?

    --Rob

     
    • Hello,

      The method "getHeader()->From()" returns an object of type "mailboxField". Using this object, you can retrieve the value, which is of type "mailbox":

      ------------------------------------------------------------------------------------------
      const vmime::mailboxField& fromField = msg.getHeader()->From();
      const vmime::mailbox& from = fromField.getValue();

      // Name is in "from.getName()"
      // Email is in "from.getEmail()"
      ------------------------------------------------------------------------------------------

      The "us-ascii" charset is set because "John Doe" contains only 7-bit chars, but also contains a space character.

      The charset is specified when you create a "vmime::word" object (for use with "vmime::text"). Please read documentation.

      If this isn't the answer you wanted, please explain your problem with more precision.

      Regards,

      -- Vincent

       
      • Andrew Whalan
        Andrew Whalan
        2007-09-14

        I'm guessing the API has changed, or I just don't get how to access headers properly, I'm getting:

        awhalan@omni:~/projects/pami$ g++ `pkg-config --cflags --libs vmime` -o pami main.cpp
        main.cpp: In function ‘int main()’:
        main.cpp:51: error: ‘const class vmime::headerFieldValue’ has no member named ‘getName’
        main.cpp:53: error: ‘const class vmime::headerFieldValue’ has no member named ‘getEmail’

        The code in question is:

            vector< ref<net::message> > msgs = inbox->getMessages();
            inbox->fetchMessages(msgs, net::folder::FETCH_ENVELOPE);   

            ref <net::message> m;
            ref <const header> h;
           
            std::string s;
            for (vector< ref<net::message> >::iterator it = msgs.begin(); it != msgs.end(); it++) {
                m = *it;
                h = m->getHeader();
                s = h->From()->getValue()->getName();
                s += " ";
                s += h->From()->getValue()->getEmail();
                cout << "Msg#: " << m->getUniqueId() << " \t " << s << endl;
            }

        How should I be doing it? Sorry if I'm a retard, but i couldn't find anything in the vmime-book that seemed to help.

         
        • > main.cpp:51: error: ‘const class vmime::headerFieldValue’ has no member named ‘getName’

          That's right. You have to dynamic-cast this to the appropriate field value type.
          I know this is not very clear, and should be refactored in a future version.

          Try this:

             s = h->From()->getValue()->dynamicCast <vmime::mailbox>()->getName();

          Vincent

           
  • Casey Harris
    Casey Harris
    2011-01-17

    Hi, I am in the same boat.

    I have tried :

    vmime::ref <vmime::net::message> msg;

    ….

         string LocalEmail(msg->getHeader()->From()->getValue()->dynamicCast <vmime::mailbox>()->getEmail());

    but the error I get is:

    MailProcess.cpp:862: error: âconst class vmime::headerFieldValueâ has no member named âdynamicCastâ
    MailProcess.cpp:862: error: expected primary-expression before â>â token
    MailProcess.cpp:862: error: expected primary-expression before â)â token

    What is one supposed to do?

    TIA

     
  • Hello,

    The right syntax is:

    ...getValue().dynamicCast...
    

    instead of:

    ...getValue()->dynamicCast...
    

    Vincent

     
  • for the record, it should actually be cast to a const mailbox, like this :

    header->From()->getValue().dynamicCast<const vmime::mailbox>()->getEmail();