永发信息网

MFC里用MAPI发送邮件时如何设置CC。

答案:4  悬赏:30  手机版
解决时间 2021-02-05 17:03
以下是我发送邮件的代码。请帮我加上CC给aa@qq.com。谢谢了。分可以追加!我在网上搜了下说用IMessage的一个方法去设置,可是不会用。高手们帮帮忙!
LHANDLE loghandle;
ULONG res = m_MAPILogon( 0,NULL,NULL,0,0,&loghandle );
CString ss;
ss.Format( "%d",res );
CString strsubject,strbody;
strsubject = mailSubject;
strbody = mailBody;
CTime time = CTime::GetCurrentTime();
CString strtime = time.Format( "%y//%m//%d//%H" );
CString address1,address2;
address1 = user
address2 = "SMTP:" + user + "@qq.com";
MapiMessage message;
//subject of the mail
message.lpszSubject = strsubject.GetBuffer(0);
//body of the mail
message.lpszNoteText = strbody.GetBuffer(0);
message.lpszMessageType = NULL;
//time sending
message.lpszDateReceived = strtime.GetBuffer(0);

message.lpszConversationID = NULL;
message.flFlags = MAPI_SENT;
//sender message
message.lpOriginator = NULL;
message.nRecipCount = 1;//reciver count
MapiRecipDesc reci = { 0,MAPI_TO,address1.GetBuffer(0),address2.GetBuffer(0),0,NULL };
message.lpRecips = &reci;
message.lpFiles = 0;//attachment
message.nFileCount = 0;//attachment count

int lresult = m_MAPISendMail( loghandle,0,&message,0,0); //发送邮件

m_MAPIFreeBuffer( &message );
m_MAPILogoff( loghandle,0,0,0 );
你们都错了。这个方法我当然试过。
MapiRecipDesc reci[1];
reci[0] = { 0,MAPI_TO,address1.GetBuffer(0),address2.GetBuffer(0),0,NULL };
reci[1] = { 0,MAPI_CC,"aa","aa@qq.com",0,NULL };
message.lpRecips = &reci;
这些地方都会报错。MapiRecipDesc 是个结构体。
最佳答案
用这个吧。简单明了。
//imapi.h================================

//#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==================================================

#include "stdafx.h"
#include
#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();
}

我以前写过这方面的。楼主还有问题都可以问问吧。今天看你发了2次问题了。
全部回答
MapiRecipDesc reci[1];定义一个元素的数组,这么简单的基础知识。。。后面的reci[1]访问越界。你还是好好去看看基础吧。没学行就学飞只会摔得更疼。 再看看别人怎么说的。
这个改动很简单的: LHANDLE loghandle; ULONG res = m_MAPILogon( 0,NULL,NULL,0,0,&loghandle ); CString ss; ss.Format( "%d",res ); CString strsubject,strbody; strsubject = mailSubject; strbody = mailBody; CTime time = CTime::GetCurrentTime(); CString strtime = time.Format( "%y//%m//%d//%H" ); CString address1,address2; address1 = user address2 = "SMTP:" + user + "@qq.com"; MapiMessage message; //subject of the mail message.lpszSubject = strsubject.GetBuffer(0); //body of the mail message.lpszNoteText = strbody.GetBuffer(0); message.lpszMessageType = NULL; //time sending message.lpszDateReceived = strtime.GetBuffer(0); message.lpszConversationID = NULL; message.flFlags = MAPI_SENT; //sender message message.lpOriginator = NULL; //改收件人计数 message.nRecipCount = 2;//reciver count //建立收件人地址数组 MapiRecipDesc reci[1]; reci[0] = { 0,MAPI_TO,address1.GetBuffer(0),address2.GetBuffer(0),0,NULL }; reci[1] = { 0,MAPI_TO,"aa","aa@qq.com",0,NULL }; message.lpRecips = &reci; message.lpFiles = 0;//attachment message.nFileCount = 0;//attachment count int lresult = m_MAPISendMail( loghandle,0,&message,0,0); //发送邮件 m_MAPIFreeBuffer( &message ); m_MAPILogoff( loghandle,0,0,0
这个改动很简单的: LHANDLE loghandle; ULONG res = m_MAPILogon( 0,NULL,NULL,0,0,&loghandle ); CString ss; ss.Format( "%d",res ); CString strsubject,strbody; strsubject = mailSubject; strbody = mailBody; CTime time = CTime::GetCurrentTime(); CString strtime = time.Format( "%y//%m//%d//%H" ); CString address1,address2; address1 = user address2 = "SMTP:" + user + "@qq.com"; MapiMessage message; //subject of the mail message.lpszSubject = strsubject.GetBuffer(0); //body of the mail message.lpszNoteText = strbody.GetBuffer(0); message.lpszMessageType = NULL; //time sending message.lpszDateReceived = strtime.GetBuffer(0); message.lpszConversationID = NULL; message.flFlags = MAPI_SENT; //sender message message.lpOriginator = NULL; //改收件人计数 message.nRecipCount = 2;//reciver count //建立收件人地址数组 MapiRecipDesc reci[1]; reci[0] = { 0,MAPI_TO,address1.GetBuffer(0),address2.GetBuffer(0),0,NULL }; //注意设置第二属性 reci[1] = { 0,MAPI_CC,"aa","SMTP:aa@qq.com",0,NULL }; message.lpRecips = &reci; message.lpFiles = 0;//attachment message.nFileCount = 0;//attachment count int lresult = m_MAPISendMail( loghandle,0,&message,0,0); //发送邮件 m_MAPIFreeBuffer( &message ); m_MAPILogoff( loghandle,0,0,0 );
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
梦幻西游里面最适合跑镖的职业是?
如果说,你突然再在QQ上删了一个男生,你忘了
邦泰物流地址在什么地方,想过去办事
3.8×3.2+3.8×7.8-3.8简便计算
长沙开汽车到温州要多少时间
安泰物流地址在什么地方,想过去办事
八方顺物流(丰镇路与杜尔伯特路交叉口东100米
max物体以另一物体为中心向外扩散移动动画
德邦物流(七台大街县加油站往北150米路西德邦
恒丰物流地址在什么地方,我要处理点事
行く,变成て形为什么变成了(行って)为什么
its和it's都有”它的“的意思有什么区别
安能快递(城关镇二马路新城小学后面安能快递)
百世快递(前进路小学往西100米路南百世快递)
VF用查询设计器时怎么用别名
推荐资讯
韵母的意思是什么啊?知道的请说下!
弹弹堂怎么买宝珠。我有免费?
我是个新人 请问银行借记通知是什么意思,还
没有apple id怎么下载软件
打工现在也挺累的,我打算创业,但老婆不让,
释小龙、何润东,大S,吴京,杨冥,黄晓明都
在显微镜下观察一滴池塘水,会发现一些能运动
吃冰棍到底会不会上火?~~
艺尚造型地址好找么,我有些事要过去
【藻类植物会呈现绿色、红色、褐色的原因是什
在市场营销中需求开发可通过_____。A.消费者
凯达装饰地址在哪,我要去那里办事
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?