Solved

Sending HTML email with Delphi

Posted on 2011-02-20
11
866 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
 

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

746 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now