📄 smtptest.cpp
字号:
// poptest.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream.h>
#include <tchar.h>
#include <comdef.h>
#include <atlconv.h>
#include <atlbase.h>
#include "smtptest.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//调用指定路径的DLL文件
//#import"..\ReleaseMinDependency\darkpop.dll" no_namespace named_guids
#import"..\Debug\darksmtp.dll" no_namespace named_guids
/////////////////////////////////////////////////////////////////////////////
// The one and only application object
CWinApp theApp;
using namespace std;
void dump_com_error(_com_error &e)
{
_tprintf(_T("Ooops - hit an error!\n"));
_tprintf(_T("\a\tCode = %08lx\n"), e.Error());
_tprintf(_T("\a\tMessage = %s\n"),(char*)e.ErrorMessage());
_tprintf(_T("\a\tDescription = %s\n"),(char*)e.Description());
}
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
// initialize MFC and print and error on failure
//初始化MFC
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
return 1;
//初始化COM
if( FAILED(CoInitialize(0)) )
{
std::cout << "failed CoInitialize()" << endl;
return 1;
}
LONG lErr = 0;
//创建接口实例
IUnknown* pUnk = NULL;
HRESULT hRes = CoCreateInstance(CLSID_SmtpModel, NULL, CLSCTX_INPROC_SERVER,
IID_IUnknown, (void**)&pUnk);
if (FAILED(hRes))
{
TRACE(_T("Failed to create MinObj\n"));
return 1;
}
ISmtpModel* pSmtpLevel = NULL;
hRes = pUnk->QueryInterface(IID_ISmtpModel, (LPVOID*)&pSmtpLevel);
pUnk->Release();
if (FAILED(hRes))
{
TRACE(_T("QueryInterface() for IMinObj failed\n"));
return 1;
}
//bstr_t bstHost;
//bstHost = "127.0.0.1";
//bstHost="pop.x263.net";
//CHAR szHost[256];
//std::cout << "host: " << (char*)bstHost << endl;
//std::cin >> szHost;//等待用户输入HOST路径,也就是POP3邮件服务器
//bstHost = szHost;
pSmtpLevel->HostName="smtp-ent.21cn.com";
//pSmtpLevel->HostName="192.168.88.158";
pSmtpLevel->Port=25;
pSmtpLevel->UserName="gaoyue@leadsky.com";
//pSmtpLevel->UserName="DarkTest";
pSmtpLevel->PassWord="iloveme";
// pPOPLevel->HostName="pop-ent.21cn.com";
//pPOPLevel->HostName="ccc.x263.net";
// pPOPLevel->Port=110;
// pPOPLevel->UserName="gaoyue@leadsky.com";
//pPOPLevel->UserName="ttt";
// pPOPLevel->PassWord="iloveme";
//pPOPLevel->put_HostName(bstHost);//Set host address for this component.
//SysFreeString(bstHost);
//pPOPLevel->put_Port(110);//Set host port.
//bstr_t bstUser;
//bstUser = "DarkMatrix";
//CHAR szUser[256];
//std::cout << "user: " << (char*)bstUser << endl;
//std::cin >> szUser;//Waiting user to state the UserName
//bstUser = szUser;
//pPOPLevel->put_UserName(bstUser);//set UserName for this component.
//SysFreeString(bstUser);
//bstr_t bstPassword;
//bstPassword = "gaofang";
//CHAR szPassword[256];
//std::cout << "password: " << "******" << endl;
//std::cin >> szPassword;//Waiting user to state the PassWord of pointing UserName
//bstPassword = szPassword;
//pPOPLevel->put_PassWord(bstPassword);//set password
//SysFreeString(bstPassword);
//must connnect pop server first.
/* USES_CONVERSION;
CComVariant bstErrorInfo;
//与远程服务器建立连接,包括用户身份认证
try
{
CComVariant bSuccess;
if(FAILED(pPOPLevel->Connect(&bstErrorInfo,&bSuccess)))//如果连接不成功
{
std::cout << "connect failed" << endl;
char* pError=OLE2T(bstErrorInfo.bstrVal);
std::cout << pError <<endl;
pPOPLevel->Release();
CoUninitialize();
return 1;
}
_bstr_t _bstPath("C:\\Inetpub\\wwwroot\\DarkMail\\");
_bstr_t _bstUserId("GaoYue");
CComVariant lRecvMailCount=0;CComVariant lRecvMailSize=0;CComVariant lNewMailCount=0;
CComVariant lSendMailCount=0;CComVariant lSendMailSize=0;
CComVariant lSaveMailCount=0;CComVariant lSaveMailSize=0;
CComVariant lWasteMailCount=0;CComVariant lWasteMailSize=0;
pPOPLevel->GetAllBoxInfoForWeb(_bstPath,_bstUserId,
VARIANT_TRUE,VARIANT_FALSE,
&lRecvMailCount,&lRecvMailSize,&lNewMailCount,
&lSendMailCount,&lSendMailSize,
&lSaveMailCount,&lSaveMailSize,
&lWasteMailCount,&lWasteMailSize);
/*
LONG lMailCount;
//must get mail count.
lMailCount=pPOPLevel->GetMailCount();//derive the count of User's mail
std::cout << "mail count: " << lMailCount << endl;
DWORD i;
for(i = 1; i <= lMailCount; i ++)//loop the mail list and show info to user
{
std::cout << "message: " << i << endl;
_bstr_t _bstPath("C:\\Inetpub\\wwwroot\\");
_bstr_t _bstUserId("GaoYue");
CComVariant bstrFrom; //define the from address of mail
CComVariant bsTo; //define the to address of mail
CComVariant bsCC; //define the CC list of mail
CComVariant bsSubject; //define the subuject
CComVariant bsDate; //define the date
CComVariant bsContent; //define the mail content
CComVariant bsMsgId;
long lMsgSize=0;
long lAttachCount=0;
CComVariant vbNew;
if(pPOPLevel->GetMailForWeb(_bstPath,_bstUserId,i, VARIANT_FALSE,
&bstrFrom,&bsTo,&bsCC,&bsSubject,&bsDate,&bsMsgId,
&lMsgSize,&lAttachCount,&vbNew) != 0)//position the mail of list for component.
{
std::cout << "receive failed" << endl;
continue;
}
}
*/
/* _bstr_t _bstPath("C:\\Inetpub\\wwwroot\\");
_bstr_t _bstUserId("GaoYue");
CComVariant bstrFrom; //define the from address of mail
CComVariant bsTo; //define the to address of mail
CComVariant bsCC; //define the CC list of mail
CComVariant bsSubject; //define the subuject
CComVariant bsDate; //define the date
CComVariant bsContent; //define the mail content
CComVariant bsMsgId;
long lMsgSize=0;
long lAttachCount=0;
CComVariant vbNew;
CComVariant vbNext;
vbNext=VARIANT_TRUE;
while(vbNext.boolVal==VARIANT_TRUE)
{
pPOPLevel->LoadMailForWeb(_bstPath,_bstUserId,&bstrFrom,&bsTo,&bsCC,
&bsSubject,&bsDate,&bsMsgId,&lMsgSize,&lAttachCount,&vbNew,&vbNext);
pPOPLevel->GetRecvMailContentForWeb(_bstPath,_bstUserId,bsMsgId,&bstrFrom,&bsTo,&bsCC,
&bsSubject,&bsDate,&bsMsgId,&lMsgSize,&vbNew,&bsContent);
CComVariant vbNextTemp(VARIANT_TRUE);
while(vbNextTemp.boolVal==VARIANT_TRUE)
{
CComVariant bsAttachPath;
long lAttachSize=0;
pPOPLevel->GetRecvMailAttachForWeb(_bstPath,_bstUserId,bsMsgId,
&bsAttachPath,&lAttachSize,&vbNextTemp);
}
}
*/
/* for(i = 1; i <= lMailCount; i ++)//loop the mail list and show info to user
{
std::cout << "message: " << i << endl;
if(pPOPLevel->GetMail(i, VARIANT_FALSE) != 0)//position the mail of list for component.
{
std::cout << "receive failed" << endl;
continue;
}
CComVariant bstrFrom; //define the from address of mail
CComVariant bsTo; //define the to address of mail
CComVariant bsCC; //define the CC list of mail
CComVariant bsSubject; //define the subuject
//BSTR bsHeader; //define the header of mail
CComVariant bsDate; //define the date
CComVariant bsContent; //define the mail content
CComVariant bsMsgId;
pPOPLevel->get_From(&bstrFrom); //acquire the from address
CHAR* pszString = OLE2T(bstrFrom.bstrVal);
//SysFreeString(bsFrom);
std::cout << "from: " << pszString << endl; //show that
pPOPLevel->get_To(&bsTo);
pszString = OLE2T(bsTo.bstrVal);
std::cout << "to: " << pszString << endl;
pPOPLevel->get_CC(&bsCC);
pszString = OLE2T(bsCC.bstrVal);
std::cout << "cc: " << pszString << endl;
pPOPLevel->get_Subject(&bsSubject);
pszString = OLE2T(bsSubject.bstrVal);
std::cout << "subject: " << pszString << endl;
// pPOPLevel->get_RawHeader(&bsHeader);
// pszString = OLE2T(bsHeader);
// SysFreeString(bsHeader); //release the header
pPOPLevel->get_Date(&bsDate);
pszString = OLE2T(bsDate.bstrVal);
std::cout << "Date: " << pszString << endl;
pPOPLevel->get_MsgId(&bsMsgId);
pszString = OLE2T(bsMsgId.bstrVal);
std::cout << "MsgId: " << pszString << endl;
pPOPLevel->get_Content(&bsContent);
pszString = OLE2T(bsContent.bstrVal);
std::cout << "Content: " << pszString << endl;
long nAttachCount;
nAttachCount=pPOPLevel->GetAttachCount();
std::cout << "Attach count: " << nAttachCount << endl;
for(int j=1;j<=nAttachCount;j++)
{
std::cout << "Attach: " << i << endl;
VARIANT bsName;
VARIANT bsExName;
VARIANT bsData;
VARIANT bsDataId;
pPOPLevel->GetAttachment(j,&bsName,&bsExName,&bsData,&bsDataId);
pszString=OLE2T(bsName.bstrVal);
std::cout << "Attach Name: " << pszString << endl;
//HANDLE hFile=CreateFile(pszString,GENERIC_READ|GENERIC_WRITE,NULL,NULL,CREATE_ALWAYS,
// FILE_ATTRIBUTE_NORMAL,NULL);
pszString=OLE2T(bsExName.bstrVal);
std::cout << "Attach ExName: " << pszString << endl;
pszString=OLE2T(bsData.bstrVal);
std::cout << "Attach Data: " << pszString << endl;
pszString=OLE2T(bsDataId.bstrVal);
std::cout << "Attach ID: " << pszString << endl;
_bstr_t _bstPath;
_bstPath="D:\\DarkPrince";//\\MyProject\\DarkPop\\downfile";
_bstr_t _bstName(bsName);
long nWriteLength=pPOPLevel->WriteAttachment(j,_bstPath,_bstName);
SysFreeString(bsData.bstrVal);
SysFreeString(bsExName.bstrVal);
SysFreeString(bsName.bstrVal);
SysFreeString(bsData.bstrVal);
}
}*/
USES_CONVERSION;
CComVariant bstErrorInfo;
try
{
CComVariant bSuccess;
if(FAILED(pSmtpLevel->Connect(&bstErrorInfo,&bSuccess)))//如果连接不成功
{
std::cout << "connect failed" << endl;
char* pError=OLE2T(bstErrorInfo.bstrVal);
std::cout << pError <<endl;
pSmtpLevel->Release();
CoUninitialize();
return 1;
}
pSmtpLevel->From="gaoyue@leadsky.com";
pSmtpLevel->FromName="高越";
pSmtpLevel->AddAddress("darkprince@leadsky.com","黑王子");
//pSmtpLevel->AddCC("gaoyue@leadsky.com","高越");
pSmtpLevel->Subject="这是一次发送测试";
pSmtpLevel->Body="这是MAIL正文,测试测试!!aaaaaa";
//pSmtpLevel->AddAttachment("D:\\Share\\测试附件.doc");
//pSmtpLevel->AddAttachment("D:\\Share\\aaa.doc");
//pSmtpLevel->SendMail();
CComVariant vbSave;
vbSave.vt=VT_BOOL;
vbSave.boolVal=VARIANT_TRUE;
pSmtpLevel->SendMailForWeb("C:\\Inetpub\\wwwroot\\DarkMail\\","DarkTest",vbSave);
}
catch (_com_error &e)
{
dump_com_error(e);
pSmtpLevel->Release();
CoUninitialize();
return 1;
}
pSmtpLevel->Disconnect();
pSmtpLevel->Release();
CoUninitialize();
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -