Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
500 Users Online NOW
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi



Loremo - the 1.5 liter car coming in 2009




Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (3)


How can I send HTML messages with attachments (using Indy)Go to Nigel Cross's websiteFormat this article printer-friendly!Bookmark function is only available for registered users!
Using IdMessage to send HTML formatted emails with attachments
Product:
Delphi all versions
Category:
Internet / Web
Skill Level:
Scoring:
Last Update:
03/28/2002
Search Keys:
delphi delphi3000 article borland vcl code-snippet Indy HTML attachment file smtp e-mail mail email attach
Times Scored:
19
Visits:
12303
Uploader: Nigel Cross
Company: Xequte Software
Reference: N/A
 
Question/Problem/Abstract:
With the Indy IdMessage component you can easily send an HTML formatted e-mails without attachments, or plain text e-mails with attachments, but how can you send HTML message WITH attachments...
Answer:



With the IdMessage component:

- You can send a plain text e-mail and add an attachment using:

TIdAttachment.Create(IdMessage.MessageParts, 'c:\attach.bmp');

- You can send an HTML formatted message simply by setting the IdMessage.body to the HTML string and the IdMessage.ContentType to 'text/html'


However if you try to combine these, i.e. send an HTML formatted message with an attachment you will find that the message is delivered with the HTML code shown as plain text.  

This is because you have to send the HTML as a text part when sending attachments.  So you need to create two text parts (one plain text and one HTML) and then add the attachments.


Here is some example code:


Notes:
  IdMsgSend is a TIdMessage component
  SMTP is a TIdSMTP component
  Delphi 5, Indy version is v9.03 (Indy version which ships with D6 is 8.0)

var
  idtTextPart:TIdText;

.
.
.

IdMsgSend. Clear;

IdMsgSend.ContentType := 'Multipart/Alternative';


// add a plain text message part

idtTextPart:=TIdText.Create(IdMsgSend.MessageParts,nil);
idtTextPart.ContentType:='text/plain';
idtTextPart.Body.Add('This is the plain part of the message.');


// add the HTML message part

idtTextPart:= TIdText.Create(IdMsgSend.MessageParts,nil);
idtTextPart.ContentType := 'text/html';
idtTextPart.Body.add('');
idtTextPart.Body.add('Testing...
');
idtTextPart.Body.add('Testing...
');
idtTextPart.Body.add('
Testing...
');
idtTextPart.Body.add('');


IdMsgSend.From.Address := 'me@mycompany.com';
IdMsgSend.From.Name := 'Me';

IdMsgSend.Sender.Address := 'me@mycompany.com';
IdMsgSend.Sender.Name := 'Me';


// add the recipients

IdMsgSend.Recipients.clear;
with IdMsgSend.Recipients.Add  do Address := 'address1@somecompany.com';
with IdMsgSend.Recipients.Add  do Address := 'address2@somecompany.com';


IdMsgSend.Subject := 'Some Subject';

// add an attachment
TIdAttachment.Create(IdMsgSend.MessageParts, 'c:\attach.bmp');


{SMTP Setup}
SMTP.Host := 'smtp.isp.com';
SMTP.Port := 25;

try
try
  {now we send the message}
  SMTP.Connect;

  SMTP.Send(IdMsgSend);

except
  on e: Exception do
    ShowMessage(e.message);
end;

finally
  SMTP.Disconnect;
end;

showmessage('Message Sent');





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
Sending HTML
    Daniel Wischnewski (Mar 28 2002 8:58AM)

Noted correctly, that attachments will not work with HTML-only mails, however it is a rather simple reason in the RFC governing the email message format.

Therefore you should always include a text-only part in the HTML emails, since there are enough mail-clients that do not (properly) show HTML Messages.

I like the way your article is showing the multi-message part.

P.S. File-attachments are send as a special message part (application/binary or more detailed) anyway.

Regards,
Daniel
Respond

D'oh, the HTML strings did not appear correctly
    Nigel Cross (Mar 28 2002 4:57AM)

The HTML strings do not appear correctly above (are actually displayed as HTML).  It should read:

idtTextPart.Body.add('<HTML><BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#3333FF" VLINK="#999999" ALINK="#FF0000">');
idtTextPart.Body.add('<FONT COLOR="#000000">Testing...</FONT><BR>');
idtTextPart.Body.add('<FONT COLOR="#000000">Testing...</FONT><BR>');
idtTextPart.Body.add('<BR><B><FONT COLOR="#000000"><FONT SIZE=+1>Testing...</FONT></FONT></B><BR>');
idtTextPart.Body.add('</BODY></HTML>');
Respond

D'oh, the HTML strings did not appear correctly
    Nigel Cross (Mar 28 2002 4:56AM)

The HTML strings do not appear correctly above (are actually displayed as HTML).  It should read:

idtTextPart.Body.add('');
idtTextPart.Body.add('Testing...
');
idtTextPart.Body.add('Testing...
');
idtTextPart.Body.add('
Testing...
');
idtTextPart.Body.add('');
Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
M. Kleiner
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)