⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testmapi.cpp

📁 A (hopefully) complete extended MAPI wrapper for WinXP, WinCE, and .NET.
💻 CPP
字号:
////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// File: TestMAPI.cpp
// Description: Simple program for testing CMapiEx
//
// Copyright (C) 2006, Noel Dillabough
//
// This source code is free to use and modify provided this notice remains intact and that any enhancements
// or bug fixes are posted to the CodeProject page hosting this class for the community to benefit.
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////

#include "TestMAPI.h"

// Some people requested a way to use MAPIEx as a library rather than a DLL
// uncomment out the next two lines and set MAPIExLib as a dependency instead of MAPIEx
//#undef AFX_EXT_CLASS
//#define AFX_EXT_CLASS

#include "MAPIEx/MAPIEx.h"
#include <conio.h>

// you may want to change these to valid settings before testing
#define FROM_NAME _T("Noel")
#define FROM_EMAIL _T("noel@nospam.com")
#define MSG_ATTACHMENT_FOLDER _T("c:\\temp")
#define MSG_ATTACHMENT _T("c:\\temp\\pic.jpg")
#define TO_EMAIL _T("noel@nospam.com")
#define TO_EMAIL2 _T("noel@nospam.com")
#define COPY_MSG_FOLDER _T("TestFolder")
#define PERSONAL_FOLDERS _T("Personal Folders")
#define CONTACTS_FOLDER _T("Contacts")

#ifdef UNICODE
#define PRINTF wprintf
#else
#define PRINTF printf
#endif

////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// To send a message:
//		-first open up a MAPI session and login
//		-then open the message store you want to send (NULL is the default store)
//		-then open the outbox
//		-create a new message, set its priority if you like 
//		-set its properties, recipients and attachments
//		-call send
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////

void SendTest(CMAPIEx& mapi)
{
	if(mapi.OpenOutbox()) {
		CMAPIMessage message;
		if(message.Create(&mapi,IMPORTANCE_LOW)) {
			message.SetSenderName(FROM_NAME);
			message.SetSenderEmail(FROM_EMAIL);
			message.SetSubject(_T("Subject"));

			// user SetBody for ANSI text, SetRTF for HTML and Rich Text
			message.SetRTF(_T("<html><body><font size=2 color=red face=Arial><span style='font-size:10.0pt;font-family:Arial;color:red'>Body</font></body></html>"));

			message.AddRecipient(TO_EMAIL);
			message.AddRecipient(TO_EMAIL2);
			message.AddAttachment(MSG_ATTACHMENT);

			if(message.Send()) PRINTF(_T("Sent Successfully\n"));
		}
	}
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// To receive a message:
//		-first open up a MAPI session and login
//		-then open the message store you want to send (NULL is the default store)
//		-then open the inbox and get the contents table
//		-iterate through the message using GetNextMessage() (sample below gets only unread messages)
//		-save attachments (if any) using SaveAttachments() if you like
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////

void ReceiveTest(CMAPIEx& mapi)
{
	if(mapi.OpenInbox() && mapi.GetContents()) {
		// use SortContents to sort the messages, default is ascending by PR_MESSAGE_DELIVERY_TIME
		mapi.SortContents(TABLE_SORT_DESCEND);

		CMAPIMessage message;
		while(mapi.GetNextMessage(message,TRUE)) {
			PRINTF(_T("Message from '%s' (%s) subject '%s', received: %s\n"),message.GetSenderName(),message.GetSenderEmail(),message.GetSubject(),message.GetReceivedTime(NULL));
			// use message.GetBody() to get the ANSI text body
			// use message.GetRTF() to get the RTF or decoded HTML email
			if(message.GetAttachmentCount()) {
				PRINTF(_T("saving attachments..."));
				message.SaveAttachments(MSG_ATTACHMENT_FOLDER);
				PRINTF(_T("done\n"));
			}
		}
	}
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// To iterate through folders:
//		-first open up a MAPI session and login
//		-then open the message store you want to send (NULL is the default store)
//		-then open the folder (probably inbox) and get the hierarchy table
//		-iterate through the folders using GetNextFolder() 
//		-OpenSubFolder() is a high level shortcut and will use Depth First Search to find a sub folder 
//		 also if you use this shortcut there is no need to RELEASE as it handles the folders internally
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////

void FolderTest(CMAPIEx& mapi)
{
	if(mapi.OpenInbox() && mapi.GetHierarchy()) {
		CString strFolder;
		LPMAPIFOLDER pSubFolder=NULL;
		do {
			RELEASE(pSubFolder);
			pSubFolder=mapi.GetNextSubFolder(strFolder);
			if(pSubFolder) {
				PRINTF(_T("Folder: %s\n"),strFolder);
			}
		} while(pSubFolder);
	}
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// This function creates a folder (opens if exists) and copies the first unread message if any to this folder
//
// To do this:
//		-first open up a MAPI session and login
//		-then open the message store you want to send (NULL is the default store)
//		-then open the folder (probably inbox) and get the contents table
//		-open the message you want to move
//		-create (open if exists) the folder you want to move to
//		-copy the message 
//
// You can also move and delete the message, but I wanted the sample to be non destructive just in case
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////

void CopyMessageTest(CMAPIEx& mapi)
{
	if(mapi.OpenInbox() && mapi.GetContents()) {
		CMAPIMessage message;
		if(mapi.GetNextMessage(message,TRUE)) {
			PRINTF(_T("Copying message from '%s' subject '%s'\n"),message.GetSenderName(),message.GetSubject());
			LPMAPIFOLDER pSubFolder=mapi.CreateSubFolder(COPY_MSG_FOLDER);
			if(pSubFolder) {
				mapi.CopyMessage(message,pSubFolder);
				RELEASE(pSubFolder);
			}

			// if you have a PST named "Personal Folders" this will copy the message there too
			CMAPIEx mapiPST;
			if(mapiPST.Login() && mapiPST.OpenMessageStore(PERSONAL_FOLDERS)) {
				pSubFolder=mapiPST.OpenRootFolder();
				if(pSubFolder) {
					mapi.CopyMessage(message,pSubFolder);
					RELEASE(pSubFolder);
				}
				mapiPST.Logout();
			}
		}
	}
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// These functions wait for a notification of a new message
//
// To do this:
//		-first open up a MAPI session and login
//		-then open the message store you want to send (NULL is the default store)
//		-call Notify with the callback and a context pointer (default ulEventMask is fnevNewMail)
//
// When a new mail arrives, the OnNewMessage function will be called with the context you passed in.  
// Note that since this test program is a single threaded console app I need to pump messages, usually in 
// a windows app this would be done automatically as part of the run loop.
//
// Obviously you'll have to send yourself a mail or two during this test, also note that the notification 
// will probably arrive a little while after the message arrives, depending on your exchange server's setup
// 
// You can also call Notify with a different even mask to capture say when the user deletes something etc
// 
////////////////////////////////////////////////////////////////////////////////////////////////////////////

long CALLBACK OnNewMessage(LPVOID lpvContext,ULONG cNotification,LPNOTIFICATION	lpNotifications)
{
	PRINTF(_T("Recieved New Message\n"));
	return 0;
}

void NotificationTest(CMAPIEx& mapi)
{
	if(mapi.Notify(OnNewMessage,NULL)) {
		PRINTF(_T("Waiting for a new incoming message... (hit a key to cancel)\n"));
		while(!_kbhit()) Sleep(100);
	}
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// This function displays your first contact in your Contacts folder
//
// To do this:
//		-first open up a MAPI session and login
//		-then open the message store you want to send (NULL is the default store)
//		-open your contacts folder (usually "Contacts" under the root)
//
// Use GetName to get the name (default DISPLAY NAME, but can be Initials, First Name etc) 
// Use GetEmail to get the email address (named property) 
// Use GetAddress to get the mailing address of CContactAddress::AddressType
// Use GetPhoneNumber supplying a phone number property (ie PR_BUSINESS_TELEPHONE_NUMBER)
// Use GetNotes to get the notes in either plain text (default) or RTF
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////

void ContactsTest(CMAPIEx& mapi)
{
	if(mapi.OpenRootFolder() && mapi.OpenSubFolder(CONTACTS_FOLDER) && mapi.GetContents()) {
		// sort by name (stored in PR_SUBJECT)
		mapi.SortContents(TABLE_SORT_ASCEND,PR_SUBJECT);

		CString strText;
		CMAPIContact contact;
		CContactAddress address;
		if(mapi.GetNextContact(contact)) {
			contact.GetName(strText);
			PRINTF(_T("Contact '%s'\n"),strText);
			contact.GetEmail(strText);
			PRINTF(_T("Email: '%s'\n"),strText);
			contact.GetAddress(address,CContactAddress::BUSINESS);
			PRINTF(_T("%s\n%s\n%s\n%s\n%s\n"),address.m_strStreet,address.m_strCity,address.m_strStateOrProvince,address.m_strCountry,address.m_strPostalCode);
			contact.GetPhoneNumber(strText,PR_BUSINESS_TELEPHONE_NUMBER);
			PRINTF("Phone: %s\n",strText);
			contact.GetNotes(strText,FALSE);
			PRINTF("Notes: %s\n",strText);
		}
	}
}

// this example works on unread messages, so send yourself a message and don't open it before trying this test
// If you have "autopreview" set turn it off to run this sample
void main(int argc,char* argv[])
{
	CMAPIEx mapi;
	if(!CMAPIEx::Init() || !mapi.Login() || !mapi.OpenMessageStore()) {
		PRINTF(_T("Failed to initialize MAPI\n"));
		return;
	}

	// you can use this profile name as an argument for Login() when no MAPI provider is open (ie "Outlook")
	PRINTF(_T("Profile Name: %s\n"),mapi.GetProfileName());

	SendTest(mapi);
	FolderTest(mapi);
	ReceiveTest(mapi);
	CopyMessageTest(mapi);
	NotificationTest(mapi);
	ContactsTest(mapi);

	mapi.Logout();
	CMAPIEx::Term();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -