Receiving an E-mail Message
| Nik Okuntseff |
MS Exchange Server Programming |
Receiving an E-mail Message
Retrieving mail from your Inbox with CDO is also easy. Here is my illustration
code (CDO/ReceiveMail):
void CReceiveMailDlg::OnReceive()
{
try
{
// Initialize session object
Session session;
BOOL bResult = session.CreateDispatch("MAPI.Session");
ASSERT(bResult);
// Logon
VARIANT vResult;
VariantInit(&vResult);
session.InvokeHelper(0x77,
DISPATCH_METHOD, VT_VARIANT, (void *) &vResult, NULL);
VariantClear(&vResult);
// Get Inbox
VARIANT vInbox = session.GetInbox();
Folder Inbox(vInbox.pdispVal);
ASSERT(Inbox.m_lpDispatch);
// Get Messages collection
VARIANT vMessages = Inbox.GetMessages();
Messages messages(vMessages.pdispVal);
ASSERT(messages.m_lpDispatch);
// Get first message
VARIANT vMessage;
VariantInit(&vMessage);
messages.InvokeHelper(0x6d,
DISPATCH_METHOD, VT_VARIANT, (void *) &vMessage, NULL);
Message message(vMessage.pdispVal);
// Get subject
VARIANT vSubject = message.GetSubject();
CString strSubject(vSubject.bstrVal);
VariantClear(&vSubject);
// Get text
VARIANT vText = message.GetText();
CString strText(vText.bstrVal);
VariantClear(&vText);
// Display a message box
with results.
CString str = "Received
message. Subject: " +
strSubject + ". Text: " + strText + ".";
::MessageBox(NULL, str,
"Success", MB_OK | MB_ICONINFORMATION);
session.Logoff();
// Cleanup
message.ReleaseDispatch();
messages.ReleaseDispatch();
Inbox.ReleaseDispatch();
}
catch (...)
{
::MessageBox(NULL, "Exception
raised...", "Error", MB_OK | MB_ICONINFORMATION);
}
}
The beginning is standard: I create the session and do logon to underlying
MAPI. Then I open the Inbox, navigate to the Messages collection and obtain
the first message in there (you must have at least one message in your
Inbox for this code to work). Then I retrieve message subject and text
and display them.
This code, as well as all other MFC examples in this chapter assume
that COM is properly initialized. All these samples are trivial dialog
based MFC applications. I have put the CoInitialize call before displaying
the modal dialog and corresponding CoUninitialize after the DoModal call.
Although the class collection in the project may at first seem quite rich,
the following simple procedure had been used to create the app:
-
Create an MFC dialog based project.
-
Use ClassWizard to import classes from olemsg32.dll.
-
Add a custom button on the dialog and map a handler to it.
-
Add #include <afxdisp.h> to stdafx.h file.
-
Add CoInitialize(NULL) before displaying the dialog and CoUninitialize()
after.
-
Add code to your handler as illustrated above.
Obviously, this demo code (and other samples for this book) lacks proper
error handling. Thus, it is not acceptable for production. This is because
the book's primary goal is to illustrate possibilities rather than making
samples robust and inevitably less readable.
I have shown how you can use CDO with C++. The two ways that I
have described in this chapter (either using IDispatch interface directly
or ClassWizard generated COleDispatchDriver derived classes) require some
knowledge of Automation (how to handle VARIANTs, for example). Although
it is possible to cut and paste code from my or somebody else's samples,
modify them slightly and make simple things working, it is unlikely that
a serious CDO C++ development would be possible without familiarity with
Automation. In this context, perhaps, one could find Visual Basic, and
even Visual Basic Script to be better choices because the development platform
handles the majority of problems.
[ Contents |
Home
]
Send comments and suggestions to niko@wrconsulting.com
Copyright © 1997-1998 by Nik Okuntseff
|