How can I send an email by using Delphi's IdSMTP-component with html-contetent?
Having to recently use the Indy IdSmtp
component, it was a shame that there was no good answer to this question. I re-wrote our helper function to send e-mail using Indy (both HTML and plaintext)
Sample Usage
SendHtmlEmailIndy(
'smtp.stackoverflow.com', //the SMTP server address
'Spammy McSpamerson', '[email protected]', //From name, from e-mail address
'[email protected], [email protected]', //To addresses - comma separated
'[email protected]', //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
The tricky part is that Indy makes it difficult to send an HTML e-mail. They eventually provided an TIdMessageBuilderHtml
class to handle most of the grunt work; but it's nowhere near as pleasent as the SmtpClient
class. In the end you'll take a dependency on three units.
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', '[email protected]', //From name, from e-mail address
'[email protected], [email protected]', //To addresses - comma separated
'[email protected]', //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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With