📄 crobotmail.cpp
字号:
////////////////////////////////////////////////////////////////////
//
// CRobotMail.cpp - CRobotMail class implementation
//
// Source: "Programming Bots, Spiders, and Intelligent Agents
// in Microsoft Visual C++"
//
// Copyright (c) 1999, David Pallmann. All rights reserved.
#include <stdafx.h>
#include <mapi.h>
#include "CRobotMail.h"
// *************************
// * MAPI function calls *
// *************************
ULONG (PASCAL *lpfnMAPISendMail)(ULONG,
ULONG,
MapiMessage*,
FLAGS,
ULONG);
ULONG (PASCAL *lpfnMAPIResolveName)(LHANDLE,
ULONG,
LPTSTR,
FLAGS,
ULONG,
MapiRecipDesc **);
ULONG (FAR PASCAL *lpfnMAPILogon)(ULONG,
LPSTR,
LPSTR,
FLAGS,
ULONG,
LPLHANDLE);
ULONG (FAR PASCAL *lpfnMAPILogoff)(LHANDLE, ULONG, FLAGS, ULONG);
ULONG (FAR PASCAL *lpfnMAPIFreeBuffer)(LPVOID);
ULONG (FAR PASCAL *lpfnMAPIAddress)(LHANDLE,
ULONG,
LPSTR,
ULONG,
LPSTR,
ULONG,
MapiRecipDesc *,
FLAGS,
ULONG,
LPULONG,
MapiRecipDesc **);
ULONG (FAR PASCAL *lpfnMAPIFindNext)(LHANDLE,
ULONG,
LPSTR,
LPSTR,
FLAGS,
ULONG,
LPSTR);
ULONG (FAR PASCAL *lpfnMAPIReadMail)(LHANDLE,
ULONG,
LPSTR,
FLAGS,
ULONG,
lpMapiMessage FAR *lppMessage);
// *****************
// * Constructor *
// *****************
CRobotMail::CRobotMail()
{
// Load Mapi32.dll and compute function addresses
m_hInstMail = ::LoadLibrary("Mapi32.dll");
if(m_hInstMail == NULL)
{
AfxMessageBox("CRobotMail: unable to load MAPI32.dll");
m_bInitialized = false;
return;
} // End if
/* Find the addresses of functions and store them in function
pointer variables */
(FARPROC&)lpfnMAPILogon = GetProcAddress(m_hInstMail,
"MAPILogon");
if (lpfnMAPILogon == NULL)
{
AfxMessageBox("CRobotMail: could not find "
"the MAPILogon function");
return;
} // End if
(FARPROC&)lpfnMAPIFindNext = GetProcAddress(m_hInstMail,
"MAPIFindNext");
if (lpfnMAPIFindNext == NULL)
{
AfxMessageBox("CRobotMail: could not find "
"the MAPIFindNext function");
return;
} // End if
(FARPROC&)lpfnMAPIReadMail = GetProcAddress(m_hInstMail,
"MAPIReadMail");
if (lpfnMAPIReadMail == NULL)
{
AfxMessageBox("CRobotMail: could not find "
"the MAPIReadMail function");
return;
} // End if
(FARPROC&)lpfnMAPIFreeBuffer = GetProcAddress(m_hInstMail,
"MAPIFreeBuffer");
if (lpfnMAPIFreeBuffer == NULL)
{
AfxMessageBox("CRobotMail: could not find "
"the MAPIFreeBuffer function");
return;
} // End if
(FARPROC&)lpfnMAPIResolveName = GetProcAddress(m_hInstMail,
"MAPIResolveName");
if (lpfnMAPIResolveName == NULL)
{
AfxMessageBox("CRobotMail: could not find "
"the MAPIResolveName function");
return;
} // End if
(FARPROC&)lpfnMAPISendMail = GetProcAddress(m_hInstMail,
"MAPISendMail");
if (lpfnMAPISendMail == NULL)
{
AfxMessageBox("CRobotMail: could not find "
"the MAPISendMail function");
return;
} // End if
// Log on to existing session
ULONG lResult = lpfnMAPILogon(0,
NULL, // sProfileName
NULL, // sPassword
0, // flFlags
0,
&m_lhSession);
if (lResult != SUCCESS_SUCCESS)
return; // Logon failed
m_bInitialized = true;
}
// ************************
// * *
// * GetNewMessageCount *
// * *
// ************************
// Description: Returns a count of the number of
// unread messages in the inbox
int CRobotMail::GetNewMessageCount()
{
if (!m_bInitialized) return 0;
int nCount = 0;
// Find the first message
ULONG lResult = lpfnMAPIFindNext(m_lhSession,
NULL,
NULL,
NULL,
MAPI_LONG_MSGID | MAPI_UNREAD_ONLY,
0,
m_pMessageID);
while (lResult == SUCCESS_SUCCESS)
{
nCount++;
lResult = lpfnMAPIFindNext(m_lhSession,
NULL,
NULL,
m_pMessageID,
MAPI_LONG_MSGID | MAPI_UNREAD_ONLY,
0,
m_pMessageID);
}
return nCount;
}
// *********************
// * *
// * GetMessageCount *
// * *
// *********************
// Description: Returns a count of the number of messages
// in the inbox (both read and unread)
int CRobotMail::GetMessageCount()
{
if (!m_bInitialized) return 0;
int nCount = 0;
// Find the first message
ULONG lResult = lpfnMAPIFindNext(m_lhSession,
NULL,
NULL,
NULL,
MAPI_LONG_MSGID,
0,
m_pMessageID);
while (lResult == SUCCESS_SUCCESS)
{
nCount++;
lResult = lpfnMAPIFindNext(m_lhSession,
NULL,
NULL,
m_pMessageID,
MAPI_LONG_MSGID,
0,
m_pMessageID);
}
return nCount;
}
// *********************
// * *
// * FirstNewMessage *
// * *
// *********************
// Description: Moves to the first unread message in the inbox
BOOL CRobotMail::FirstNewMessage(BOOL bMarkAsRead)
{
if (!m_bInitialized) return false;
m_bNewOnly = true;
// Find the first unread message
ULONG lResult = lpfnMAPIFindNext(m_lhSession,
NULL,
NULL,
NULL,
MAPI_LONG_MSGID | MAPI_UNREAD_ONLY,
0,
m_pMessageID);
if (lResult != SUCCESS_SUCCESS)
return false;
// Read the message (that was found by MAPIFindNext)
long nFlags = MAPI_SUPPRESS_ATTACH;
if (!bMarkAsRead)
nFlags = nFlags | MAPI_PEEK;
lResult = lpfnMAPIReadMail(m_lhSession,
NULL,
m_pMessageID,
nFlags,
0,
&m_pMessage);
if (lResult != SUCCESS_SUCCESS)
return false;
m_sFrom = CString(m_pMessage->lpOriginator->lpszName);
m_sFromAddress = CString(m_pMessage->lpOriginator->lpszAddress);
m_sDate = CString(m_pMessage->lpszDateReceived);
m_sSubject = CString(m_pMessage->lpszSubject);
m_sMessage = CString(m_pMessage->lpszNoteText);
if (m_pMessage->flFlags & MAPI_UNREAD)
m_bRead = true;
else
m_bRead = false;
// Deallocate the memory for the message
lpfnMAPIFreeBuffer(m_pMessage);
return true;
}
// ******************
// * *
// * FirstMessage *
// * *
// ******************
// Description: Moves to the first message in the inbox
BOOL CRobotMail::FirstMessage(BOOL bMarkAsRead)
{
if (!m_bInitialized) return false;
m_bNewOnly = false;
// Find the first message
ULONG lResult = lpfnMAPIFindNext(m_lhSession,
NULL,
NULL,
NULL,
MAPI_LONG_MSGID,
0,
m_pMessageID);
if (lResult != SUCCESS_SUCCESS)
return false;
// Read the message (that was found by MAPIFindNext)
long nFlags = MAPI_SUPPRESS_ATTACH;
if (!bMarkAsRead)
nFlags = nFlags | MAPI_PEEK;
lResult = lpfnMAPIReadMail(m_lhSession,
NULL,
m_pMessageID,
nFlags,
0,
&m_pMessage);
if (lResult != SUCCESS_SUCCESS)
return false;
m_sFrom = CString(m_pMessage->lpOriginator->lpszName);
m_sFromAddress = CString(m_pMessage->lpOriginator->lpszAddress);
m_sDate = CString(m_pMessage->lpszDateReceived);
m_sSubject = CString(m_pMessage->lpszSubject);
m_sMessage = CString(m_pMessage->lpszNoteText);
if (m_pMessage->flFlags & MAPI_UNREAD)
m_bRead = true;
else
m_bRead = false;
// Deallocate the memory for the message
lpfnMAPIFreeBuffer(m_pMessage);
return true;
}
// *****************
// * *
// * NextMessage *
// * *
// *****************
// Description: Returns a count of the number
// of messages (both read and unread)
BOOL CRobotMail::NextMessage(BOOL bMarkAsRead)
{
if (!m_bInitialized) false;
if (!m_bInitialized) return false;
// Find the next message
long nFlags = MAPI_LONG_MSGID;
if (m_bNewOnly)
nFlags = nFlags | MAPI_UNREAD_ONLY;
ULONG lResult = lpfnMAPIFindNext(m_lhSession,
NULL,
NULL,
m_pMessageID,
nFlags,
0,
m_pMessageID);
if (lResult != SUCCESS_SUCCESS)
return false;
// Read the message (that was found by MAPIFindNext)
nFlags = MAPI_SUPPRESS_ATTACH;
if (!bMarkAsRead)
nFlags = nFlags | MAPI_PEEK;
lResult = lpfnMAPIReadMail(m_lhSession,
NULL,
m_pMessageID,
nFlags,
0,
&m_pMessage);
if (lResult != SUCCESS_SUCCESS)
return false;
m_sFrom = CString(m_pMessage->lpOriginator->lpszName);
m_sFromAddress = CString(m_pMessage->lpOriginator->lpszAddress);
m_sDate = CString(m_pMessage->lpszDateReceived);
m_sSubject = CString(m_pMessage->lpszSubject);
m_sMessage = CString(m_pMessage->lpszNoteText);
if (m_pMessage->flFlags & MAPI_UNREAD)
m_bRead = true;
else
m_bRead = false;
// Deallocate the memory for the message
lpfnMAPIFreeBuffer(m_pMessage);
return true;
}
// *****************
// * *
// * SendMessage *
// * *
// *****************
// Description: Sends a message
BOOL CRobotMail::SendMessage(CString sTo,
CString sSubject,
CString sMessage,
CString sAttachment)
{
if (!m_bInitialized) return false;
char recipient[512];
char subject[512];
char path[512];
char filename[512];
char text[5000];
CString sPath, sFilename;
MapiFileDesc m_FileInfo;
memset(&m_message, 0, sizeof(m_message));
m_message.ulReserved = 0;
m_message.lpszMessageType = NULL;
strcpy(subject, sSubject);
m_message.lpszSubject = subject;
strcpy(text, sMessage);
m_message.lpszNoteText = text;
m_message.flFlags = MAPI_SENT;
m_message.lpOriginator = NULL;
m_message.nRecipCount = 1;
strcpy(recipient, sTo);
ULONG lResult = lpfnMAPIResolveName(m_lhSession,
0,
recipient,
0,
0,
&m_message.lpRecips);
if (lResult != SUCCESS_SUCCESS)
return false;
if (sAttachment == "")
m_message.nFileCount = 0;
else
{
int nPos = sAttachment.ReverseFind('\\');
if (nPos == -1)
{
sPath = sAttachment;
sFilename = sAttachment;
} // End if
else
{
sPath = sAttachment;
sFilename = sAttachment.Mid(nPos + 1);
} // End else
strcpy(path, sPath);
strcpy(filename, sFilename);
m_message.nFileCount = 1;
m_FileInfo.ulReserved = 0;
m_FileInfo.flFlags = 0;
m_FileInfo.nPosition = sMessage.GetLength() - 1;
m_FileInfo.lpszPathName = path;
m_FileInfo.lpszFileName = filename;
m_FileInfo.lpFileType = NULL;
m_message.lpFiles = &m_FileInfo;
} // End else
lResult = lpfnMAPISendMail(0, 0, &m_message, 0, 0);
lpfnMAPIFreeBuffer(m_message.lpRecips);
if (lResult == SUCCESS_SUCCESS)
return true;
else
return false;
}
// ****************
// * Destructor *
// ****************
CRobotMail::~CRobotMail()
{
if (!m_bInitialized) return;
m_bInitialized = false;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -