Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Sending HTML email with Delphi

Posted on 2011-02-20
11
872 Views
Last Modified: 2012-05-11
Hi all,

I have some html code in a TMemo and I'd like to send a HTML email using the HTML code from the TMemo or save it to html and then send it .. or something like that anyway.

Thanks

0
Comment
Question by:error77
  • 6
  • 3
  • 2
11 Comments
 
LVL 10

Expert Comment

by:jfromanski
ID: 34936660
So you need e-mail client in Delphi. Try ICS suite from http://www.overbyte.be/frame_index.html. TSMTPCli component there is able to send HTML e-mails.
0
 

Author Comment

by:error77
ID: 34936690
I'm trying with Indy and the email is been send BUT it's not a html message it's attached as a file ... Is this an extra setting that I need?
0
 
LVL 10

Expert Comment

by:jfromanski
ID: 34936697
That's why I'm recomendig ICS, it can sent html e-mails. BTW: very good software.
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:error77
ID: 34936711
I'll give it a shot
0
 

Author Comment

by:error77
ID: 34936722
OK, I'm getting installation issues saying I need some personality to load the project or something so ... I'm going to have to look for a solution without ics :o/
0
 
LVL 10

Expert Comment

by:jfromanski
ID: 34936724
See sample code
uses
  OverbyteIcsWndControl
, OverbyteIcsSmtpProt;

    SmtpClient: THtmlSmtpCli;
    procedure SmtpClientRequestDone(Sender: TObject; RqType: TSmtpRequest;
      ErrorCode: Word);

procedure TMail.SmtpClientRequestDone(Sender: TObject;
  RqType: TSmtpRequest; ErrorCode: Word);
begin
// all dirty job here
  if ErrorCode <> 0 then
  begin
// code for error
    Exit;
  end;
  case RqType of
    smtpConnect:  if SmtpClient.AuthType = smtpAuthNone then
                      SmtpClient.Helo
                   else
                      SmtpClient.Ehlo;
    smtpHelo:     SmtpClient.MailFrom;
    smtpEhlo:     SmtpClient.Auth;
    smtpAuth:     SmtpClient.MailFrom;
    smtpMailFrom: SmtpClient.RcptTo;
    smtpRcptTo:   SmtpClient.Data;
    smtpData:     SmtpClient.Quit;
    smtpQuit:     Terminate;
  end;
end;

procedure TMail.SendE_Mail;
begin
// fill required fields
  with SmtpClient do
  begin
    OnRequestDone  := SmtpClientRequestDone;
    HtmlCharSet    :='windows-1252';
    Host           := smtpHost;
    Port           := 'smtp';
    SignOn         := Karol;
    FromName       := Emil;
    HdrFrom        := FromName;
    HdrTo          := Rec;
    HdrCC          := '';
    HdrSubject     :='Subject';
    AuthType       := smtpA;
    Username       := smtpUser;
    Password       := smtpPasswd;
    HdrPriority    := smtpPriorityNone;
    ConfirmReceipt := false;
    ContentType    := smtpHtml;
    RcptName.Clear;
    RcptNameAdd(HdrTo, '', '');
    HtmlText := TMemo1.Lines;
// then
    Connect;
  end;
end;

Open in new window

0
 

Author Comment

by:error77
ID: 34936728
Is that code for ICS? I can't install it so I need a solution without ics :o/
0
 
LVL 24

Expert Comment

by:jimyX
ID: 34936842
I think this question was already asked before (using Indy):
http://www.experts-exchange.com/Programming/Editors_IDEs/Delphi/Q_22843803.html
0
 

Author Comment

by:error77
ID: 34936878
That sample doesn't send html it's just text :o/
0
 
LVL 24

Accepted Solution

by:
jimyX earned 500 total points
ID: 34936899
It's in the Author's question part:
function SendReport(ServerName, FromEmail, FriendlyName, ToEmail, Subject, Info: string): Boolean;
var
 htmpart, txtpart: TIdText;
 Email: TIdMessage;
 html: TStrings;
 IdSMTP: TIdSMTP;
begin
 Email := TIdMessage.Create(nil);
 html := TStringList.Create();
 IdSMTP := TIdSMTP.Create(nil);

 HTML.Add(Info);
 IdSMTP.Host := ServerName;

 email.ReplyTo.EMailAddresses := FromEmail;
 email.From.Text := FriendlyName;
 email.Recipients.EMailAddresses := ToEmail;
 email.Subject := Subject;
 email.ContentType := 'multipart/mixed';
 email.Body.Assign(html);
 
 txtpart := TIdText.Create(email.MessageParts, html);
 txtpart.ContentType := 'text/plain';
 txtpart.Body.Text := 'This is a html email';
 htmpart := TIdText.Create(email.MessageParts, html);
 htmpart.ContentType := 'text/html';
 { Now send it }
 try
  idSMTP.Connect();
  try
   idSMTP.Send(email);
  except on E: Exception do
    begin
     result := false;
    end;
  end;
 finally
  idSMTP.Disconnect();
 end;
 html.Free();
 htmpart.Free;
 txtpart.Free;
 Email.Free;
 IdSMTP.Free;
end;

Open in new window

0
 

Author Closing Comment

by:error77
ID: 34936902
Thanks :o)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question