html - 如何使用 IdSMTP (Delphi) 发送包含 html 内容的电子邮件?

标签 html delphi email

如何使用带有 html 内容的 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
        '<!DOCTYPE html><html><body>Hello, world!</body></html>', //html body
        True,                                      //the body is HTML (as opposed to plaintext)
        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', '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
            '<!DOCTYPE html><html><body>Hello, world!</body></html>', //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 := '<Name of recipient>';
                    Address := emailAddress;
                end;
            end;
        end;

        for s in CCAddresses.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;

Note: Any code released into public domain. No attribution required.

关于html - 如何使用 IdSMTP (Delphi) 发送包含 html 内容的电子邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1658535/

相关文章:

javascript - ng-app 指令的作用是什么?

html - 离开悬停状态时应用动画

javascript - 如何在第一个 contenteditable div 超过高度后使用 jquery ajax 在 php 中添加新的 contenteditable div?

html - <h1>没有从其容器继承font-weight

python 3 : Move email to trash by uid (imaplib)

android - Gmail Android 应用程序(Android 5-6)中我的表格之间的细线边界

android - Android 和 iOS 上的 ipv6 和 TidHTTP 问题

Delphi 网络浏览器和 CSS3

delphi - 当 case 变量未命名时,如何判断变体记录中哪种情况有效,以及如何创建此类记录的值?

php - 警告 : Bad Message Return Path (PHP)