Delphi用Indy10实现smtp发送邮件支持附件

Delphi使用SMTP服务实现邮件发送的实践教程 在Delphi中发送邮件,开发者通常会利用内置的组件如TIdSMTP和TIdMessage,或第三方库如Indy和Synapse。这些组件和库能够简化邮件发送过程,并提供丰富的接口来进行邮件的创建、配置以及发送。简单邮件传输协议(SMTP)是用于发送电子邮件的一个标准协议。SMTP服务器之间以及发送者和服务器之间利用这个协议进行通信,确保信息能够准确无误地送达。SMTP协议通常工作在TCP/IP网络环境中,端口号为25。SMTP协议的核心功能是管理邮件的发送工作,但它并不处理邮件的接收。 阅读详情

原先使用了第三方插件EASendMail SMTP Component来实现发送邮件,谁知道授权过期了,无奈只好求助于Indy,Google上翻阅了相关资料一番,发现现成的代码并不能满足我的需求,就在原基础上加以修改,添加了HTML格式化和添加附件接口。


//revise by bccsafe //add property edFormatHTML, edAttachmentList //support indy10 and above version! interface uses IdMessage, Classes, IdSMTP, IdMessageBuilder;
const
    SMTP_PORT_EXPLICIT_TLS = 587;


type
    TSSLEmail = class(TObject)
    private
        IdMessage: TIdMessage;
        SMTP: TIdSMTP;
        FedBody: TStrings;
        FedSMTPPort: Integer;
        FedToEmail: string;
        FedSubject: string;
        FedSMTPServer: string;
        FedCCEmail: string;
        FedPassword: string;
        FedBCCEmail: string;
        FedSenderName: string;
        FedUserName: string;
        FedPriority: TIdMessagePriority;
        FedSenderEmail: string;
        FedSSLConnection: Boolean;
        FedAttachmentList: TStrings;
        FedFormatHTML: Boolean;
 // Getter / Setter procedure SetBody(const Value: TStrings);
        procedure Init;
        procedure InitMailMessage;
        procedure InitSASL;
        procedure AddSSLHandler;
        procedure SetedAttachmentList(const Value: TStrings);
        procedure SetedFormatHTML(const Value: Boolean);
    public
        constructor Create; overload;
        constructor Create(const ASMTPServer: string; const ASMTPPort: Integer; const AUserName, APassword: string); overload;
        destructor Destroy; override;
        procedure SendEmail;
 // Properties property edBCCEmail: string read FedBCCEmail write FedBCCEmail;
        property edBody: TStrings read FedBody write SetBody;
        property edCCEmail: string read FedCCEmail write FedCCEmail;
        property edPassword: string read FedPassword write FedPassword;
        property edPriority: TIdMessagePriority read FedPriority write FedPriority;
        property edSenderEmail: string read FedSenderEmail write FedSenderEmail;
        property edSenderName: string read FedSenderName write FedSenderName;
        property edSMTPServer: string read FedSMTPServer write FedSMTPServer;
        property edSMTPPort: Integer read FedSMTPPort write FedSMTPPort;
        property edSSLConnection: Boolean read FedSSLConnection write FedSSLConnection;
        property edToEmail: string read FedToEmail write FedToEmail;
        property edUserName: string read FedUserName write FedUserName;
        property edSubject: string read FedSubject write FedSubject;
        property edFormatHTML: Boolean read FedFormatHTML write SetedFormatHTML;
        property edAttachmentList: TStrings read FedAttachmentList write SetedAttachmentList;
    end;


implementation


uses
    IdComponent, IdTCPConnection, IdTCPClient, IdExplicitTLSClientServerBase,
    IdMessageClient, IdSMTPBase, IdBaseComponent, IdIOHandler, IdIOHandlerSocket,
    IdIOHandlerStack, IdSSL, IdSSLOpenSSL, IdSASLLogin, IdSASL_CRAM_SHA1, IdSASL,
    IdSASLUserPass, IdSASL_CRAMBase, IdSASL_CRAM_MD5, IdSASLSKey, IdSASLPlain,
    IdSASLOTP, IdSASLExternal, IdSASLDigest, IdSASLAnonymous, IdUserPassProvider;


constructor TSSLEmail.Create;
begin
    inherited;
    Init;
    FedBody := TStringList.Create;
    edAttachmentList := TStringList.Create;
end;


procedure TSSLEmail.Init;
begin
    edSSLConnection := True;
    edPriority := TIdMessagePriority.mpNormal;
end;


constructor TSSLEmail.Create(const ASMTPServer: string; const ASMTPPort: Integer; const AUserName, APassword: string);
begin
    Create;
    edSMTPServer := ASMTPServer;
    edSMTPPort := ASMTPPort;
    edUserName := AUserName;
    edPassword := APassword;
end;


destructor TSSLEmail.Destroy;
begin
    edBody.Free;
    edAttachmentList.Free;
    inherited;
end;
 // Setter / Getter -----------------------------------------------------------


procedure TSSLEmail.SetBody(const Value: TStrings);
begin
    FedBody.Assign(Value);
end;


procedure TSSLEmail.SetedAttachmentList(const Value: TStrings);
begin
    FedAttachmentList := Value;
end;


procedure TSSLEmail.SetedFormatHTML(const Value: Boolean);
begin
    FedFormatHTML := Value;
end;
 // Send the mail -------------------------------------------------------------


procedure TSSLEmail.SendEmail;
begin
    IdMessage := TIdMessage.Create;
    try
        InitMailMessage;
        SMTP := TIdSMTP.Create;
        try
            if edSSLConnection then
            begin
                AddSSLHandler;
                if edSMTPPort = SMTP_PORT_EXPLICIT_TLS then
                    SMTP.UseTLS := utUseExplicitTLS
                else
                    SMTP.UseTLS := utUseImplicitTLS;
            end;
            if (edUserName <> '') or (edPassword <> '') then
            begin
                SMTP.AuthType := satSASL;
                InitSASL;
            end
            else
            begin
                SMTP.AuthType := satNone;
            end;
            SMTP.Host := edSMTPServer;
            SMTP.Port := edSMTPPort;
            SMTP.ConnectTimeout := 30000;
            SMTP.UseEHLO := True;
            SMTP.Connect;
            try
                SMTP.Send(IdMessage);
            finally
                SMTP.Disconnect;
            end;
        finally
            SMTP.Free;
        end;
    finally
        IdMessage.Free;
    end;
end;
 // Prepare the mail ----------------------------------------------------------


procedure TSSLEmail.InitMailMessage;
var
    Index: Integer;
begin
    with TIdMessageBuilderHtml.Create do
    try
        HtmlCharSet := 'UTF-8';
        if edFormatHTML then
            Html.Text := FedBody.Text
        else
            PlainText.Text := FedBody.Text;
        for Index := 0 to FedAttachmentList.Count - 1 do
            Attachments.Add(edAttachmentList[Index]);
        FillMessage(IdMessage);
    finally
        Free;
    end;
    IdMessage.Charset := 'UTF-8';
    IdMessage.Body := edBody;
    IdMessage.Sender.Text := edSenderEmail;
    IdMessage.From.Name := edSenderName;
    IdMessage.From.Address := edSenderEmail;
    IdMessage.ReplyTo.EMailAddresses := edSenderEmail;
    IdMessage.Recipients.EMailAddresses := edToEmail;
    IdMessage.Subject := edSubject;
    IdMessage.Priority := edPriority;
    IdMessage.CCList.EMailAddresses := edCCEmail;
    IdMessage.ReceiptRecipient.Text := '';
    IdMessage.BccList.EMailAddresses := edBCCEmail;
end;


procedure TSSLEmail.AddSSLHandler;
var
    SSLHandler: TIdSSLIOHandlerSocketOpenSSL;
begin
    SSLHandler := TIdSSLIOHandlerSocketOpenSSL.Create(SMTP);
 // SSL/TLS handshake determines the highest available SSL/TLS version dynamically SSLHandler.SSLOptions.Method := sslvSSLv23;
    SSLHandler.SSLOptions.Mode := sslmClient;
    SSLHandler.SSLOptions.VerifyMode := [];
    SSLHandler.SSLOptions.VerifyDepth := 0;
    SMTP.IOHandler := SSLHandler;
end;


procedure TSSLEmail.InitSASL;
var
    IdUserPassProvider: TIdUserPassProvider;
    IdSASLCRAMMD5: TIdSASLCRAMMD5;
    IdSASLCRAMSHA1: TIdSASLCRAMSHA1;
    IdSASLPlain: TIdSASLPlain;
    IdSASLLogin: TIdSASLLogin;
    IdSASLSKey: TIdSASLSKey;
    IdSASLOTP: TIdSASLOTP;
    IdSASLAnonymous: TIdSASLAnonymous;
    IdSASLExternal: TIdSASLExternal;
begin
    IdUserPassProvider := TIdUserPassProvider.Create(SMTP);
    IdUserPassProvider.Username := edUserName;
    IdUserPassProvider.Password := edPassword;
    IdSASLCRAMSHA1 := TIdSASLCRAMSHA1.Create(SMTP);
    IdSASLCRAMSHA1.UserPassProvider := IdUserPassProvider;
    IdSASLCRAMMD5 := TIdSASLCRAMMD5.Create(SMTP);
    IdSASLCRAMMD5.UserPassProvider := IdUserPassProvider;
    IdSASLSKey := TIdSASLSKey.Create(SMTP);
    IdSASLSKey.UserPassProvider := IdUserPassProvider;
    IdSASLOTP := TIdSASLOTP.Create(SMTP);
    IdSASLOTP.UserPassProvider := IdUserPassProvider;
    IdSASLAnonymous := TIdSASLAnonymous.Create(SMTP);
    IdSASLExternal := TIdSASLExternal.Create(SMTP);
    IdSASLLogin := TIdSASLLogin.Create(SMTP);
    IdSASLLogin.UserPassProvider := IdUserPassProvider;
    IdSASLPlain := TIdSASLPlain.Create(SMTP);
    IdSASLPlain.UserPassProvider := IdUserPassProvider;
    SMTP.SASLMechanisms.Add.SASL := IdSASLCRAMSHA1;
    SMTP.SASLMechanisms.Add.SASL := IdSASLCRAMMD5;
    SMTP.SASLMechanisms.Add.SASL := IdSASLSKey;
    SMTP.SASLMechanisms.Add.SASL := IdSASLOTP;
    SMTP.SASLMechanisms.Add.SASL := IdSASLAnonymous;
    SMTP.SASLMechanisms.Add.SASL := IdSASLExternal;
    SMTP.SASLMechanisms.Add.SASL := IdSASLLogin;
    SMTP.SASLMechanisms.Add.SASL := IdSASLPlain;
end;

Delphi SMTP邮件发送程序源码解析与实战 SMTP,即简单邮件传输协议(Simple Mail Transfer Protocol),是一种用于在两台或多台服务器之间传输电子邮件的标准。SMTP协议工作在TCP/IP协议簇的应用层,其端口号通常为25。SMTP协议规定了邮件从发送者到接收者的传递过程,包含邮件服务器之间的身份验证、传输、和提交信息等步骤。 阅读详情

相关推荐

利用Delphi实现匿名邮件发送的完整指南

Delphi,作为一种强大的编程语言,主要特点是快速开发能力、丰富的组件库以及对数据库编程的优秀支持。它使用Object Pascal作为其主要编程语言,其编译器可以生成效率极高的本地代码,为开发者提供了一个灵活而又高效的编程平台。简单邮件传输协议(SMTP)是一种用于电子邮件传输的协议。它定义了邮件服务器之间以及邮件客户端与服务器之间进行消息交换的标准。SMTP通常用于将邮件从发送方的服务器投递到接收方的服务器。Delphi开发者在编写邮件发送程序时,往往需要与SMTP协议打交道。

weixin_33363025的博客 1003

Delphi 发送邮件!

{拉2个控件下来IdSMTP1(在Indy Clients页面), IdMessage1(在Indy Misc页面) 实现从XXOO@163.com邮箱发送带附件的邮件到10086@qq.com邮箱} procedure TForm1.SendMail; begin with IdSMTP1 do begin Host:='smtp.163.com'; Port:=25

easyboot的专栏 3909

Delphi环境下实现电子邮件发送功能完整指南

回过头来看,Delphi虽然不像Python那样流行,也不像Java那样生态庞大,但它在某些特定领域依旧不可替代。尤其是当你需要一个小巧、高效、脱离运行时依赖的Windows服务程序时,Delphi+Indy的组合依然是王炸。而通过这次对TIdSMTP和TIdMessage的深入剖析,你会发现:💡真正的工程能力,不在于会不会调API,而在于能不能把一个个零散的功能点,编织成一张稳定、可扩展、易维护的系统之网。

weixin_35835030的博客 1878

在 Visual C++ - 使用 SMTP 服务器发送电子邮件

要在您的 C++ 项目中使用 EASendMail SMTP ActiveX 对象,第一步是“将 EASendMail 的头文件添加到您的项目中”。请转到 或文件夹,找到和,然后将这些文件复制到您的项目文件夹中。EASendMail 是一个 SMTP 组件,支持 SMTP/ESMTP 协议(​​RFC 821、RFC 822、RFC 2554)的所有操作。为了正确运行,请将、、、、值更改为您的值。以下 C++ 示例代码演示如何使用 SMTP 服务器撰写和发送文本/纯文本格式的电子邮件。

ljygood2的博客 922

Delphi邮件收发程序实践指南

Indy是Internet Direct的缩写,它是一个独立的开源控件集合,用于网络编程。Indy项目提供了一套用于Internet协议的客户端和服务器端组件。它被广泛应用于Delphi和C++ Builder环境中,为开发者提供一种简单的方法来实现基于TCP/IP的网络通信。Indy集包含了大量的组件,但是这里我们关注几个关键组件,如TCP/UDP客户端组件、HTTP和FTP客户端组件,以及SSL和SSH等高级组件。POP3的会话过程可以分为三个阶段:认证阶段、事务处理阶段和更新阶段。

weixin_29025501的博客 1180

用 Delphi 实现一个基本的网页邮件抓取和发送功能

如何用 Delphi 实现一个基本的网页邮件抓取和发送功能。以下示例仅作为概念验证,实际应用中需要考虑更多的细节和技术问题。

嵌入式行业打工人,不定期更新博客。 661

delphi indy_使用Delphi和Indy发送电子邮件(和附件)

delphi indy Below are instructions for creating an "email sender" that includes an option for sending email messages and attachments directly from a Delphi application. Before we begin, consider the ...

cumao2792的博客 1326

SMTP破解器(25.80.465.587端口)

邮件破解器,支持国内外所有邮箱类型,破解速度快!软件占用内存小,适合服务器批量登陆

在Delphi中使用Indy控件实现邮件群发

    随着人们交往的不断扩大,人与人、单位与单位之间的邮件通讯日益普及,通过邮件群发,可以将会议通知、产品说明、产品报价等通过邮件方式把大量邮件送到用户的邮箱中,既加快了信息的传递,又节省了费用。目前网上有许多邮件群发是基于Delphi5中的TNMSMTP控件的,很多的SMTP邮件服务器要对发送邮件的用户进行身份验证,即要求给出用户名和密码信息,否则无法发送,包括163.net,

阿鑫的家 1832

用Delphi2010 实现邮件附件收发功能

用Delphi2010 实现邮件附件收发功能 TIdPOP3组件简介 TIdPOP3 是用来接收邮件服务器的邮件信息到用户端的一个组件。它实现了RFC 1939协议。 在使用TIdPOP3组件时需设置它的几个成员属性。 Host :指定邮件服务器,一般为pop3邮件服务器的地址,如 pop3.126.com。 Username :用户名,也就是邮箱名,如billanking2002@126.com。 Password :邮箱密码,在进行收发邮件时组件需要凭密码进行登录。 其它成员属性 Connected:返回它与邮件服务器的连接状态,这true表示已经连接。 CheckMessages:邮件数,如果连接服务器成功,则可以获得服务器端的邮件数。 成员函数 procedure Connect(const ATimeout: Integer = IdTimeoutDefault); 与服务器连接函数。参数为无效连接时等待的毫秒数。 function RetrieveHeader(const MsgNum: Integer; AMsg: TIdMessage): Boolean; 接收邮件头信息,它有两个参数,MsgNum表示在接收第几个邮件,从1开始,AMsg为邮件消息组件实例。 function Retrieve(const MsgNum: Integer; AMsg: TIdMessage): Boolean; 接收邮件主体信息,它与 RetrieveHeader的参数是一样的。接收的邮件内容将保存在AMsg中。 function Delete(const MsgNum: Integer): Boolean; 删除邮件服务器中第几个邮件。从1开始。 procedure Disconnect; override; 关闭连接。 TIdMessage组件简介 TIdMessage用来支持邮件消息协议,如POP3,SMTP,NNTP等。TIdMessage支持多用途Internet邮件扩展(MIME)协议。 常用的TIdMessage的属性: Subject:邮件主题,这个字符串经过BASE64编码的。所以在使用时需对它进行解码。 MessageParts:这是TIdMessageParts类的一个实例,它用来存储邮件的信息。如邮件内容及附件信息。在进行解析时需要判断它是否为附件或文本,如果为附件时,其文件名是经过BASE64编码的。判断常量分别为TIdText , TIdAttachment。 Body:这是个字符串列表,包含构成该邮件的正文内容。 Form:发送邮件者的地址信息。 Recipients:收件人地址信息。 BccList:抄送地址列表。 CharSet:指示邮件信息中使用的字符集。 ContentType:指定MIME媒体数据类型,描述正文中包含的数据,使用户代理决定如何显示数据,常用的有text/html,text/xml。 TIdSMTP组件简介 TIdSMTP是TIdMessageClient派生出的一个简单邮件传输协议和SMTP客户端。 它的主要功能是发送邮件信息。 常用的属性: Host :SMTP邮件服务器的地址,如smtp.126.com。它与上面的POP3地址不一样。 AuthenticationType:服务器认证类型,它有atNone,atLogin两种,即不需要认证和需要凭用户名和密码进行认证。 Username:用户名,这里与TIdPOP3 有点不一样,即它不需要后缀,如billanking2002 Password:邮箱登录密码。如果AuthenticationType设置了atLongin则必须设置密码和用户名。

delphi2010版本IMAP协议下载邮件,能自动获取大附件下载地址

delphi2010版本IMAP协议下载邮件,能自动获取大附件下载地址,自动保存。

Delphi 使用Indy发送Gmail账号邮件

一旦你把你的电子邮件帐户转移到GMail(甚至是整个域名),你就必须修正你在Delphi程序中用于自动生成和发送电子邮件的代码。Gmail使用25、465和587作为SMTP,但由于几乎所有的ISP都屏蔽了25,465是Gmail首选的带SSL的SMTP,而587是首选的带TLS的SMTP端口。当然,你既可以向你的Gmail账户发送信息,也可以向你的GMail账户中的任何其他账户发送信息。下面是一个演示程序的代码,其中有DFM文件和实际的Delphi代码(这和你一般使用的代码一样)。

sensor_WU 博客 1894

indy10 ssl 发送邮件 dll

用Indy10中的TIdSMTP发送邮件,按以下方法可以实现SSL安全连接: 1 把附件文件解压缩后的两个dll放到程序文件夹下 2 增加一个TIdSSLIOHandlerSocketOpenSSL控件 3 两个控件的关键属性设置如下: object IdSMTP1: TIdSMTP IOHandler = IdSSLIOHandlerSocketOpenSSL1 Host = 'smtp.qq.com' Port = 465 UseTLS = utUseImplicitTLS end object IdSSLIOHandlerSocketOpenSSL1: TIdSSLIOHandlerSocketOpenSSL Destination = 'smtp.qq.com:465' Host = 'smtp.qq.com' Port = 465 end 以上方法,用Delphi6+Indy10.5.5测试通过。同样用此方法,可以用TIdPOP3实现接收邮件。 注意,网上查到的相关说明大都是针对Indy9的,不适用于Indy10。

用Delphi编写邮件特快专递程序

 “邮件特快专递”这个词现在已经是被广大的网民所熟知了,它就好比是网络上的EMS,能够直接把邮件送到对方的邮箱中,邮件发送完毕后,对方就可以立刻收到。Foxmail在其最新的4.0版本中也特别新增了该功能,作为一名编程爱好者,你是不是也想知道如何编程实现该功能呢?那就随本文一起揭开“邮件特快专递”的神秘面纱:一、实现原理在Outlook Expres

xwchen的专栏 1462

indy10 ssl 发送邮件 dll,libeay32.dll,ssleay32.dll

DelphiXE7 + Indy 10.6.0.5169 测试通过 使用ssl时要用openssl动态库,libeay32.dll,ssleay32.dl

C++Builder6 用Id10 发加密邮件所需资源

有libeay32.lib,libeay32.dll,ssleay32.lib,ssleay32.dll,解决Error "Could not load SSL library。 c++Builder6.0 添加Id10控件所需文件在Indy目录。我已用此资源开发出了加密邮件的发送程序。资源的使用借鉴Delphi网上有。 提醒:我用的是windows-xp 32位,要先移除Id8(c++Builder6自带),再添加Id10,文件头部要有#pragma linker (lib,"ssleay32.lib") #pragma linker (lib,"libeay32.lib")

【免费下载】 轻松集成:Indy10 完全安装版,Delphi 开发者的福音

轻松集成:Indy10 完全安装版,Delphi 开发者的福音 【下载地址】Indy10完全安装版附带详细教程D72010 本仓库提供了一个名为 `indy10.2.3 full 完全安装版 D7~2010版本` 的资源文件下载。该资源文件包含了 Indy10.2.3 在 Delphi 7 至 Delphi 2010 版...

gitblog_09808的博客 1197

四类九种移位寄存器总结(循环(左、右、双向)移位寄存器、逻辑和算术移位寄存器、串并转换移位寄存器、线性反馈移位寄存器LFSR|verilog代码|Testbench|仿真结果)

移位寄存器有哪些分类呢? 按移位方向分类:①单向移位寄存器(包括左移、右移)②双向移位寄存器 按循环方式分类:①循环移位寄存器②非循环移位寄存器 按部位的不同分类:①逻辑移位寄存器②算术移位寄存器 按输入输出方式分类:①串入串出②串入并出③并入串出④并入并出

Loudrs的博客 1万+
上一篇: 使用Delphi收发GMail的邮件
下一篇: Stack的三种含义
chinajobs
博客等级 码龄12年 114粉丝 33原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值