以下是我发送邮件的代码。请帮我加上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 是个结构体。
MFC里用MAPI发送邮件时如何设置CC。
答案:4 悬赏:30 手机版
解决时间 2021-02-05 17:03
- 提问者网友:疯子也有疯子的情调
- 2021-02-05 11:58
最佳答案
- 五星知识达人网友:轻雾山林
- 2021-02-05 12:25
用这个吧。简单明了。
//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次问题了。
//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次问题了。
全部回答
- 1楼网友:不甚了了
- 2021-02-05 13:34
MapiRecipDesc reci[1];定义一个元素的数组,这么简单的基础知识。。。后面的reci[1]访问越界。你还是好好去看看基础吧。没学行就学飞只会摔得更疼。
再看看别人怎么说的。
- 2楼网友:独钓一江月
- 2021-02-05 13:24
这个改动很简单的:
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
- 3楼网友:有你哪都是故乡
- 2021-02-05 12:40
这个改动很简单的:
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 );
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯