Solved

Sending HTML email with Delphi

Posted on 2011-02-20
11
869 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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 is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

920 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

15 Experts available now in Live!

Get 1:1 Help Now