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

时间:2009-11-01 22:09:05

标签: html delphi email

如何使用带有html内容的Delphi SFTP组件发送电子邮件?

2 个答案:

答案 0 :(得分:7)

最近不得不使用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;
  

注意:任何已发布到公共领域的代码。无需归属。

答案 1 :(得分:3)

我很确定有一个样本Indy项目向您展示如何做到这一点。在Indy演示中寻找'MailClient'项目。您还可以查看CodeGear新闻组档案,例如:

http://codenewsfast.com/isapi/isapi.dll/article?id=4507106B&article=6979409

Remy Lebeau的短文也在这里: http://www.indyproject.org/Sockets/Blogs/RLebeau/2005_08_17_A.en.aspx

顺便说一下,所有这些都可以通过快速的互联网搜索轻松找到。如果您在Delphi工作并且未使用CodeNewsFast.com知识库,那么您将浪费时间和精力。