Solved

Sending HTML email with Delphi

Posted on 2011-02-20
11
878 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
[X]
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
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

690 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