HTML   发布时间:2022-04-14  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何使用IdSMTP(Delphi)发送带有html-contetent的电子邮件?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何使用带有html-contetent的Delphi的IdSMTP组件发送电子邮件?

最佳答案
不得不最近使用Indy IdSmtp组件,遗憾的是这个问题没有很好的答案.我重新编写了我们的帮助函数,使用Indy(HTML和纯文本)发送电子邮件

样本用法

SendHtmlEmailIndy(
        'smtp.stackoverflow.com',//the SMTP server address
        'SpAMMy McSpamerson','spams@example.com',//From name,from e-mail address
        'joe@foo.net,jane@bar.net',//To addresses - comma separated
        'john@doe.net',//CC addresses - comma separated
        '',//BCC addresses - comma separated
        'Here is your sample spam e-mail',//Subject
        'Hello,world!ntext)
        nil); //attachments

棘手的部分是Indy使发送HTML电子邮件变得困难.他们最终提供了一个TIdmessageBuilderHtml类来处理大部分繁琐的工作;但它远没有像SmtpClient类那样令人愉快.最后,你会依赖三个单位.

代码

procedure SendEmailIndy(
        const SMTPServer: String;
        const Fromname,FromAddress: String;
        const toaddresses: String; //comma "," separated list of e-mail addresses
        const cCAddresses: String; //comma "," separated list of e-mail addresses
        const BCCAddresses: String; //comma "," separated list of e-mail addresses
        const Subject: String;
        const EmailBody: String;
        const IsBodyHtml: Boolean; //verses Plain Text
        const Attachments: TStrings);
var
    smtp: TIdSMTP; // IdSmtp.pas
    msg: Tidmessage; // Idmessage.pas
    builder: TIdCustommessageBuilder; //IdmessageBuilder.pas
    s: String;
    emailAddress: String;
begin
{
    Sample usage:

    SendEmailIndy(
            'smtp.stackoverflow.com',//the SMTP server address
            'SpAMMy McSpamerson',from e-mail address
            'joe@foo.net,//To addresses - comma separated
            'john@doe.net',//CC addresses - comma separated
            '',//BCC addresses - comma separated
            'Here is your sample spam e-mail',//Subject
            'Hello,//html body
            True,//the body is HTML (as opposed to plaintext)
            nil); //attachments
}
    msg := Tidmessage.Create(nil);
    try
        if IsBodyHtml then
        begin
            builder := TIdmessageBuilderHtml.Create;
            TIdmessageBuilderHtml(builder).Html.Text := EmailBody
        end
        else
        begin
            builder := TIdmessageBuilderPlain.Create;
        end;
        try
            if Attachments <> nil then
            begin
                for s in Attachments do
                    builder.Attachments.Add(s);
            end;

            builder.Fillmessage(msg);
        finally
            builder.Free;
        end;

        msg.From.Name := Fromname;
        msg.From.Address := FromAddress;
        msg.Subject := Subject;

        //If the message is plaintext then we must fill the body outside of the Plaintext email builder.
        //(the PlaintextBuilder is unable to build plaintext e-mail)
        if not IsBodyHtml then
            msg.body.Text := EmailBody;

        for s in toaddresses.Split([',']) do
        begin
            emailAddress := Trim(s);
            if emailAddress <> '' then
            begin
                with msg.recipients.Add do
                begin
                    //Name := 's.Split([',']) do
        begin
            emailAddress := Trim(s);
            if emailAddress <> '' then
                msg.CCList.Add.Address := emailAddress;
        end;

        for s in BCCAddresses.Split([',']) do
        begin
            emailAddress := Trim(s);
            if emailAddress <> '' then
                msg.bccList.Add.Address := emailAddress;
        end;

        smtp := TIdSMTP.Create(nil);
        try
            smtp.Host := SMTPServer; // IP Address of SMTP server
            smtp.port := 25; //The default already is port 25 (the SMTP port)

            //Indy (and C# SmtpClient class) already defaults to the computer name
            //smtp.HeloName :=
            smtp.Connect;
            try
                smtp.Send(msg)
            finally
                smtp.Disconnect;
            end;
        finally
            smtp.Free;
        end;
    finally
        msg.Free;
    end;
end;

大佬总结

以上是大佬教程为你收集整理的如何使用IdSMTP(Delphi)发送带有html-contetent的电子邮件?全部内容,希望文章能够帮你解决如何使用IdSMTP(Delphi)发送带有html-contetent的电子邮件?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。