CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Jan 2002
    Location
    India
    Posts
    72

    Reading email mesgs from the inbox

    How to read the msgs from the inbox?
    I need a detailed tutorial or example on this.
    Thankx


  2. #2
    Join Date
    May 1999
    Location
    G day Mayt Land
    Posts
    971

    Re: Reading email mesgs from the inbox

    Please if you managed to find a good code
    that reads your UNREAD emails from your mailbox
    please let me know..

    Share what you know and ask what you dont....
    Using Java version on windows 1.8_51

  3. #3
    Join Date
    Jan 2002
    Location
    India
    Posts
    72

    Re: Reading email mesgs from the inbox


    HINSTANCE hMAPILib;
    LHANDLE lhSession;
    ULONG err=0;
    hMAPILib = LoadLibrary("MAPI32.DLL"); //lpfnMAPISendDocuments = (LPMAPISENDDOCUMENTS)GetProcAddress(hMAPILib, "MAPISendDocuments");
    LPMAPILOGON lpfnMAPILogon;
    LPMAPIFINDNEXT lpfnMAPIFindNext ;
    LPMAPIREADMAIL lpfnMAPIReadMail;
    LPMAPILOGOFF lpfnMAPILogoff ;
    LPMAPIDETAILS lpfnMAPIDetails;
    LPMAPIFREEBUFFER lpfnMAPIFreeBuffer ;
    if (hMAPILib) { // loaded the DLL okay
    lpfnMAPILogon = (LPMAPILOGON)GetProcAddress(hMAPILib, "MAPILogon");
    lpfnMAPIFindNext = (LPMAPIFINDNEXT)GetProcAddress(hMAPILib, "MAPIFindNext");
    lpfnMAPIReadMail = (LPMAPIREADMAIL)GetProcAddress(hMAPILib, "MAPIReadMail");
    lpfnMAPILogoff = (LPMAPILOGOFF)GetProcAddress(hMAPILib, "MAPILogoff");
    lpfnMAPIDetails = (LPMAPIDETAILS)GetProcAddress(hMAPILib, "MAPIDetails");
    lpfnMAPIFreeBuffer = (LPMAPIFREEBUFFER)GetProcAddress(hMAPILib, "MAPIFreeBuffer");
    err = lpfnMAPILogon(0L, // ulUIParam; 0 always valid. First param
    NULL,// account name
    NULL, // No password needed.
    MAPI_LOGON_UI , // 0L for Use shared session.
    0L, // Reserved; must be 0.
    &lhSession); // Session handle.

    switch(err){
    case MAPI_E_FAILURE :
    AfxMessageBox(" MAPI_E_FAILURE : ");
    break;
    case MAPI_E_INSUFFICIENT_MEMORY :
    AfxMessageBox("MAPI_E_INSUFFICIENT_MEMORY ");
    break;
    case MAPI_E_LOGIN_FAILURE :
    AfxMessageBox("MAPI_E_LOGIN_FAILURE");
    break;
    case MAPI_E_TOO_MANY_SESSIONS :
    AfxMessageBox("MAPI_E_TOO_MANY_SESSIONS ");
    break;
    case MAPI_E_USER_ABORT :
    AfxMessageBox("MAPI_E_USER_ABORT ");
    break;
    default:
    AfxMessageBox(" SUCCESS_SUCCESS ");

    }
    if(err != SUCCESS_SUCCESS) {exit(0); }

    UINT index=0;
    while (index < 1) { //MapiRecipDesc recip;
    MapiMessage* message;
    CHAR rgchMsgID[513]; // Message IDs should be >= 512 CHARs + a null.
    ULONG uFindRet=SUCCESS_SUCCESS;
    // find the first unread message
    uFindRet = lpfnMAPIFindNext(lhSession, // explicit session required
    0L, // always valid ulUIParam
    NULL, // NULL specifies interpersonal messages
    NULL, // seed message ID; NULL=get first message
    MAPI_LONG_MSGID | // needed for 512 byte rgchMsgID.
    MAPI_UNREAD_ONLY, // only get unread messages.
    0L, // reserved; must be 0
    rgchMsgID);// buffer to get back a message ID.
    if (uFindRet == MAPI_E_NO_MESSAGES) // make sure a message was found
    {
    AfxMessageBox("No unread messages.\r\n");
    } else if (uFindRet != SUCCESS_SUCCESS) // make sure MAPIFindNext didn't fail
    {
    AfxMessageBox("Error while searching for messages\r\n");
    return;
    } // retrieve the message
    err = lpfnMAPIReadMail(lhSession, // Explicit session required.
    0L, // Always valid ulUIParam.
    rgchMsgID, // The message found by MAPIFindNext.
    MAPI_SUPPRESS_ATTACH, // TO DO: handle attachments.
    0L, // Reserved; must be 0.
    &message); // Location of the returned message.

    if(err != SUCCESS_SUCCESS) // Make sure MAPIReadMail succeeded.
    {
    AfxMessageBox("Error retrieving message");
    }
    // add data to ListCtrl




    This worked fine for me.
    Hope this helps.
    Please Rate if this helps u.



  4. #4
    Join Date
    May 1999
    Location
    G day Mayt Land
    Posts
    971

    Re: Reading email mesgs from the inbox

    Hello;

    Thanks for the responce'
    I have actually used that piece of code as well and it does work
    only if you have your outlook up and running....
    I managed to find another piece of code that does not requires
    outlook to be up .....check http://www.codeproject.com/useritems/mailcheck.asp
    Now Here is another question that I am throwing at you and hope you 'd be able
    to answer it.


    When I use OutLook Express and set my pos3 server values It does work but with
    Exchange it does not seem to work although the code is using MAPI

    Is your mail server by any chance a Pop3 server or is it not?

    And if it is not ,did you do any change in settings in MS/Outlook to
    get your emails through this Code?



    Thanks

    Share what you know and ask what you dont....
    Using Java version on windows 1.8_51

  5. #5
    Join Date
    Jan 2002
    Location
    India
    Posts
    72

    Re: Reading email mesgs from the inbox

    The code I posted works even if the Outlook express is not up.It asks for the user id and password for the user profile.
    I didnt change any settings for this.


  6. #6
    Join Date
    May 1999
    Location
    G day Mayt Land
    Posts
    971

    Re: Reading email mesgs from the inbox

    As you can see below , I m using exactly what you are doing.
    I have a toolkit that pumps emails to my account for testing.
    my app cant pick up the emails and it keeps tellig me that "On ReadMail MAPI_E_NO_MESSAGES"
    however when I bringup my Outlook Express , it manages to ftech the sent emails and then my app pickes them up...
    I can understand that this is perhaps something that you probably no nothing about...

    Thanks

    DWORD dwStyle= m_Listbox.SendMessage(LVM_GETEXTENDEDLISTVIEWSTYLE, 0 ,0);
    dwStyle |= LVS_EX_FULLROWSELECT;

    m_Listbox.SendMessage( LVM_SETEXTENDEDLISTVIEWSTYLE, 0,dwStyle );
    m_Listbox.SetBkColor(RGB(255,255,255));
    m_Listbox.SetTextColor(RGB(0,0,255));
    m_Listbox.SetTextBkColor(RGB(255,255,255));


    HINSTANCE hMAPILib;
    LPMAPILOGON lpfnMAPILogon;
    LPMAPIFINDNEXT lpfnMAPIFindNext ;
    LPMAPIREADMAIL lpfnMAPIReadMail;
    LPMAPILOGOFF lpfnMAPILogoff ;
    LPMAPIDETAILS lpfnMAPIDetails;
    LPMAPIFREEBUFFER lpfnMAPIFreeBuffer ;
    LHANDLE lhSession;
    ULONG err=0;
    hMAPILib = LoadLibrary("MAPI32.DLL"); //lpfnMAPISendDocuments = (LPMAPISENDDOCUMENTS)GetProcAddress(hMAPILib, "MAPISendDocuments");
    if (NULL == hMAPILib)
    {
    AfxMessageBox("Error 1/MAPI32.DLL "+AFX_IDP_FAILED_MAPI_LOAD);
    return 0;
    }

    lpfnMAPILogon = (LPMAPILOGON)GetProcAddress(hMAPILib, "MAPILogon");
    lpfnMAPIFindNext = (LPMAPIFINDNEXT)GetProcAddress(hMAPILib, "MAPIFindNext");
    lpfnMAPIReadMail = (LPMAPIREADMAIL)GetProcAddress(hMAPILib, "MAPIReadMail");
    lpfnMAPILogoff = (LPMAPILOGOFF)GetProcAddress(hMAPILib, "MAPILogoff");
    lpfnMAPIDetails = (LPMAPIDETAILS)GetProcAddress(hMAPILib, "MAPIDetails");
    lpfnMAPIFreeBuffer = (LPMAPIFREEBUFFER)GetProcAddress(hMAPILib, "MAPIFreeBuffer");
    err = lpfnMAPILogon(0L, // ulUIParam; 0 always valid. First param
    NULL,// account name
    NULL,// No password needed.
    MAPI_LOGON_UI , // 0L for Use shared session.
    0L, // Reserved; must be 0.
    &lhSession); // Session handle.
    err = lpfnMAPILogon(0L, // ulUIParam; 0 always valid.
    NULL, // account name
    NULL, // NULL, // No password needed.
    0L, // Use shared session.
    0L, // Reserved; must be 0.
    &lhSession); // Session handle.
    if(err != SUCCESS_SUCCESS)
    {
    AfxMessageBox("Logon Error");
    }

    UINT index=0;
    MapiMessage* message;

    while (index < 10000)
    {

    int flag;
    CTime today(CTime::GetCurrentTime());
    CString datetime=today.Format("%A%d%b%y%H%M%S");// 01Jan99HHMM.IXF
    char rgchMsgID[513]; // Message IDs should be >= 512 CHARs + a null.
    ULONG uFindRet=SUCCESS_SUCCESS; // find the first unread message
    uFindRet = lpfnMAPIFindNext(lhSession, // explicit session required
    0L, // always valid ulUIParam
    NULL, // NULL specifies interpersonal messages
    NULL, // seed message ID; NULL=get first message
    MAPI_LONG_MSGID | // needed for 512 byte rgchMsgID.
    MAPI_UNREAD_ONLY, // only get unread messages.
    0L, // reserved; must be 0
    rgchMsgID); // buffer to get back a message ID.


    switch(uFindRet)
    {

    case MAPI_E_FAILURE :
    //Add(index,datetime,"Error while searching","MAPI_E_FAILURE","..","..");
    // ignore nore for now
    lpfnMAPILogoff(lhSession,0L,0L,0L);
    log("On readMail MAPI_E_FAILURE");
    return index;
    break;

    case MAPI_E_INSUFFICIENT_MEMORY :
    Add2Ctrl(PROBLEM,datetime,"Error while searching","MAPI_E_INSUFFICIENT_MEMORY","..","..");
    lpfnMAPILogoff(lhSession,0L,0L,0L);
    log("On readMail MAPI_E_INSUFFICIENT_MEMORY");

    return index;
    break;

    case MAPI_E_INVALID_MESSAGE :
    Add2Ctrl(PROBLEM,datetime,"Error while searching","MAPI_E_INVALID_MESSAGE","..","..");
    lpfnMAPILogoff(lhSession,0L,0L,0L);
    log("On readMail MAPI_E_INVALID_MESSAGE");
    return index;
    break;

    case MAPI_E_INVALID_SESSION :
    Add2Ctrl(PROBLEM,datetime,"Error while searching","MAPI_E_INVALID_SESSION","..","..");
    lpfnMAPILogoff(lhSession,0L,0L,0L);
    log("On readMail MAPI_E_INVALID_SESSION");
    return index;
    break;

    case MAPI_E_NO_MESSAGES : // no new messages
    //Add(index,datetime,"Error while searching","MAPI_E_NO_MESSAGES","..","..");
    log("On ReadMail MAPI_E_NO_MESSAGES");
    lpfnMAPILogoff(lhSession,0L,0L,0L);
    return index;
    break;

    case SUCCESS_SUCCESS :
    break;

    }//switch
    err = lpfnMAPIReadMail(lhSession, // Explicit session required.
    0L, // Always valid ulUIParam.
    rgchMsgID, // The message found by MAPIFindNext.
    MAPI_SUPPRESS_ATTACH, // TO DO: handle attachments.
    0L, // Reserved; must be 0.
    &message); // Location of the returned message.

    if(err != SUCCESS_SUCCESS) // Make sure MAPIReadMail succeeded.
    {
    Add2Ctrl(PROBLEM,datetime,"Error retrieving message","..","..","..");
    }
    else
    { // add data to ListCtrl
    CString xx;
    xx.Format(_T("%s"),message->lpszNoteText);
    int ret;
    ret=Authenticate( message->lpOriginator->lpszAddress, message->lpszSubject,message->lpszNoteText);
    if(ret==OK)
    {

    OnSend(xx);
    // CString yy;
    // yy.Format(_T("This is a confirmation to your request :\n%s\n\nreceived on :%s"
    // "\n\nYour request has been started."
    // "\n\nThank you"),message->lpszNoteText,message->lpszDateReceived);
    // CString subject="Confirmation Re:";
    // subject+=message->lpszSubject;
    //
    // Mailto("IVRSOS Listener",message->lpOriginator->lpszAddress,subject,yy);
    sList.AddTail(message->lpOriginator->lpszAddress);

    }


    flag=ret;

    if(flag==OK)good++; else bad++;

    }//else
    Add2Ctrl(flag,
    message->lpszDateReceived,
    message->lpOriginator->lpszName,
    message->lpOriginator->lpszAddress,
    message->lpszSubject,
    message->lpszNoteText);

    lpfnMAPIFreeBuffer(message);
    index++;
    Sleep(10);
    } //while

    // lpfnMAPILogoff(lhSession, // The session.
    // 0L, // 0 always valid for ulUIParam.
    // 0L, // No logoff flags.
    // 0L); // Reserved; must be 0.

    return index;
    }






    Share what you know and ask what you dont....
    Using Java version on windows 1.8_51

  7. #7
    Join Date
    Jan 2002
    Location
    India
    Posts
    72

    Re: Reading email mesgs from the inbox

    You reading the msgs two times.That is why u r getting that msg as no unread msgs.


    /******************************************/
    /***This block is used to find the unread msgs.AFter executing this block , the msgs will become read.But again u r reading the msgs using shared session, by that time,msgs were already read.That is the mistake u r doing.***/

    err = lpfnMAPILogon(0L,
    // ulUIParam; 0 always valid. First param
    NULL,// account name
    NULL,// No password needed.
    MAPI_LOGON_UI , // 0L for Use shared
    session.
    0L, // Reserved; must be
    0.
    &lhSession); // Session handle.
    /******************************************/


    /*************This block is reading again which is a problem in ur code.Remove this blk,and it will work fine.************/
    err = lpfnMAPILogon(0L,
    // ulUIParam; 0 always valid.
    NULL, // account name
    NULL, // NULL, // No
    password needed.
    0L, // Use shared session.
    0L, // Reserved; must be 0.
    &lhSession); // Session handle.
    /*************This block is reading again which is a problem in ur code.Remove this blk,and it will work fine.************/




    Pl rate if this helps u


  8. #8
    Join Date
    May 1999
    Location
    G day Mayt Land
    Posts
    971

    Re: Reading email mesgs from the inbox

    Thanks-That is a bug I missed , but that did not change the story.still it comes up with
    2002-3-26 11:19:9 On ReadMail MAPI_E_NO_MESSAGES
    2002-3-26 11:24:18 lpfnMAPILogon was SUCCESS_SUCCESS
    2002-3-26 11:24:18 On ReadMail MAPI_E_NO_MESSAGES

    The **** thing works when Outlook express is up..

    Thansk and cheers


    Share what you know and ask what you dont....
    Using Java version on windows 1.8_51

  9. #9
    Join Date
    Oct 2010
    Posts
    6

    Re: Reading email mesgs from the inbox

    See DCI Mail Processor API sample:

    //texttList for retrive mailboxesList from Mail Processor
    TMString textList;
    try
    {
    //get all registered mailboxes location
    GetAllMailboxes(&textList);
    //parse mailbox list from text to string array
    vector <string> mailboxesList = Split(textList, "\n\r");
    //clean empty lines from mailboxesList
    int i = 0;
    while (i != mailboxesList.size())
    {
    if (mailboxesList[i].size() == 0) { mailboxesList[i].erase(); }
    else
    { //Write mailbox list on the console
    cout << i + 1 << ")" << mailboxesList[i].data() << endl;
    i++;
    }
    }

    cout << "\nPlease enter mailbox number and press enter: ";
    int mailboxNumber;
    scanf( "&#37;d", &mailboxNumber ); //get mailbox number
    mailboxNumber--; //change mailbox number to mailboxesList item index

    string path = "C:\\MailStore"; //prepare folder to save objects
    CreateDirectoryA(path.data(), 0);
    path += "\\";

    cout << "Save mail messages from " << mailboxesList[mailboxNumber].data() << endl;

    int totalCount = 0;

    TMString mailboxName;
    strcpy_s(mailboxName, 4016, mailboxesList[mailboxNumber].data());

    DWORD mailboxHandle = OpenMailbox(&mailboxName); //open mailbox file


    if (mailboxHandle > OMS_NOERROR) //if mailbox is opened
    {
    //select the object type to save
    cout << "Mailbox is open, now choose object type to save:" << endl;
    cout << " 1) mail messages" << endl;
    cout << " 2) mail messages text only" << endl;
    cout << " 3) attached files" << endl;
    cout << "Please enter object type number and press enter: ";

    int objectType;
    scanf( "%d", &objectType); //get object type number

    int result = OMS_NOERROR;
    TMString objectPath;
    CHAR szTemp[32];
    CHAR objectPathtemp[4016];


    for (DWORD folderCount = 0; folderCount < GetFolderCount(mailboxHandle); folderCount++)//enumerate folders
    for (DWORD mailCount = 0; mailCount < GetMailCount(mailboxHandle, folderCount); mailCount++) //enumerate mail messages inside the mailbox
    {
    strcpy_s(objectPathtemp, 4016, path.data());

    itoa(mailboxHandle, szTemp, 10);
    strcat(objectPathtemp, szTemp);
    strcat(objectPathtemp, "_");

    itoa(folderCount, szTemp, 10);
    strcat(objectPathtemp, szTemp);
    strcat(objectPathtemp, "_");

    itoa(mailCount, szTemp, 10);
    strcat(objectPathtemp, szTemp);

    //Save objects to files - mail messages, mail messages as text or attached files only

    switch (objectType)
    {
    case 1:
    strcpy_s(objectPath, 4016, objectPathtemp);
    result = SaveMailToFile(mailboxHandle, folderCount, mailCount, &objectPath);
    break;
    case 2:
    strcat(objectPathtemp, ".txt");
    strcpy_s(objectPath, 4016, objectPathtemp);
    result = SaveMailToFileAs(mailboxHandle, folderCount, mailCount, &objectPath, 2);
    break;
    case 3:
    strcpy_s(objectPath, 4016, path.data());
    result = SaveMailToFileAs(mailboxHandle, folderCount, mailCount, &objectPath, 4);
    break;
    default: //if bad object type number - break
    cout << "Bad object type.";
    result = OMS_BADMESSAGE;
    break;
    }

    if (result == OMS_OK) { totalCount++; }
    if (result == OMS_MAILBOXSDKNOTREGISTER) //check Engine registration
    {
    cout << "You use unregistered Mail Processor SDK copy.";
    break;
    }
    if (result == OMS_BADMESSAGE) { break; }
    cout << "Processed objects count: " << totalCount << "\r";
    }
    CloseMailbox(mailboxHandle);
    }
    else
    cout << "Selected mailbox open problem, code: " + mailboxHandle; //if can't open selected mailbox
    }
    catch (exception &e)
    {
    cout << "Application execute problem: " << e.what();
    }
    cout << "Press enter key to close this application.";

    getchar();
    return 0;

  10. #10
    Join Date
    May 1999
    Location
    G day Mayt Land
    Posts
    971

    Re: Reading email mesgs from the inbox

    After 11 years, Thanks to dot net and c sharp , bye bye cpp

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured