You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

RAD Studio 11 C++使用Outlook发邮件报错,求可用示例代码

旧Borland C++程序迁移至RAD Studio 11:Outlook邮件发送问题及解决方案

问题背景

将旧Borland C程序迁移至RAD Studio 11时,使用Outlook_2K_SRVR.h实现邮件发送功能失败,现寻求RAD Studio 11兼容的C邮件发送示例代码。

当前错误代码

现有无法正常运行的代码片段:

#define  Outlook_2k_olMailItem         OlItemType::olMailItem

//---------------------------------------------------------------------------
#pragma package(smart_init)

//----------------------------------
// emailing ...
//----------------------------------
#pragma link "Outlook_2K_SRVR"  

void __fastcall TLicMainForm::Button1Click(TObject *Sender)
{
    MailItem = (MailItemPtr)(Outlook->CreateItem(Outlook_2k_olMailItem));  
    MailItem->set_To((MailAddressee.w_str()));
    MailItem->set_Subject((MailSubject.w_str()));
    MailItem->set_Body((MailFullText.w_str()));
    MailItem->Attachments->Add( (TVariant)(FDispatch_path), TNoParam(), TNoParam(), TNoParam() );

    MailItem->Send();
}

错误信息

链接时出现以下错误:

[ilink32 Error] Error: Unresolved external '__fastcall Outlook_2k::TSyncObject::Disconnect()' referenced from C:\mypath\OUTLOOK_2K_SRVR.OBJ
[ilink32 Error] Error: Unable to perform link

已尝试使用VCL文件夹中的头文件重新编译Outlook_2k_SRVR.obj,但未解决问题。

RAD Studio 11兼容的邮件发送方案

方案1:直接通过OLE操作Outlook

无需依赖旧组件,直接使用RAD Studio的OLE功能调用Outlook,兼容性更好:

#include <ComObj.hpp>

void __fastcall TLicMainForm::BtnSendOutlookMailClick(TObject *Sender)
{
    Variant OutlookApp, MailItem;
    try
    {
        // 初始化Outlook应用
        OutlookApp = CreateOleObject("Outlook.Application");
        // 创建邮件项(0对应olMailItem枚举值)
        MailItem = OutlookApp.OleFunction("CreateItem", 0);

        // 设置邮件基本信息
        MailItem.OlePropertySet("To", "收件人邮箱@example.com");
        MailItem.OlePropertySet("Subject", "RAD Studio 11测试邮件");
        MailItem.OlePropertySet("Body", "这是通过OLE直接调用Outlook发送的邮件内容");

        // 添加附件(可选,需确保文件存在)
        if (FileExists("C:\\test_file.txt"))
        {
            MailItem.OlePropertyGet("Attachments").OleFunction("Add", "C:\\test_file.txt");
        }

        // 发送邮件
        MailItem.OleFunction("Send");
        ShowMessage("邮件发送成功");
    }
    catch (Exception &e)
    {
        ShowMessage("发送失败:" + e.Message);
    }
    finally
    {
        // 释放资源
        MailItem.Clear();
        OutlookApp.Clear();
    }
}

方案2:使用Indy组件发送SMTP邮件(不依赖Outlook)

如果不需要依赖本地Outlook客户端,推荐使用Indy的SMTP组件,支持多种邮箱服务商:

  1. 先在窗体上添加以下组件:

    • TIdSMTP(Indy Clients面板)
    • TIdMessage(Indy Misc面板)
    • TIdSSLIOHandlerSocketOpenSSL(Indy SSL面板),用于SSL加密连接
  2. 示例代码:

#include <IdSMTP.hpp>
#include <IdMessage.hpp>
#include <IdText.hpp>
#include <IdAttachmentFile.hpp>
#include <IdSSLOpenSSL.hpp>

void __fastcall TLicMainForm::BtnSendSMTPMailClick(TObject *Sender)
{
    try
    {
        // 配置SMTP服务器信息(以QQ邮箱为例)
        IdSMTP1->Host = "smtp.qq.com";
        IdSMTP1->Port = 465;
        IdSMTP1->Username = "你的QQ邮箱@qq.com";
        IdSMTP1->Password = "你的邮箱授权码"; // 注意是授权码而非登录密码
        IdSMTP1->UseTLS = utUseImplicitTLS;
        IdSMTP1->IOHandler = IdSSLIOHandlerSocketOpenSSL1;

        // 配置邮件内容
        IdMessage1->From->Address = "你的QQ邮箱@qq.com";
        IdMessage1->From->Name = "发送者名称";
        IdMessage1->Subject = "Indy SMTP测试邮件";
        IdMessage1->Body->Add("这是使用Indy组件发送的测试邮件内容");

        // 添加收件人
        IdMessage1->Recipients->Add()->Address = "收件人邮箱@example.com";

        // 添加附件(可选)
        if (FileExists("C:\\test_attachment.txt"))
        {
            TIdAttachmentFile *attach = new TIdAttachmentFile(IdMessage1->MessageParts, "C:\\test_attachment.txt");
            attach->FileName = "自定义附件名称.txt";
        }

        // 连接服务器并发送
        IdSMTP1->Connect();
        IdSMTP1->Send(IdMessage1);
        IdSMTP1->Disconnect();

        ShowMessage("邮件发送成功");
    }
    catch (Exception &e)
    {
        ShowMessage("发送失败:" + e.Message);
        if (IdSMTP1->Connected())
            IdSMTP1->Disconnect();
    }
}

问题原因说明

旧的Outlook_2K_SRVR组件是针对早期Borland C++版本和旧版Outlook开发的,与RAD Studio 11的编译器、VCL库存在兼容性差异。链接错误中的未解析外部符号,是因为组件的实现代码依赖的旧库或编译逻辑在新版环境中无法匹配,仅重新编译头文件无法解决该问题。

内容的提问来源于stack exchange,提问作者mvcad

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 14:05:36