Solved

Sending HTML email with Delphi

Posted on 2011-02-20
11
870 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Printing problem 2 95
JAudiorecorder record freezing the app 29 67
Twebbrowser in Fire monkey android scrolling to bottom 1 30
Delphi problems to abort a printjob 9 20
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…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

809 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