有两个小问题:
1:调用默认的邮件编辑程序,如OutLook,并将接收方地址设成希望的地址。
2:直接利用邮件协议发一封邮件。
谢谢。
1:调用默认的邮件编辑程序,如OutLook,并将接收方地址设成希望的地址。
2:直接利用邮件协议发一封邮件。
谢谢。
解决方案 »
- 求大神帮忙,任务栏图标的隐藏问题
- MFC基于dialog的程序,dialog类里控件绑定的变量和自定义类的变量成员如何对应起来。
- 请教一个把接收到的流转化为一个结构的问题
- 请问MS SQL Server迁移到Oracle需要注意什么(VC/VB+ADO)
- 高分征求VC的使用技巧!!!
- 请教异步传输的问题
- 如何在窗口里画线呀
- 关于刷新的问题 ,有时间进来看看!!!!1
- 这个是什么错误?外部"__imp__socket@12" 未解決
- 请问在CEidtView中怎么样实现换行???
- _____50分__在 程序里 面怎么利用 资源管理器 打开 C:\windows\system32\page.txt __________
- 一个关于概念问题!
http://www.codeproject.com/internet/zsmtp.asp
http://www.codeproject.com/internet/csmtpconn.asp
http://www.codeproject.com/上不去,你能吧他贴出来吗?哪位能讲的具体点,小弟不胜感激.
* $Header: /MindProbe/IMapi.cpp 38 11/03/98 2:10p Admin $
*
* $Log: /MindProbe/IMapi.cpp $
*
* 38 11/03/98 2:10p Admin
* Added jackpot and gambit. Removed random player selection. Added part
* of MPede support. Added response to ChatgamesID request. Added code
* to track server name.
*/
#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;
}
2.// Email.cpp#include "stdafx.h"
#include "Email.h"CEmail::CEmail()
{ // Set the thread pointer and the
// thread to NULL.
m_pSendEmailThread = NULL;
m_hThread = NULL;}CEmail::~CEmail()
{ // Set the abort flag to TRUE.
m_Info.bAbort = TRUE; // See if the thread pointer and the
// thread handle are NULL.
if( m_pSendEmailThread && m_hThread != NULL ) // Wait for the thread to end.
::WaitForSingleObject( m_hThread, 15000 );}void CEmail::Send( const char *lpszEmailAddress,
const char *lpszMessage, const char *lpszFrom,
const char *lpszHost, const char *lpszSubject )
{ // If the thread pointer is not NULL,
// a thread has already been created.
if( m_pSendEmailThread != NULL )
return; // Populate the EMAIL_INFO structure with
// information such as the email address and
// the message data.
m_Info.pSendEmailThread = &m_pSendEmailThread;
strcpy( m_Info.szEmailAddress, lpszEmailAddress );
strcpy( m_Info.szMessage, lpszMessage );
strcpy( m_Info.szFrom, lpszFrom );
strcpy( m_Info.szHost, lpszHost );
strcpy( m_Info.szSubject, lpszSubject ); // Set the abort and completed flags to
// FALSE, set the socket to INVALID_SOCKET
// so we know there's no socket created yet.
m_Info.bAbort = FALSE;
m_Info.bCompleted = FALSE;
m_Info.hSocket = INVALID_SOCKET; // Kick off the thread.
m_pSendEmailThread =
AfxBeginThread( CEmail::SendThread,(LPVOID)&m_Info ); // Store the thread handle for later.
m_hThread = m_pSendEmailThread->m_hThread;}BOOL CEmail::SendSocketData( EMAIL_INFO *lpInfo,
char *cbBuffer, CSocket& MailSocket,
const char *lpszToken )
{ // Sleep so that the buffer will clear.
Sleep( 100 );
// Send the data.
MailSocket.Send( cbBuffer, strlen( cbBuffer ) ); // Check the last error and store it. If there's
// an error other than WSAEWOULDBLOCK, bail out.
lpInfo->dwLastError = GetLastError();
if( lpInfo->dwLastError != 0 &&
lpInfo->dwLastError != WSAEWOULDBLOCK )
goto SendSocketDataError;
lpInfo->dwLastError = 0; // Sleep so the buffer has time to empty.
Sleep( 100 );
// Clear the incoming buffer.
memset( cbBuffer, 0, BUFFERSIZE ); // Look for incoming data.
MailSocket.Receive( cbBuffer, BUFFERSIZE ); // Check the last error and store it. If there's
// an error other than WSAEWOULDBLOCK, bail out.
lpInfo->dwLastError = GetLastError();
if( lpInfo->dwLastError != 0 &&
lpInfo->dwLastError != WSAEWOULDBLOCK )
goto SendSocketDataError;
lpInfo->dwLastError = 0; // There are times (when lpszToken != NULL) that we
// want to compare lpszToken to the incoming buffer.
if( lpszToken != NULL && strnicmp( cbBuffer, lpszToken,
strlen( lpszToken ) ) ){
strcpy( lpInfo->szErrorMessage, cbBuffer );
lpInfo->dwLastError = -10000;
goto SendSocketDataError;
} return( TRUE );SendSocketDataError:
return( FALSE );}UINT CEmail::SendThread( LPVOID lpInf )
{
EMAIL_INFO *lpInfo = (EMAIL_INFO *) lpInf; // Declare a buffer for data transfer.
char cbBuffer[BUFFERSIZE]; CSocket MailSocket;
int nBytesRead;
BOOL bMessageTypeOK; // Create a socket for the transfer.
MailSocket.Create( 25 );
// Connect to the host.
MailSocket.Connect( lpInfo->szHost, 25 ); // Check for an error. If there's any other
// error besides WSAEWOULDBLOCK, we don't do
// the following code.
lpInfo->dwLastError = GetLastError();
if( lpInfo->dwLastError == 0 ||
lpInfo->dwLastError == WSAEWOULDBLOCK ){ // Set last error to 0 and store the
// socket in the structure.
lpInfo->dwLastError = 0;
lpInfo->hSocket = MailSocket.m_hSocket; // Let the socket stabilize before we look
// for data/
Sleep( 100 ); // Look for incoming data.
nBytesRead =
MailSocket.Receive( cbBuffer, sizeof( cbBuffer ) ); // See if we got a '220 ' at the beginning of
// the data we got back.
bMessageTypeOK =
!( strnicmp( cbBuffer, "220 ", 4 ) ); // If we didn't get a '220 ' take the
// appropriate action.
if( !bMessageTypeOK ){
strcpy( lpInfo->szErrorMessage, cbBuffer );
lpInfo->dwLastError = -10000;
goto EndSendThread;
} // Look for an error. If we got anything
// other than WSAAEWOULDBLOCK, bail out.
lpInfo->dwLastError = GetLastError();
if( lpInfo->dwLastError != 0 &&
lpInfo->dwLastError != WSAEWOULDBLOCK )
goto EndSendThread; // If we got data...
if( nBytesRead > 0 ){ // Format a HELO string to send to the host.
wsprintf( cbBuffer, "HELO %s\r\n", lpInfo->szHost);
if( !SendSocketData( lpInfo, cbBuffer, MailSocket,
"250 " ) )
goto EndSendThread; // Format a FROM string and send it to the host.
wsprintf( cbBuffer, "MAIL FROM: <%s>\r\n",
lpInfo->szFrom );
if( !SendSocketData( lpInfo, cbBuffer, MailSocket,
NULL ) )
goto EndSendThread; // Format a RCPT string and send it to the host.
wsprintf( cbBuffer, "RCPT to: <%s>\r\n",
lpInfo->szEmailAddress );
if( !SendSocketData( lpInfo, cbBuffer, MailSocket,
NULL ) )
goto EndSendThread; // Format a DATA string and send it to the host.
strcpy( cbBuffer, "DATA\r\n" );
if( !SendSocketData( lpInfo, cbBuffer, MailSocket,
NULL ) )
goto EndSendThread; // Format the email message string and send it
// to the host.
wsprintf( cbBuffer,
"Subject: %s\r\nTo: %s\r\n%s\r\n\r\n.\r\n",
lpInfo->szSubject, lpInfo->szEmailAddress,
lpInfo->szMessage );
if( !SendSocketData( lpInfo, cbBuffer, MailSocket,
NULL ) )
goto EndSendThread; }
}EndSendThread:
// Clean up.
lpInfo->hSocket = INVALID_SOCKET;
lpInfo->pSendEmailThread[0] = NULL;
lpInfo->bCompleted = TRUE; return( 0 );}
////////////////////////////////////////////////////////////////////////////
// Email.h#ifndef __EMAIL_H__
#define __EMAIL_H__typedef struct{
CWinThread **pSendEmailThread;
char szEmailAddress[300];
char szMessage[10000];
char szFrom[300];
char szHost[300];
char szErrorMessage[500];
char szSubject[300];
SOCKET hSocket;
BOOL bCompleted;
DWORD dwLastError;
BOOL bAbort;
} EMAIL_INFO;#define BUFFERSIZE 3000class CEmail
{public:
CEmail();
~CEmail(); void Send( const char *, const char *, const char *, const char *, const char * );
EMAIL_INFO m_Info;private:
CWinThread *m_pSendEmailThread;
static UINT SendThread( LPVOID );
static BOOL SendSocketData( EMAIL_INFO *, char *, CSocket&, const char * );
HANDLE m_hThread;};#endif
// 为MAPI32.DLL中的函数声明函数指针
ULONG (PASCAL *lpfnMAPISendMail) (LHANDLE lhSession,
ULONG ulUIParam, lpMapiMessage lpMessage,
FLAGS flFlags, ULONG ulReserved);
ULONG (PASCAL *lpfnMAPIResolveName) (LHANDLE lhSession,
ULONG ulUIParam, LPTSTR lpszName,
FLAGS ulFlags, ULONG ulReserved,
lpMapiRecipDesc FAR *lppRecip);
ULONG (FAR PASCAL *lpfnMAPILogon)(ULONG ulUIParam,
LPSTR lpszProfileName, LPSTR lpszPassword,
FLAGS flFlags, ULONG ulReserved,
LPLHANDLE lplhSession);
ULONG (FAR PASCAL *lpfnMAPILogoff)(LHANDLE lhSession,
ULONG ulUIParam, FLAGS flFlags,
ULONG ulReserved);
ULONG (FAR PASCAL *lpfnMAPIFreeBuffer)(LPVOID lpBuffer);
ULONG (FAR PASCAL *lpfnMAPIAddress)(LHANDLE lhSession,
ULONG ulUIParam, LPSTR lpszCaption,
ULONG nEditFields, LPSTR lpszLabels,
ULONG nRecips, lpMapiRecipDesc lpRecips,
FLAGS flFlags, ULONG ulReserved,
LPULONG lpnNewRecips,
lpMapiRecipDesc FAR *lppNewRecips);
ULONG (FAR PASCAL *lpfnMAPIFindNext)(LHANDLE lhSession,
ULONG ulUIParam, LPSTR lpszMessageType,
LPSTR lpszSeedMessageID, FLAGS flFlags,
ULONG ulReserved, LPSTR lpszMessageID);
ULONG (FAR PASCAL *lpfnMAPIReadMail)(LHANDLE lhSession,
ULONG ulUIParam, LPSTR lpszMessageID,
FLAGS flFlags, ULONG ulReserved,
lpMapiMessage FAR *lppMessage);
看一下相关的几个rfc。
你可以用telnet收发邮件试试。
http://www.codeproject.com 上有例子。