Send Email with HTML body format in Delphi
Sometime we need to send Email with HTML body format. So here I have explained two approach to send HTML Email by using Delphi.
1. By using TIDText object
With Indy components we can use same TIDSMTP and TIDMessage components to send a Email with normal Text body or HTML body in Delphi. But we also need to initiate a TIDText object so that we can set its ContentType = text/html to allow HTML body format. And if we need an Image in HTML body then we also need to attach that image to mail body. So for that we need to create a TIdAttachment object also.
Here are the code details how to use TIDText.
And for this example I assume that we have already put TIDSMTP and TIDMessage component on form and also set required properties.
For more information how to use TIDSMTP and TIDMessage visit my following blog.
lSMTP : TIdSMTP;
lMessage: TIdMessage;
var
lTextPart: TIdText;
lImagePart: TIdAttachment;
begin
// ... here we need to set required properties for SMTP component. for moe visit How to send Email in Delphi?//
lSMTP.Host := 'xxx';
lSMTP.AuthType := satDefault;
lSMTP.Username := 'xxxx';
lSMTP.Password := 'xxxx';
lSMTP.Port := 587;
lMessage.From.Address := 'yourGmailaddress@gmail.com';
lMessage.Subject := 'My test email with HTML body';
lMessage.Recipients.Add.Address := 'receipent@gmail.com';
lMessage.Body.Clear;
// here we have initiated TIdtext object for plain text message in body//
lTextPart := TIdText.Create(lMessage.MessageParts);
lTextPart.Body.Text := 'This is a plain text message';
lTextPart.ContentType := 'text/plain';
// here we have initiated another TIdtext object for HTML text message in body//
lTextPart := TIdText.Create(lMessage.MessageParts);
lTextPart.ContentType := 'text/html';
lTextPart.Body.Text := '<html><body><b>Hello World</b><a href="htmlbodyIMG0000.JPG" ></body></html>';
// here we have initiated TIDAttachment object to add image file as attachment to mail as we have use this image in above HTML body message //
lImagePart := TIdAttachment.Create(lMessage.MessageParts, 'htmlbodyIMG0000.JPG');
lImagePart.ContentType := 'image/jpg';
lImagePart.Headers.Add('Content-ID: <htmlbodyIMG0000.JPG>'); // if an image is used //
// ... write your code to send a message for more visit How to send Email in Delphi? //
lSMTP.Connect();
try
lSMTP.Send(email);
ShowMessage('Email sent');
finally
lSMTP.Disconnect();
end;
end;
2. By using TIdMessageBuilderHtml object
TIdMessageBuilderHtml class to help you prepare the TIdMessage properly. So we need to create an object of this class and then we can use HTML property to add messages.
Here are the code detail how to use TIdMessageBuilderHtml .
And for this example also I assume that we have already put TIDSMTP and TIDMessage component on form and also set required properties.
How to send Email in Delphi?
IdSMTP : TIdSMTP;
ldMessage: TIdMessage;procedure TFormEmail.HTMLEmailSendClick(Sender: TObject);
var
Htmlbuilder: TIdMessageBuilderHtml;
imgfilename: string;
begin
IdSMTP.Host := 'xxx';
IdSMTP.AuthType := satDefault;
IdSMTP.Username := 'xxxx';
IdSMTP.Password := 'xxxx';
idSMTP.Port := 587;
imgfilename := 'C:\img1.jpg'; // if images in HTML body//
try
Htmlbuilder := TIdMessageBuilderHtml.Create;
try
Htmlbuilder.Html.Add('<html>');
Htmlbuilder.Html.Add('<head>');
Htmlbuilder.Html.Add('</head>');
Htmlbuilder.Html.Add('<body>Hello World</body>');
Htmlbuilder.Html.Add('</html>');
Htmlbuilder.HtmlCharSet := 'utf-8';
Htmlbuilder.HtmlFiles.Add(imgfilename, 'us.jpg');
// if you want to add unrelated HTML attachment like PDF file as mail attachment //
Htmlbuilder.Attachments.Add('c:\filename.pdf');
ldMessage:= Htmlbuilder.NewMessage(nil);
ldMessage.From.Address := yourGmailaddress@gmail.com; // please change to your gmail address //;
ldMessage.Subject := 'Test Email Subject';
ldMessage.Body := 'Test Email with HTML body';
ldMessage.Priority := mpHigh;
IdSMTP.Connect();
try
IdSMTP.Send(email);
ShowMessage('Email sent');
finally
IdSMTP.Disconnect();
end;
finally
Htmlbuilder.Free();
end;
except
on E: Exception do
ShowMessage('Failed: ' + E.Message);
end;
end;
Comments
Post a Comment