




免费预览已结束,剩余17页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
VC中自动发送邮件的实现现在做一个邮件发送的模块,用于邮件报警的,在网上找了两种方案,一种是调用OUT LOOK EXPREES,用MAPI实现,另一是用SMTP协议,还没想好用哪一个呢.思考中!发送邮件:VC + socket + SMTP用VC发送邮件./ mysmtp.h .#pragma once .#include .#include . .class CSocket; .class CMySmtp . .private: . static const char m_staticBase64CodeTable; . static const std:string MIMEMultipartMixedLogin; . static const std:string MIMETextPlainLogin; . static const std:string MyBoundary; . static const std:string CTCodeQP; . static const std:string CTCodeBase64; . static const std:string CTTextPlainCharCodeGB2312; . static const std:string CTAppOctetStreamName; . static const std:string CDAttachemntFileName; . . struct stSMTP . . int Post; . std:string Host; . std:string UserName; . std:string Password; . std:string MailFrom; . std:string SendTo; . std:string Subject; . std:string Date; . std:string Data; . ; . struct stPathFile . . std:string Path; . std:string File; . ; . . CSocket* m_pSmtpServer; . stSMTP m_stSmtpPro; . std:list m_listAttachmentFileName; . .public: . CMySmtp(void); . virtual CMySmtp(void); . . static int Base64EncodeLen( int nSrcLen ); . static bool Base64Encode( const BYTE* szSrcData, int nSrcLen, BYTE* szDestData, int* pnDestLen ); . . void SetPost( int t ) m_stSmtpPro.Post = t; . void SetHost( std:string t ) m_stSmtpPro.Host = t; . void SetUserName( std:string t ) m_stSmtpPro.UserName = t; . void SetPassword( std:string t ) m_stSmtpPro.Password = t; . void SetMailFrom( std:string t ) m_stSmtpPro.MailFrom = t; . void SetSendTo( std:string t ) m_stSmtpPro.SendTo = t; . void SetSubject( std:string t ) m_stSmtpPro.Subject = t; . void SetDate( std:string t ) m_stSmtpPro.Date = t; . . bool AddDataFromCol( std:string strData ); . bool AddDataFromString( std:string strData ); . bool AddDataFromBuffer( char* strData, int iLen ); . bool AddDataFromFile( std:string FileName ); . void AddAttachment( std:string FilePath, std:string FileName ); . . bool SandMail(); .; . . ./ mysmtp.cpp .#include StdAfx.h .#include mysmtp.h .#include afxsock.h .#include .using namespace std; . ./= ./ Base64 编码表数据 ./= .const char CMySmtp:m_staticBase64CodeTable = . ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/ .; . ./= ./ MIME 一些基本的字符串常量 ./= .const std:string CMySmtp:MIMEMultipartMixedLogin = .X-Mailer: Gddsky mailerrn .MIME-Version: 1.0rn .Content-type: multipart/mixed; .boundary=_000_Gddsky_000_=rn .rn; .const std:string CMySmtp:MIMETextPlainLogin = .X-Mailer: Gddsky mailerrn .MIME-Version: 1.0rn .Content-type: text/plain; charset=GB2312rn .Content-Transfer-Encoding: base64rn .rn; . .const std:string CMySmtp:MyBoundary = rn-=_000_Gddsky_000_=rn; .const std:string CMySmtp:CTCodeQP = Content-Transfer-Encoding: quoted-printablernrn; .const std:string CMySmtp:CTCodeBase64 = Content-Transfer-Encoding: base64rnrn; .const std:string CMySmtp:CTTextPlainCharCodeGB2312 = Content-Type: text/plain; charset=GB2312rn; .const std:string CMySmtp:CTAppOctetStreamName = Content-Type: application/octet-stream; name=; .const std:string CMySmtp:CDAttachemntFileName = Content-Disposition: attachment; filename=; . ./= ./ 类构造与析构 ./ 功能: 建立Socket, 初始 SMTP 默认端口 ./= .CMySmtp:CMySmtp(void) . . m_stSmtpPro.Post = 25; / STMP . m_pSmtpServer = new CSocket(); . if( m_pSmtpServer ) . . m_pSmtpServer-Create(); . . . .CMySmtp:CMySmtp(void) . . m_listAttachmentFileName.clear(); . if( m_pSmtpServer ) . . m_pSmtpServer-Close(); . delete m_pSmtpServer; . . . ./= ./ 字符串转Base64 : RFC 2045 ./ 看 : /rfc/rfc2045.txt ./= .int CMySmtp:Base64EncodeLen( int nSrcLen ) . . int nRet = nSrcLen * 4 / 3 + nSrcLen % 3; . int nCRLFs = ( nRet / 76 + 1 ) * 2; / 每行多加rn . int nOnLastLine = nRet % 76; . . nRet += nCRLFs; . . if( nOnLastLine & nOnLastLine % 4 ) . . nRet += 4 - ( nOnLastLine % 4 ); . . . return nRet; . . .bool CMySmtp:Base64Encode( const BYTE* szSrcData, int nSrcLen, BYTE* szDestData, int* pnDestLen ) . . if( !( szSrcData & nSrcLen & szDestData & pnDestLen & *pnDestLen ) ) . . return false; . . . if( *pnDestLen Base64EncodeLen( nSrcLen ) ) . . return false; . . . int nWritten = 0 ; . int nLen1 = ( nSrcLen / 3 ) * 4; . int nLen2 = nLen1 / 76; . int nLen3 = 19; . . for( int i = 0; i = nLen2; i+ ) . . if( i = nLen2 ) . . nLen3 = ( nLen1 % 76 ) / 4; . . for( int j = 0; j nLen3; j+ ) . . DWORD dwCurr = 0; . for( int n = 0; n 3; n+ ) . . dwCurr |= *szSrcData+; . dwCurr = 8; . . for( int k = 0; k 26 ); . *szDestData+ = m_staticBase64CodeTable b ; . dwCurr = 6; . . . nWritten += nLen3 * 4; . . *szDestData+ = r; . *szDestData+ = n; . nWritten+= 2; . . . if( nWritten ) . . szDestData-= 2; . nWritten -= 2; . . . nLen2 = ( nSrcLen % 3 ) ? ( nSrcLen % 3 ) + 1 : 0; . if( nLen2 ) . . DWORD dwCurr = 0; . for( int n = 0; n 3; n+ ) . . if( n ( nSrcLen % 3 ) ) . . dwCurr |= *szSrcData+; . . dwCurr = 8; . . for( int k = 0; k 26 ); . *szDestData+ = m_staticBase64CodeTable b ; . dwCurr = 6; . . nWritten += nLen2; . . nLen3 = nLen2 ? 4 - nLen2 : 0; . for( int j = 0; j Connect( m_stSmtpPro.Host.c_str(), m_stSmtpPro.Post ) = FALSE ) . . return false; . . . string strSendBuf; . int nTemp = 1024; . char strBuf 1024 ; . memset( strBuf, 0, 1024 ); . . /- . / 邮件头 . . strSendBuf.append( AUTH LOGINrn ); . . Base64Encode( (const BYTE*)m_stSmtpPro.UserName.c_str(), m_stSmtpPro.UserName.size(), (BYTE*)strBuf, &nTemp ); . strSendBuf.append( strBuf ); . . nTemp = 1024; . memset( strBuf, 0, 1024 ); . Base64Encode( (const BYTE*)m_stSmtpPro.Password.c_str(), m_stSmtpPro.Password.size(), (BYTE*)strBuf, &nTemp ); . strSendBuf.append( strBuf ); . . strSendBuf.append( MAIL FROM: rn ); . . strSendBuf.append( RCPT TO: rn ); . . strSendBuf.append( DATArn ); . . strSendBuf.append( From: rn ); . . strSendBuf.append( To: rn ); . . strSendBuf.append( SUBJECT: ); . strSendBuf.append( m_stSmtpPro.Subject.c_str() ); . strSendBuf.append( rn ); . . strSendBuf.append( Date: ); . strSendBuf.append( m_stSmtpPro.Date.c_str() ); . strSendBuf.append( rn ); . . if( m_listAttachmentFileName.size() ) . . strSendBuf.append( MIMEMultipartMixedLogin.c_str() ); . . else . . strSendBuf.append( MIMETextPlainLogin.c_str() ); . . . m_pSmtpServer-Send( strSendBuf.c_str(), strSendBuf.size() ); . . /- . . /- . / 邮件内容, 以Base64加密 . . if( m_listAttachmentFileName.size() ) . . strSendBuf.clear(); . strSendBuf.append( MyBoundary.c_str() ); . strSendBuf.append( CTTextPlainCharCodeGB2312.c_str() ); . strSendBuf.append( CTCodeBase64.c_str() ); . m_pSmtpServer-Send( strSendBuf.c_str(), strSendBuf.size() ); . . . strSendBuf.clear(); . int nSize = Base64EncodeLen( m_stSmtpPro.Data.size() ); . strSendBuf.resize( nSize ); . Base64Encode( (const BYTE*)m_stSmtpPro.Data.c_str(), m_stSmtpPro.Data.size(), (BYTE*)strSendBuf.c_str(), &nSize ); . m_pSmtpServer-Send( strSendBuf.c_str(), strSendBuf.size() ); . . /- . . /- . / 邮件附件 . for( list:iterator it = m_listAttachmentFileName.begin(); . it != m_listAttachmentFileName.end(); it+ ) . . string strFile = ( *it ).Path + ( *it ).File; . string strFileData; . . / 读文件 . ifstream InputFile; . InputFile.open( strFile.c_str(), ios_base:binary | ios_base:in ); . if( InputFile.fail() ) . . it+; . continue; . . InputFile.seekg( 0, ios_base:end ); . int iLen = InputFile.tellg(); . InputFile.seekg( 0, ios_base:beg ); . . strFileData.resize( iLen ); . InputFile.read( (char*)strFileData.c_str(), iLen ); . . / 加入一个附件头 . strSendBuf.clear(); . strSendBuf.append( MyBoundary.c_str() ); . strSendBuf.append( CTAppOctetStreamName.c_str() ); . strSendBuf.append( ( *it ).File.c_str() ); . strSendBuf.append( rn ); . strSendBuf.append( CDAttachemntFileName.c_str() ); . strSendBuf.append( ( *it ).File.c_str() ); . strSendBuf.append( rn ); . strSendBuf.append( CTCodeBase64.c_str() ); . m_pSmtpServer-Send( strSendBuf.c_str(), strSendBuf.size() ); . . / 附件数据, Base64加密 . int nSize = Base64EncodeLen( strFileData.size() ); . strSendBuf.clear(); . strSendBuf.resize( nSize ); . Base64Encode( (const BYTE*)strFileData.c_str(), strFileData.size(), (BYTE*)strSendBuf.c_str(), &nSize ); . . m_pSmtpServer-Send( strSendBuf.c_str(), strSendBuf.size() ); . . /- . . /- . / 邮件结束 . strSendBuf.clear(); . strSendBuf = rn.rnQUITrn; . . m_pSmtpServer-Send( strSendBuf.c_str(), strSendBuf.size() ); . /- . . return true; .在VC中调用默认的电子邮件程序发送邮件很多时候大家需要在程序中发送邮件,自己编又太麻烦,怎么办,呵呵,有现成的!1、想省事儿的,用ShellExecute函数:ShellExecute(NULL,NULL,mailto:,NULL,NULL,SW_SHOW);2、如果想自己多处理一些东西的话,比如加上默认的帐号、密码、附件等,就可以调用系统的Mapi函数。具体的用法大家可以去查MSDN都是以MAPI开头的,如MAPILogon、MAPISendMail等。下面这段代码演示如何调用默认的邮件程序发送邮件。#include mapi.h void CTestMapiDlg:OnSendMail() HMODULE hMod = LoadLibrary(MAPI32.DLL);if (hMod = NULL)AfxMessageBox(AFX_IDP_FAILED_MAPI_LOAD);return;ULONG (PASCAL *lpfnSendMail)(ULONG, ULONG, MapiMessage*, FLAGS, ULONG);(FARPROC&)lpfnSendMail = GetProcAddress(hMod, MAPISendMail);if (lpfnSendMail = NULL)AfxMessageBox(AFX_IDP_INVALID_MAPI_DLL);return;ASSERT(lpfnSendMail != NULL);TCHAR szPath_MAX_PATH = C:Winntsetup.log;TCHAR szTitle_MAX_PATH = setup.log;/ prepare the file description (for the attachment)MapiFileDesc fileDesc;memset(&fileDesc, 0, sizeof(fileDesc);fileDesc.nPosition = (ULONG)-1;fileDesc.lpszPathName = szPath;fileDesc.lpszFileName = szTitle;/ prepare the message (empty with 1 attachment)MapiMessage message;memset(&message, 0, sizeof(message);message.nFileCount = 1;message.lpFiles = &fileDesc;int nError = lpfnSendMail(0, 0,&message, MAPI_LOGON_UIMAPI_DIALOG, 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.if (nError != SUCCESS_SUCCESS &nError != MAPI_USER_ABORT & nError != MAPI_E_LOGIN_FAILURE) AfxMessageBox(AFX_IDP_FAILED_MAPI_SEND);VC+中进程与多进程管理的方法2007-02-15 12:34摘要:本文主要介绍了多任务管理中的多进程管理技术,对进程的互斥运行、子进程的创建与结束等作了较详细的阐述。进程是当前操作系统下一个被加载到内存的、正在运行的应用程序的实例。每一个进程都是由内核对象和地址空间所组成的,内核对象可以让系统在其内存放有关进程的统计信息并使系统能够以此来管理进程,而地址空间则包括了所有程序模块的代码和数据以及线程堆栈、堆分配空间等动态分配的空间。进程仅仅是一个存在,是不能独自完成任何操作的,必须拥有至少一个在其环境下运行的线程,并由其负责执行在进程地址空间内的代码。在进程启动的同时即同时启动了一个线程,该线程被称作主线程或是执行线程,由此线程可以继续创建子线程。如果主线程退出,那么进程也就没有存在的可能了,系统将自动撤消该进程并完成对其地址空间的释放。加载到进程地址空间的每一个可执行文件或动态链接库文件的映象都会被分配一个与之相关联的全局唯一的实例句柄(Hinstance)。该实例句柄实际是一个记录有进程加载位置的基本内存地址。进程的实例句柄在程序入口函数WinMain()中通过第一个参数HINSTANCEhinstExe传递,其实际值即为进程所使用的基本地址空间的地址。对于VC+链接程序所链接产生的程序,其默认的基本地址空间地址为0x00400000,如没有必要一般不要修改该值。在程序中,可以通过GetModuleHandle()函数得到指定模块所使用的基本地址空间。子进程的创建进程的创建通过CreateProcess()函数来实现,CreateProcess()通过创建一个新的进程及在其地址空间内运行的主线程来启动并运行一个新的程序。具体的,在执行CreateProcess()函数时,首先由操作系统负责创建一个进程内核对象,初始化计数为1,并立即为新进程创建一块虚拟地址空间。随后将可执行文件或其他任何必要的动态链接库文件的代码和数据装载到该地址空间中。在创建主线程时,也是首先由系统负责创建一个线程内核对象,并初始化为1。最后启动主线程并执行进程的入口函数WinMain(),完成对进程和执行线程的创建。CreateProcess()函数的原型声明如下:BOOLCreateProcess(LPCTSTRlpApplicationName,/可执行模块名LPTSTRlpCommandLine,/命令行字符串LPSECURITY_ATTRIBUTESlpProcessAttributes,/进程的安全属性LPSECURITY_ATTRIBUTESlpThreadAttributes,/线程的安全属性BOOLbInheritHandles,/句柄继承标志DWORDdwCreationFlags,/创建标志LPVOIDlpEnvironment,/指向新的环境块的指针LPCTSTRlpCurrentDirectory,/指向当前目录名的指针LPSTARTUPINFOlpStartupInfo,/指向启动信息结构的指针LPPROCESS_INFORMATIONlpProcessInformation/指向进程信息结构的指针);在程序设计时,某一个具体的功能模块可以通过函数或是线程等不同的形式来实现。对于同一进程而言,这些函数、线程都是存在于同一个地址空间下的,而且在执行时,大多只对与其相关的一些数据进行处理。如果算法存在某种错误,将有可能破坏与其同处一个地址空间的其他一些重要内容,这将造成比较严重的后果。为保护地址空间中的内容可以考虑将那些需要对地址空间中的数据进行访问的操作部分放到另外一个进程的地址空间中运行,并且只允许其访问原进程地址空间中的相关数据。具体的,可在进程中通过CreateProcess()函数去创建一个子进程,子进程在全部处理过程中只对父进程地址空间中的相关数据进行访问,从而可以保护父进程地址空间中与当前子进程执行任务无关的全部数据。对于这种情况,子进程所体现出来的作用同函数和线程比较相似,可以看成是父进程在运行期间的一个过程。为此,需要由父进程来掌握子进程的启动、执行和退出。下面这段代码即展示了此过程:/临时变量CStringsCommandLine;charcWindowsDirectoryMAX_PATH;charcCommandLineMAX_PATH;DWORDdwExitCode;PROCESS_INFORMATIONpi;STARTUPINFOsi=sizeof(si);/得到Windows目录GetWindowsDirectory(cWindowsDirectory,MAX_PATH);/启动记事本程序的命令行sCommandLine=CString(cWindowsDirectory)+NotePad.exe;:strcpy(cCommandLine,sCommandLine);/启动记事本作为子进程BOOLret=CreateProcess(NULL,cCommandLine,NULL,NULL,FALSE,0,NULL,NULL,&si,&pi);if(ret)/关闭子进程的主线程句柄CloseHandle(pi.hThread);/等待子进程的退出WaitForSingleObject(pi.hProcess,INFINITE);/获取子进程的退出码GetExitCodeProcess(pi.hProcess,&dwExitCode);/关闭子进程句柄CloseHandle(pi.hProcess);此段代码首先通过CreateProcess()创建Windows自带的“记事本”程序为子进程,子进程启动后父进程通过WaitForSingleObject()函数等待其执行的结束,在子进程没有退出前父进程是一直处于阻塞状态的,这里子进程的作用同单线程中的函数类似。一旦子进程退出,WaitForSingleObject()函数所等待的pi.hProcess对象将得到通知,父进程将得以继续,如有必要可以通过GetExitCodeProcess()来获取子进
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 中国广电来宾市2025秋招笔试行测题库及答案市场与服务类
- 保山市中石油2025秋招心理测评常考题型与答题技巧
- 莆田市中石化2025秋招面试半结构化模拟题及答案炼油工艺技术岗
- 国家能源齐齐哈尔市2025秋招笔试题库含答案
- 2025年电厂安装考试题及答案
- 中国广电黄石市2025秋招市场与服务类专业追问清单及参考回答
- 亳州市中石化2025秋招面试半结构化模拟题及答案数智化与信息工程岗
- 达州市中石油2025秋招笔试模拟题含答案炼油设备技术岗
- 教育学章节测试题及答案
- 国家能源太原市2025秋招笔试言语理解与表达题专练及答案
- 水库水坝施工方案范本
- 肺康复个案护理
- 人美版美术六年级上册全册教案
- GB/T 21499-2024粮油检验稻谷和糙米潜在出米率测定方法
- (版)科学道德与学风建设题库
- GB/Z 44314-2024生物技术生物样本保藏动物生物样本保藏要求
- 2023年全国职业院校技能大赛-融媒体内容策划与制作赛项规程
- 《电力建设施工企业安全生产标准化实施规范》
- 糖尿病周围神经病变知多少课件
- 儿童肺炎支原体肺炎诊疗指南(2023年版)解读
- 个人履职考核情况表
评论
0/150
提交评论