📄 smapisampleview.cpp
字号:
// SMapiSampleView.cpp : implementation of the CSMapiSampleView class
//
#include "stdafx.h"
#include "SMapiSample.h"
#include "SMapiSampleDoc.h"
#include "SMapiSampleView.h"
#include "mapi.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CSMapiSampleView
IMPLEMENT_DYNCREATE(CSMapiSampleView, CEditView)
BEGIN_MESSAGE_MAP(CSMapiSampleView, CEditView)
//{{AFX_MSG_MAP(CSMapiSampleView)
ON_COMMAND(IDM_SEND, OnSend)
ON_COMMAND(IDM_RECEIVE, OnReceive)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CEditView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CEditView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CEditView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSMapiSampleView construction/destruction
CSMapiSampleView::CSMapiSampleView()
{
// TODO: add construction code here
//在这里只是完成动态链接库的加载
//而其他的各个任务是在其他的函数里现用现完成的
//添加初始化MAPI的第一步:装载MAPI32.DLL动态链接库
hMAPILib=LoadLibrary("MAPI32.DLL");
if(hMAPILib==NULL)
{
AfxMessageBox("不能正常的加载动态链接库文件MAPI32.DLL");
// return FALSE;
}
}
CSMapiSampleView::~CSMapiSampleView()
{
//释放和DLL的空间
FreeLibrary(hMAPILib);
}
BOOL CSMapiSampleView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
BOOL bPreCreated = CEditView::PreCreateWindow(cs);
cs.style &= ~(ES_AUTOHSCROLL|WS_HSCROLL); // Enable word-wrapping
return bPreCreated;
}
/////////////////////////////////////////////////////////////////////////////
// CSMapiSampleView drawing
void CSMapiSampleView::OnDraw(CDC* pDC)
{
CSMapiSampleDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
}
/////////////////////////////////////////////////////////////////////////////
// CSMapiSampleView printing
BOOL CSMapiSampleView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default CEditView preparation
return CEditView::OnPreparePrinting(pInfo);
}
void CSMapiSampleView::OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo)
{
// Default CEditView begin printing.
CEditView::OnBeginPrinting(pDC, pInfo);
}
void CSMapiSampleView::OnEndPrinting(CDC* pDC, CPrintInfo* pInfo)
{
// Default CEditView end printing
CEditView::OnEndPrinting(pDC, pInfo);
}
/////////////////////////////////////////////////////////////////////////////
// CSMapiSampleView diagnostics
#ifdef _DEBUG
void CSMapiSampleView::AssertValid() const
{
CEditView::AssertValid();
}
void CSMapiSampleView::Dump(CDumpContext& dc) const
{
CEditView::Dump(dc);
}
CSMapiSampleDoc* CSMapiSampleView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CSMapiSampleDoc)));
return (CSMapiSampleDoc*)m_pDocument;
}
#endif //_DEBUG
BOOL CSMapiSampleView::OnSend()
{
// TODO: Add your command handler code here
//添加初始化MAPI的第二步:找到想要调用的MAPI函数地址
//而这些函数指针的声明是在头文件里完成的
(FARPROC&) lpfnMAPISendMail =
GetProcAddress(hMAPILib,"MAPISendMail");
(FARPROC&) lpfnMAPILogon =
GetProcAddress(hMAPILib,"MAPILogon");
(FARPROC&) lpfnMAPILogoff =
GetProcAddress(hMAPILib,"MAPILogoff");
(FARPROC&) lpfnMAPIFreeBuffer =
GetProcAddress(hMAPILib, "MAPIFreeBuffer");
// (FARPROC&) lpfnMAPIAddress =
// GetProcAddress(hMAPILib,"MAPIAddress");
// (FARPROC&) lpfnMAPIResolveName =
// GetProcAddress(hMAPILib, "MAPIResolveName");
//接收信报的第一步:生成信报结构指针
pMessage=new(MapiMessage);
memset(pMessage,0,sizeof(MapiMessage));
//下面设置信报结构的各个域
//[1]把ulReserved设置为0:
pMessage->ulReserved = 0;
//[2]设置信件标题(lpszSubject):
char subject[512]="助你精通网络编程";
pMessage->lpszSubject=subject;
//[3]设置信件内容:
char text[5000]="悟空啊,你可以好好的学习这一本网络编程的书,\
它详细的介绍Windows网络编程的各个方面。\
真的很不错啊。你想要啊,你想要,我就给你";
pMessage->lpszNoteText=text;
//[4]设置信息类型指针lpszMessageType,可以为NULL:
pMessage->lpszMessageType = NULL;
//[5] 指向接收这个消息的时间的指针
pMessage->lpszDateReceived=NULL;
//[6] 指向标识消息所属会话线程的串的指针
pMessage->lpszConversationID=NULL;
//[7]设置flFlags标识
pMessage->flFlags=MAPI_SENT;
//[8]用一个指向MapiRecipDesc结构的指针设置发送者信息
//可以设置为NULL:
pMessage->lpOriginator=NULL;
//[9]设置接收者数目,现在是1
pMessage->nRecipCount=1;
//[10]设置接收者信息(lpRecips)
MapiRecipDesc recipient=
{
0,MAPI_TO,"至尊宝","SMTP:qitiandasheng",0,NULL
};
pMessage->lpRecips=&recipient;
//[11]设置附件数量(nFileCount),现在是0
pMessage->nFileCount=0;
//[12]设置附件信息,现在没有
pMessage->lpFiles=NULL;
//第二步登录到电子邮件对象,即建立会话
LHANDLE lhSession;
ULONG lResult = lpfnMAPILogon(0, NULL, NULL,
0, 0, &lhSession);
if (lResult != SUCCESS_SUCCESS)
//SUCCESS_SUCCESS在MAPI.H中被定义
{
AfxMessageBox("没有成功的建立起会话");
return FALSE;
}
//第三步进行信报的发送
lResult = lpfnMAPISendMail(0, 0, pMessage, 0, 0);
if (lResult != SUCCESS_SUCCESS)
//SUCCESS_SUCCESS在MAPI.H中被定义
{
AfxMessageBox("没有成功的信报");
return FALSE;
}
//第四步:释放信报结构的内存空间
lpfnMAPIFreeBuffer(pMessage);
//第五步:结束会话
lResult=lpfnMAPILogoff(lhSession,0,0,0);
AfxMessageBox("已经成功的信报");
return TRUE;
}
BOOL CSMapiSampleView::OnReceive()
{
// TODO: Add your command handler code here
//int i=1;//这是一个调试断点
//添加初始化MAPI的第二步:找到想要调用的MAPI函数地址
//而这些函数指针的声明是在头文件里完成的
//(FARPROC&) lpfnMAPIResolveName = GetProcAddress(hMAPILib, "MAPIResolveName");
(FARPROC&) lpfnMAPILogon = GetProcAddress(hMAPILib,"MAPILogon");
(FARPROC&) lpfnMAPILogoff = GetProcAddress(hMAPILib,"MAPILogoff");
(FARPROC&) lpfnMAPIFreeBuffer = GetProcAddress(hMAPILib, "MAPIFreeBuffer");
// (FARPROC&) lpfnMAPIAddress = GetProcAddress(hMAPILib,"MAPIAddress");
(FARPROC&) lpfnMAPIFindNext = GetProcAddress(hMAPILib,"MAPIFindNext");
(FARPROC&) lpfnMAPIReadMail = GetProcAddress(hMAPILib,"MAPIReadMail");
//接收邮件的第一步:登录到电子邮件对象
//即建立会话
ULONG lResult=lpfnMAPILogon(0,
NULL,NULL,
MAPI_LOGON_UI,
//MAPI_PASSWORD_UI,
0, &lhSession);
if (lResult != SUCCESS_SUCCESS)
//SUCCESS_SUCCESS在MAPI.H中被定义
{
AfxMessageBox("建立会话失败");
return FALSE;
}
//接收邮件的第二步:定位到第一个邮件
char pMessageID[513];
lResult =
lpfnMAPIFindNext(lhSession,
NULL, NULL, NULL,
MAPI_LONG_MSGID ,
0, pMessageID);
if (lResult != SUCCESS_SUCCESS)
{
AfxMessageBox("定位信报时出错");
return FALSE;
}
//接收邮件的第三步:读取第一个信报
//在这里分配信报结构的内存空间
pMessage=new(MapiMessage);
memset(pMessage,0,sizeof(MapiMessage));
lResult =
lpfnMAPIReadMail(lhSession, NULL, pMessageID,
MAPI_SUPPRESS_ATTACH, 0, &pMessage);
if (lResult != SUCCESS_SUCCESS)
{
AfxMessageBox("读取信报时出错");
return FALSE;
}
//接收邮件的第四步:处理读取的结果,在窗口中显示得到的信报
CEdit *pEdit;
pEdit=&GetEditCtrl();
CString str="标题:"+
(CString)pMessage->lpszSubject+
" "
"发送者:"+
(CString)pMessage->lpOriginator->lpszName;
pEdit->SetWindowText(str);
//接收邮件的第五步:
//在访问另一条信件以前应当释放内存,否则会出现内存泄漏
lpfnMAPIFreeBuffer(pMessage);
//第六步:结束会话
lResult=lpfnMAPILogoff(lhSession,0,0,0);
//以下读者可以自己完成读取其他信报的操作
AfxMessageBox("接收邮件成功的完成了");
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -