请教:发邮件的VC++6.0代码?

haidong 2000-02-23 07:27:00
...全文
155 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
jamesw 2000-02-24
  • 打赏
  • 举报
回复
////////////////////////////////////////////////////////////////////
//
// CRobotMail.h - CRobotMail class declarations
//
// Source: "Programming Bots, Spiders, and Intelligent Agents
// in Microsoft Visual C++"
//
// Copyright (c) 1999, David Pallmann. All rights reserved.

#include <mapi.h>

class CRobotMail
{
public:
CRobotMail();
int GetNewMessageCount();
int GetMessageCount();
BOOL FirstNewMessage(BOOL bMarkAsRead = false);
BOOL FirstMessage(BOOL bMarkAsRead = false);
BOOL NextMessage(BOOL bMarkAsRead = false);
BOOL SendMessage(CString sTo,
CString sSubject,
CString sMessage,
CString sAttachment);
~CRobotMail();

public:
CString m_sFrom;
CString m_sFromAddress;
CString m_sSubject;
CString m_sMessage;
CString m_sDate;
BOOL m_bRead;

private:
BOOL m_bInitialized;
BOOL m_bNewOnly;
HINSTANCE m_hInstMail;
ULONG m_lhSession; // LHANDLE

private:
char m_pMessageID[513];
MapiMessage m_message;
MapiMessage *m_pMessage;
};


////////////////////////////////////////////////////////////////////
//
// 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 and MAPI_UNREAD_ONLY,
0,
m_pMessageID);
while (lResult == SUCCESS_SUCCESS)
{
nCount++;
lResult = lpfnMAPIFindNext(m_lhSession,
NULL,
NULL,
m_pMessageID,
MAPI_LONG_MSGID and 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 and 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 and 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 and 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 and 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 and 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;
}
zjy 2000-02-23
  • 打赏
  • 举报
回复
I'll send to your mail-box.

16,467

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧