惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
H
Help Net Security
博客园_首页
P
Privacy International News Feed
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Tenable Blog
Latest news
Latest news
D
Darknet – Hacking Tools, Hacker News & Cyber Security
爱范儿
爱范儿
Cyberwarzone
Cyberwarzone
P
Palo Alto Networks Blog
月光博客
月光博客
有赞技术团队
有赞技术团队
Know Your Adversary
Know Your Adversary
博客园 - 叶小钗
P
Proofpoint News Feed
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
S
Schneier on Security
A
About on SuperTechFans
F
Full Disclosure
The Cloudflare Blog
T
The Exploit Database - CXSecurity.com
S
SegmentFault 最新的问题
Apple Machine Learning Research
Apple Machine Learning Research
Microsoft Security Blog
Microsoft Security Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
N
News and Events Feed by Topic
www.infosecurity-magazine.com
www.infosecurity-magazine.com
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Webroot Blog
Webroot Blog
量子位
大猫的无限游戏
大猫的无限游戏
小众软件
小众软件
WordPress大学
WordPress大学
Last Week in AI
Last Week in AI
美团技术团队
Help Net Security
Help Net Security
Microsoft Azure Blog
Microsoft Azure Blog
GbyAI
GbyAI
云风的 BLOG
云风的 BLOG
Y
Y Combinator Blog
博客园 - 司徒正美
N
Netflix TechBlog - Medium
S
Security @ Cisco Blogs
B
Blog
P
Privacy & Cybersecurity Law Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
Hacker News - Newest:
Hacker News - Newest: "LLM"
V
Visual Studio Blog
NISL@THU
NISL@THU

博客园 - hcfalan

Mybatis分页功能 安聊服务端Netty的应用 安聊系统1.0发布 自绘窗口边框和标题栏 Windows下生产者-消费者问题的解法 Javascript 时间日期转换 Java Threading - Consumer&Producer Boost socket 同步编程示例(服务端,客户端) MFC CSocket的跨线程问题 用于主题检测的临时日志(54b8134e-5e4a-46fc-95af-c75ccf06d66e - 3bfe001a-32de-4114-a6b4-4005b770f6d7) 网络电话 VC 剪贴板操作 The 1st Boost Serial Demo - hcfalan MS Proxy FTP Sample 《斯坦福大学开放课程: 编程方法学》 VC2005下编译log4cpp的修正方法 - hcfalan - 博客园 适合孩子的书籍 一个最经典的线程类(转) 应用MFC开发高级应用程序(转)
VC中发送电子邮件
hcfalan · 2011-12-02 · via 博客园 - hcfalan

注:原文来自http://www.vckbase.com/document/viewdoc/?id=651

功能介绍:很多时候大家需要在程序中发送邮件,自己编又太麻烦,怎么办,呵呵,有现成的!

1、简单的方案

用ShellExecute函数简单的调用默认的电子邮件程序:

ShellExecute(NULL,NULL,"mailto:email@163.net",NULL,NULL,SW_SHOW);

2、复杂的方案

如果想实现复杂一点儿的功能,如添加多个收件人、主题、附件等,就可以调用系统的MAPI函数。具体的用法可以去查MSDN,都是以MAPI开头的,如MAPILogon、MAPISendMail等。下面演示如何调用默认的邮件程序发送邮件,可以添加多个附件

部分关键代码:

//必须包括 mapi.h 头文件
#include "mapi.h"                     
 
 
/*********************************************************************
 * 函数名称:CSendEMailDlg::OnSendMapi
 * 说明:  调用MAPI函数发送邮件。
 * 作者:  Geng
 * 时间 : 2003-04-22 20:08:30 
*********************************************************************/
void CSendEMailDlg::OnSendMapi() 
{
        UpdateData(true);
 
        //装入MAPI32.DLL动态库
        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;
        }
 
        int nFileCount = m_list.GetCount();   //有多少个附件需要发送
 
        //分配内存保存附件信息 不能使用静态数组,因为不知道要发送附件的个数
        MapiFileDesc* pFileDesc = (MapiFileDesc*)malloc(sizeof(MapiFileDesc) * nFileCount);
        memset(pFileDesc,0,sizeof(MapiFileDesc) * nFileCount);
 
        //分配内存保存附件文件路径
        TCHAR* pTchPath = (TCHAR*)malloc(MAX_PATH * nFileCount);
 
        CString szText;
        for(int i = 0;i < nFileCount;i++)
        {
               TCHAR* p = pTchPath + MAX_PATH * i;
               m_list.GetText(i,szText);
               strcpy(p,szText);
 
               (pFileDesc + i)->nPosition = (ULONG)-1;
               (pFileDesc + i)->lpszPathName = p;
               (pFileDesc + i)->lpszFileName = p;
        }
 
        //收件人结构信息
        MapiRecipDesc recip;
        memset(&recip,0,sizeof(MapiRecipDesc));
        recip.lpszAddress      = m_szEmailMAPI.GetBuffer(0);
        recip.ulRecipClass = MAPI_TO;
 
        //邮件结构信息
        MapiMessage message;
        memset(&message, 0, sizeof(message));
        message.nFileCount     = nFileCount;                         //文件个数
        message.lpFiles        = pFileDesc;                          //文件信息
        message.nRecipCount    = 1;                                  //收件人个数
        message.lpRecips       = &recip;                             //收件人
        message.lpszSubject    = m_szSubject.GetBuffer(0);           //主题
        message.lpszNoteText   = m_szText.GetBuffer(0);              //正文内容
 
        //保存本程序窗口指针,因为发完邮件后要返回本程序的窗口
        CWnd* pParentWnd = CWnd::GetSafeOwner(NULL, NULL);
 
        //发送邮件
        int nError = lpfnSendMail(0, 0,&message, MAPI_LOGON_UI|MAPI_DIALOG, 0);
 
        if (nError != SUCCESS_SUCCESS && nError != MAPI_USER_ABORT 
                       && nError != MAPI_E_LOGIN_FAILURE)
        {
               AfxMessageBox(AFX_IDP_FAILED_MAPI_SEND);
        }
 
        //返回程序
        pParentWnd->SetActiveWindow();
 
        //不要忘了释放分配的内存
        free(pFileDesc);
        free(pTchPath);
        FreeLibrary(hMod);
}