我有对方的Email地址,还有要发送的内容,如何能够将Mail通过编程发送出去呢?
需要调用什么函数?是不是可以不留下自己发送方的Mail地址?
需要调用什么函数?是不是可以不留下自己发送方的Mail地址?
解决方案 »
- 线程不响应的问题
- 从 FTP 服务器将一个文件拷到另一个服务器或 服务器内拷贝文件, 这个函数安全可靠吗?
- 一个基于TCP传文件的通信代码。文件传送成功,且数据没有错误(传递的文挡没错误、传递的软件可以运行)。但在关闭改程序前,不能打开所传的文件,说另一个程序正式使用
- [求教]调小socket的send buffer and recv buffer, 是不是可以减少一个socket占用的未分页内存, 以增加系统可以接入的连接数?
- 请问如何将char转换为unsigned char?
- 怎么通过多级代理(代理服务器+代理服务器)访问一些特定的网站?
- 如何给一个软件做加密模块?
- ●●关于消息循环,对你们来说可能很简单,但它困惑我很久了。请进来看看吧●●
- 50分,vc中,关于一个简单的类型转换问题。
- 明天微软研究院的人 到我们学校宣传vs.net 有谁有问题问他们的吗?
- sniffer的软件有哪些?
- vc++技术内幕这本书哪一版最好!!!
如果不要不用某个SMTP,而是直接把邮件送到对方的信箱的话,有点难度。看看这个网页中对SMTP的介绍你就明白了。好象没有具体的函数可以调用。
你就可以调用 Outlook 的 COM 组件发信。
否则, SMTP协议也非常简单,直接用就行。
我用smtp.sina.com.cn总是说连接不到服务器啊。
#include "MAPI.H"
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;
};
//实现CPP
#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 (m_lpfnSendMail == NULL)
{
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) malloc(strlen(recip) + 1);
strcpy(m_message.lpRecips[m_message.nRecipCount].lpszName, 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) malloc(strlen(recip) + 1);
strcpy(m_message.lpRecips[m_message.nRecipCount].lpszName, 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) malloc(strlen(path) + 1);
strcpy(m_message.lpFiles[m_message.nFileCount].lpszPathName, path); if (name != (LPCTSTR) NULL)
{
m_message.lpFiles[m_message.nFileCount].lpszFileName = (LPTSTR) malloc(strlen(name) + 1);
strcpy(m_message.lpFiles[m_message.nFileCount].lpszFileName, 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); // Now free malloced recipients
for (i = 0; i < m_message.nRecipCount; i++)
free(m_message.lpRecips[i].lpszName); // Then free malloced attachments
for (i = 0; i < m_message.nFileCount; i++)
{
free(m_message.lpFiles[i].lpszPathName);
free(m_message.lpFiles[i].lpszFileName);
} if (nError != SUCCESS_SUCCESS && nError != MAPI_USER_ABORT && nError != MAPI_E_LOGIN_FAILURE)
{
AfxMessageBox(AFX_IDP_FAILED_MAPI_SEND);
return FALSE;
} return TRUE;
}