开启辅助访问 切换到宽版

精易论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

用微信号发送消息登录论坛

新人指南 邀请好友注册 - 我关注人的新帖 教你赚取精币 - 每日签到


求职/招聘- 论坛接单- 开发者大厅

论坛版规 总版规 - 建议/投诉 - 应聘版主 - 精华帖总集 积分说明 - 禁言标准 - 有奖举报

查看: 3010|回复: 9
收起左侧

[技术专题] COM组件 操作 Outlook 发送邮件 实现

[复制链接]
结帖率:91% (75/82)
发表于 2023-3-6 14:58:30 | 显示全部楼层 |阅读模式   江苏省苏州市
本帖最后由 z13228604287 于 2023-3-6 15:00 编辑

[C++] 纯文本查看 复制代码
Wrap_errno SendMail_UsingOOM(
    const std::wstring& to,
    const std::wstring& cc,
    const std::wstring& bcc,
    const std::wstring& subject,
    const std::wstring& body,
    const std::wstring& attachmentPath,
    bool showUI
) {
    CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);

    CLSID clsid;
    HRESULT hr;

    Wrap_errno err;
    memset(&err, 0, sizeof err);

    LPCOLESTR progID = L"Outlook.Application";
    hr = CLSIDFromProgID(progID, &clsid);
    if (FAILED(hr)) {
        CoUninitialize();
        return err;
    }

    // 创建Outlook对象
    IDispatch* pOutlookApp = NULL;
    hr = CoCreateInstance(
        clsid,                    // CLSID of the server
        NULL,
        CLSCTX_LOCAL_SERVER,    // Outlook.Application is a local server
        IID_IDispatch,            // Query the IDispatch interface
        (void**)&pOutlookApp);    // Output

    if (FAILED(hr)) {
        // 处理错误
        if (pOutlookApp != NULL) {
            pOutlookApp->Release();
        }
        CoUninitialize();
        return err;
    }

    // 获取Namespace对象
    IDispatch* pNS = NULL;
    {
        VARIANT x;
        x.vt = VT_BSTR;
        x.bstrVal = SysAllocString(L"MAPI");

        VARIANT result;
        VariantInit(&result);
        err = AutoWrapA(DISPATCH_METHOD, &result, pOutlookApp, (wchar_t*)L"GetNamespace", 1, x);
        if (err.hr < 0) {
            // 处理错误
            VariantClear(&result);
            VariantClear(&x);
            if (pOutlookApp != NULL) {
                pOutlookApp->Release();
            }
            if (pNS != NULL) {
                pNS->Release();
            }
            CoUninitialize();
            return err;
        }
        pNS = result.pdispVal;
        VariantClear(&x);
    }

    //指定邮箱账户的名称,通常是邮箱地址。
    {
        VARIANT vtNewSession;
        vtNewSession.vt = VT_BSTR;
        vtNewSession.bstrVal = NULL;//空为当前账户

        VARIANT vtShowDialog;
        vtShowDialog.vt = VT_BSTR;
        vtShowDialog.bstrVal = NULL;//空为当前账户密码

        VARIANT vtMissing;
        vtMissing.vt = VT_BOOL;
        vtMissing.boolVal = VARIANT_TRUE;
        
        AutoWrapA(DISPATCH_METHOD, NULL, pNS, (wchar_t*)L"Logon", 3, vtNewSession,
                  vtShowDialog, vtMissing);
        VariantClear(&vtShowDialog);
        VariantClear(&vtNewSession);
        VariantClear(&vtMissing);
    }

    // 创建新邮件
    IDispatch* pMail = NULL;
    {
        VARIANT x;
        x.vt = VT_I4;
        x.lVal = 0;     // Outlook::olMailItem

        VARIANT result;
        VariantInit(&result);
        err = AutoWrapA(DISPATCH_METHOD, &result, pOutlookApp, (wchar_t*)L"CreateItem", 1, x);
        if (err.hr < 0) {
            if (pMail != NULL) {
                pMail->Release();
            }
            if (pNS != NULL) {
                pNS->Release();
            }
            if (pOutlookApp != NULL) {
                pOutlookApp->Release();
            }
            VariantClear(&x);
            VariantClear(&result);
            CoUninitialize();
            return err;
        }
        pMail = result.pdispVal;
    }
    //设置邮件的主题
    // pMail->Subject = _bstr_t(L"Feedback of All-In-One Code Framework");
    {
        VARIANT x;
        x.vt = VT_BSTR;
        x.bstrVal = SysAllocString(subject.c_str());
        AutoWrapA(DISPATCH_PROPERTYPUT, NULL, pMail, (wchar_t*)L"Subject", 1, x);
        VariantClear(&x);
    }
    //设置邮件的收件人
    // pMail->To = _bstr_t(L"codefxf@microsoft.com");
    {
        VARIANT x;
        x.vt = VT_BSTR;
        x.bstrVal = SysAllocString(to.c_str());
        AutoWrapA(DISPATCH_PROPERTYPUT, NULL, pMail, (wchar_t*)L"To", 1, x);
        VariantClear(&x);
    }
    //设置邮件的抄送
    // pMail->cc
    {
        VARIANT x;
        x.vt = VT_BSTR;
        x.bstrVal = SysAllocString(cc.c_str());
        AutoWrapA(DISPATCH_PROPERTYPUT, NULL, pMail, (wchar_t*)L"Cc", 1, x);
        VariantClear(&x);
    }
    //设置邮件的密送
    //// pMail->bcc
    {
        VARIANT x;
        x.vt = VT_BSTR;
        x.bstrVal = SysAllocString(bcc.c_str());
        AutoWrapA(DISPATCH_PROPERTYPUT, NULL, pMail, (wchar_t*)L"Bcc", 1, x);
        VariantClear(&x);
    }

    //邮件内容
    // pMail->body
    {
        VARIANT x;
        x.vt = VT_BSTR;
        x.bstrVal = SysAllocString(body.c_str());
        AutoWrapA(DISPATCH_PROPERTYPUT, NULL, pMail, (wchar_t*)L"Body", 1, x);
        VariantClear(&x);
    }
    //获取MailItem对象
    //添加邮件附件
     //pAtta = pMail->GetAttachments
     //pAtta->Add(source,type,position,displayname)
    IDispatch* pAtta = NULL;
    {
        VARIANT result;
        VariantInit(&result);
        err = AutoWrapA(DISPATCH_PROPERTYGET, &result, pMail, (wchar_t*)L"Attachments", 0);
        if (err.hr < 0)
        {
            if (pMail != NULL)
            {
                pMail->Release();
            }
            if (pNS != NULL)
            {
                pNS->Release();
            }
            if (pOutlookApp != NULL)
            {
                pOutlookApp->Release();
            }
            VariantClear(&result);
            CoUninitialize();
            return err;
        }
        pAtta = result.pdispVal;

        VARIANT path;
        path.vt = VT_BSTR;
        path.bstrVal = SysAllocString(attachmentPath.c_str());
        VARIANT x;
        x.vt = VT_I4;
        x.lVal = 1;
        err = AutoWrapA(DISPATCH_METHOD, NULL, pAtta, (wchar_t*)L"Add", 4, path, x, x, path);
        if (err.hr < 0)
        {
            if (pAtta != NULL)
            {
                pAtta->Release();
            }
            if (pMail != NULL)
            {
                pMail->Release();
            }
            if (pNS != NULL)
            {
                pNS->Release();
            }
            if (pOutlookApp != NULL)
            {
                pOutlookApp->Release();
            }
            VariantClear(&result);
            VariantClear(&path);
            CoUninitialize();
            return err;
        }
        VariantClear(&path);
    }

    //显示邮件
    // pMail->Display(true);
    {
        VARIANT vtModal;
        vtModal.vt = VT_BOOL;
        vtModal.boolVal = VARIANT_TRUE;
        err = AutoWrapA(DISPATCH_METHOD, NULL, pMail, (wchar_t*)L"Display", 1, vtModal);
        if (err.hr < 0) {
            if (pMail != NULL) {
                pMail->Release();
            }
            if (pNS != NULL) {
                pNS->Release();
            }
            if (pOutlookApp != NULL) {
                pOutlookApp->Release();
            }
            VariantClear(&vtModal);
            CoUninitialize();
            return err;
        }
    }

    //发送邮件
    AutoWrapA(DISPATCH_METHOD, NULL, pMail, (wchar_t*)L"Send", 0);

    //执行Outlook应用程序的登出操作。
    // pNS->Logoff()
    err = AutoWrapA(DISPATCH_METHOD, NULL, pNS, (wchar_t*)L"Logoff", 0);
    if (err.hr < 0) {
        if (pMail != NULL) {
            pMail->Release();
        }
        if (pNS != NULL) {
            pNS->Release();
        }
        if (pOutlookApp != NULL) {
            pOutlookApp->Release();
        }
        CoUninitialize();
        return err;
    }

    if (pMail != NULL) {
        pMail->Release();
    }
    if (pNS != NULL) {
        pNS->Release();
    }
    if (pOutlookApp != NULL) {
        pOutlookApp->Release();
    }

    CoUninitialize();
    err.hr = S_OK;
    return err;
}





int main() {
    SendMail_UsingOOM(L"Yong.Han@darwinprecisions.com", L"Yong.Han@darwinprecisions.com", L"Yong.Han@darwinprecisions.com", L"测试", L"It`s OOM Test.Do not care it.", L"", true);
    return 0;
}






签到天数: 4 天

发表于 2023-8-26 09:43:50 | 显示全部楼层   河北省石家庄市
        支持开源~!感谢分享
回复 支持 反对

使用道具 举报

签到天数: 5 天

发表于 2023-5-11 02:53:21 | 显示全部楼层   湖北省武汉市
感谢分享
回复 支持 反对

使用道具 举报

结帖率:79% (11/14)

签到天数: 28 天

发表于 2023-3-8 22:51:02 高大上手机用户 | 显示全部楼层   福建省莆田市
翻译成易语言造福人类
回复 支持 反对

使用道具 举报

结帖率:100% (3/3)

签到天数: 24 天

发表于 2023-3-6 17:47:48 | 显示全部楼层   广东省揭阳市
翻译成易语言可好

点评

需要可以 给你 做成 DLL 或 支持库 私聊   江苏省苏州市  发表于 2023-3-6 19:02
回复 支持 反对

使用道具 举报

结帖率:100% (8/8)

签到天数: 28 天

发表于 2023-3-6 16:59:03 | 显示全部楼层   广东省东莞市
类似vba那样调用

点评

是的 和VBA 一样   江苏省苏州市  发表于 2023-3-6 19:02
回复 支持 反对

使用道具 举报

结帖率:88% (14/16)

签到天数: 22 天

发表于 2023-3-6 16:19:53 | 显示全部楼层   安徽省六安市
我不信,除非你编译出来我能用

点评

测试 2013 可以正常发送 公司邮箱   江苏省苏州市  发表于 2023-3-6 19:02
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 致发广告者

发布主题 收藏帖子 返回列表

sitemap| 易语言源码| 易语言教程| 易语言论坛| 诚聘英才| 易语言模块| 手机版| 广告投放| 精易论坛
拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论,本站内容均为会员发表,并不代表精易立场!
论坛帖子内容仅用于技术交流学习和研究的目的,严禁用于非法目的,否则造成一切后果自负!如帖子内容侵害到你的权益,请联系我们!
防范网络诈骗,远离网络犯罪 违法和不良信息举报电话0663-3422125,QQ: 800073686,邮箱:800073686@b.qq.com
Powered by Discuz! X3.4 揭阳市揭东区精易科技有限公司 ( 粤ICP备12094385号-1) 粤公网安备 44522102000125 增值电信业务经营许可证 粤B2-20192173

快速回复 返回顶部 返回列表