您的位置:首页 > 编程语言 > C语言/C++

c++调用mapi发送邮件的方法

2013-09-06 18:16 232 查看
#include "stdafx.h"

#include <windows.h>

#include <mapi.h>

#include <stdlib.h>

#define SENDMAIL_LOADMAPI_ERROR 1

#define SENDMAIL_LOGON_ERROR 2

#define SENDMAIL_SENDMAIL_ERROR 3

#define SENDMAIL_SECCESS 0

int main(int argc, char* argv[])

{

HINSTANCE hMapIns;

LPMAPILOGON pMAPILogon;

LPMAPILOGOFF pMAPILogoff;

LPMAPISENDMAIL pMAPISendMall;

LPMAPIRESOLVENAME pMAPIResolveName;

LHANDLE lplhSessio=0;

hMapIns=(HINSTANCE)LoadLibrary("MAPI32.DLL");

pMAPILogon=(LPMAPILOGON)GetProcAddress(hMapIns,"MAPILogon");

pMAPILogoff =(LPMAPILOGOFF)GetProcAddress(hMapIns,"MAPILogoff");

pMAPISendMall=(LPMAPISENDMAIL)GetProcAddress(hMapIns,"MAPISendMail");

pMAPIResolveName=(LPMAPIRESOLVENAME)GetProcAddress(hMapIns,"MAPIResolveName");

pMAPILogon(0,NULL,NULL,MAPI_NEW_SESSION,0,&lplhSessio);

MapiRecipDesc receiv;

memset(&receiv,0,sizeof(MapiRecipDesc));

receiv.lpszAddress="xxxx@qq.com";

receiv.ulRecipClass=MAPI_TO;

MapiMessage msg;

memset(&msg,0,sizeof(msg));

msg.lpRecips=&receiv;

msg.lpFiles=0;

msg.lpszSubject="test";

msg.nFileCount=0;

msg.lpszNoteText="also test!";

msg.nRecipCount=1;

pMAPIResolveName(lplhSessio,0,msg.lpRecips->lpszAddress,0,0,&msg.lpRecips);

pMAPISendMall(lplhSessio,0,&msg,0 ,0);

pMAPILogoff(lplhSessio,0 ,0,0);

FreeLibrary(hMapIns);

return 0;

}

备注:上面的代码会自动调用系统上默认的邮件客户端软件,曾经尝试过通过system()等方法调用lotus或outlook应用程序通过传参数的方法来调用,然后发送邮件到指定的邮箱,但差了很久都没有找到实现方法,也没有找到lotus和outlook针对二次编程的接口,也许存在接口但是自己还没找到(如果有哪位大虾有针对outlook或lotus的接口库的资料,一定要告知我一下丫,共同进步O(∩_∩)O)

引用自:http://zhidao.baidu.com/question/154244956.html

其他参考:http://zhidao.baidu.com/question/114428849.html

http://baike.baidu.com/view/1285848.htm

http://zhidao.baidu.com/question/111459085.html    mailslot

 

-------------------------------

待测:

//imapi.h================================

/*

* $Header:$

*

* $Log:$

*/

//#include

class CIMapi

{

public:

CIMapi();

~CIMapi();

enum errorCodes

{

IMAPI_SUCCESS = 0,

IMAPI_LOADFAILED,

IMAPI_INVALIDDLL,

IMAPI_FAILTO,

IMAPI_FAILCC,

IMAPI_FAILATTACH

};

// Attributes

void Subject(LPCTSTR subject) { m_message.lpszSubject = (LPTSTR) subject; }

void Text(LPCTSTR text) { m_text = text; }

UINT Error();

void From(LPCTSTR from) { m_from.lpszName = (LPTSTR) from; }

static BOOL HasEmail();

// Operations

BOOL To(LPCTSTR recip);

BOOL Cc(LPCTSTR recip);

BOOL Attach(LPCTSTR path, LPCTSTR name = NULL);

BOOL Send(ULONG flags = 0);

private:

BOOL AllocNewTo();

MapiMessage m_message;

MapiRecipDesc m_from;

UINT m_error;

CString m_text;

ULONG (PASCAL *m_lpfnSendMail)(ULONG, ULONG, MapiMessage*, FLAGS, ULONG);

static HINSTANCE m_hInstMail;

static BOOL m_isMailAvail;

};

//imapi.cpp==================================================

/*

* $Header:$

*

* $Log:$

*/

#include "stdafx.h"

#include <MAPI.H>

#include "imapi.h"

HINSTANCE CIMapi::m_hInstMail = (HINSTANCE) NULL;

BOOL CIMapi::m_isMailAvail = (BOOL) -1;

CIMapi::CIMapi()

{

m_error = 0; // Initially error free

memset(&m_message, 0, sizeof(MapiMessage));

memset(&m_from, 0, sizeof(MapiRecipDesc));

m_message.lpOriginator = &m_from;

m_from.ulRecipClass = MAPI_ORIG;

if (m_hInstMail == (HINSTANCE) NULL) // Load the MAPI dll

m_hInstMail = ::LoadLibraryA("MAPI32.DLL");

if (m_hInstMail == (HINSTANCE) NULL)

{

AfxMessageBox(AFX_IDP_FAILED_MAPI_LOAD);

m_error = IMAPI_LOADFAILED;

return;

}

ASSERT(m_hInstMail != (HINSTANCE) NULL); // Now get the pointer to the send function

(FARPROC&) m_lpfnSendMail = GetProcAddress(m_hInstMail, "MAPISendMail");

if (NULL == m_lpfnSendMail)

{

AfxMessageBox(AFX_IDP_INVALID_MAPI_DLL);

m_error = IMAPI_INVALIDDLL;

return;

}

ASSERT(m_lpfnSendMail != NULL);

}

CIMapi::~CIMapi()

{

if (m_hInstMail != (HINSTANCE) NULL)

::FreeLibrary(m_hInstMail);

m_hInstMail = (HINSTANCE) NULL;

free(m_message.lpFiles);

free(m_message.lpRecips);

}

BOOL CIMapi::HasEmail()

{

if (m_isMailAvail == (BOOL) -1)

m_isMailAvail = ::GetProfileInt(_T("MAIL"), _T("MAPI"), 0) != 0 && SearchPath(NULL, _T("MAPI32.DLL"), NULL, 0, NULL, NULL) != 0;

return m_isMailAvail;

}

UINT CIMapi::Error()

{

UINT temp = m_error;

m_error = IMAPI_SUCCESS;

return temp;

}

BOOL CIMapi::AllocNewTo()

{

// Allocate a new MapiRecipDesc structure and initialise it to all zeros

m_message.lpRecips = (MapiRecipDesc *) realloc(m_message.lpRecips, (m_message.nRecipCount + 1) * sizeof(MapiRecipDesc));

memset(&m_message.lpRecips[m_message.nRecipCount], 0, sizeof(MapiRecipDesc));

ASSERT(m_message.lpRecips);

return m_message.lpRecips != (MapiRecipDesc *) NULL;

}

BOOL CIMapi::To(LPCTSTR recip)

{

if (AllocNewTo())

{

// We succeeded in allocating a new recipient record

m_message.lpRecips[m_message.nRecipCount].lpszName = (LPTSTR) recip;

m_message.lpRecips[m_message.nRecipCount].ulRecipClass = MAPI_TO;

m_message.nRecipCount++;

return TRUE;

}

m_error = IMAPI_FAILTO;

return FALSE;

}

BOOL CIMapi::Cc(LPCTSTR recip)

{

if (AllocNewTo())

{

// We succeeded in allocating a new recipient record

m_message.lpRecips[m_message.nRecipCount].lpszName = (LPTSTR) recip;

m_message.lpRecips[m_message.nRecipCount].ulRecipClass = MAPI_CC;

m_message.nRecipCount++;

return TRUE;

}

m_error = IMAPI_FAILCC;

return FALSE;

}

BOOL CIMapi::Attach(LPCTSTR path, LPCTSTR name)

{

// Add a new attachment record

m_message.lpFiles = (MapiFileDesc *) realloc(m_message.lpFiles, (m_message.nFileCount + 1) * sizeof(MapiFileDesc));

memset(&m_message.lpFiles[m_message.nFileCount], 0, sizeof(MapiFileDesc));

ASSERT(m_message.lpFiles);

if (m_message.lpFiles == (MapiFileDesc *) NULL)

{

m_error = IMAPI_FAILATTACH;

return FALSE;

}

m_message.lpFiles[m_message.nFileCount].lpszPathName = (LPTSTR) path;

m_message.lpFiles[m_message.nFileCount].lpszFileName = (LPTSTR) name;

m_message.nFileCount++;

return TRUE;

}

BOOL CIMapi::Send(ULONG flags)

{

CWaitCursor wait;

int offset = m_text.GetLength();

// Add 1 space per attachment at the end of the body text.

m_text += CString(' ', m_message.nFileCount);

// Set each attachment to replace one of the added spaces at the end of the body text.

for (UINT i = 0; i < m_message.nFileCount; i++)

m_message.lpFiles[i].nPosition = offset++;

m_message.lpszNoteText = (LPTSTR) (LPCTSTR) m_text; // Set the body text

// prepare for modal dialog box

AfxGetApp()->EnableModeless(FALSE);

HWND hWndTop;

CWnd* pParentWnd = CWnd::GetSafeOwner(NULL, &hWndTop);

// some extra precautions are required to use MAPISendMail as it

// tends to enable the parent window in between dialogs (after

// the login dialog, but before the send note dialog).

pParentWnd->SetCapture();

::SetFocus(NULL);

pParentWnd->m_nFlags |= WF_STAYDISABLED;

int nError = m_lpfnSendMail(0, (ULONG) pParentWnd->GetSafeHwnd(), &m_message, MAPI_LOGON_UI | flags, 0);

// after returning from the MAPISendMail call, the window must

// be re-enabled and focus returned to the frame to undo the workaround

// done before the MAPI call.

::ReleaseCapture();

pParentWnd->m_nFlags &= ~WF_STAYDISABLED;

pParentWnd->EnableWindow(TRUE);

::SetActiveWindow(NULL);

pParentWnd->SetActiveWindow();

pParentWnd->SetFocus();

if (hWndTop != NULL)

::EnableWindow(hWndTop, TRUE);

AfxGetApp()->EnableModeless(TRUE);

if (nError != SUCCESS_SUCCESS && nError != MAPI_USER_ABORT && nError != MAPI_E_LOGIN_FAILURE)

{

AfxMessageBox(AFX_IDP_FAILED_MAPI_SEND);

return FALSE;

}

return TRUE;

}

//在你的dlg.cpp #include imapi.h

//你的dlg.cpp 的按钮事件===================================

void 你的DLG::OnBnClickedButton1()

{

CIMapi mail;

mail.To("Tofirst@qq.com");

mail.To("Tosecond@qq.com");

mail.Cc("CCfirst@qq.com");

mail.From("aa@qq.com");

mail.Subject("Test Email");

// mail.Attach("somefilename"); //

// mail.Attach("someotherfile", "different_name_for_recipient");

mail.Text("Body text for this email");

mail.Send();

}

引用自:http://zhidao.baidu.com/question/176525328.html

上面的这种方式不知道什么原因编译不能通过,待测...
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++