Delphi用Indy10实现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;

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则必须设置密码和用户名。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值