4

I'm trying to send a email with indy 9:

  • Body as RTF, formatted from a TRichEdit
  • A single file attached

The code:

 Message := TIdMessage.Create()
 Message.Recipients.EMailAddresses := 'someone@domain.dotcom';

 Message.ContentType := 'multipart/alternative';

 with TIdText.Create(Message.MessageParts) do
   ContentType := 'text/plain';

 with TIdText.Create(Message.MessageParts) do
 begin
   ContentType := 'text/richtext';
   Body.LoadFromFile('c:\bodymsg.rtf');
 end;

 TIdAttachment.Create(Message.MessageParts, 'c:\myattachment.zip');

 // send...

The result: body comes empty (using web gmail and outlook 2010 as clients).

I'm already tried other content types without success:

  • text/rtf
  • text/enriched

NOTE: I'll not upgrade to Indy 10.

Beto Neto
  • 3,962
  • 7
  • 47
  • 81
  • 3
    Any sensible reason why indy10 is out of the picture?? Indy9 is really old and has some smtp related bugs. – whosrdaddy Mar 13 '13 at 21:30
  • Remy Lebeau's answer to the linked duplicate (including his comments) seems to answer this question. (You have to read the comments, though, which is probably why his answer wasn't accepted there.) The key is to use the `TRichEdit.SaveToStream` and `Body.LoadFromStream` instead of `Body.LoadFromFile`, as `LoadFromFile` assumes plain text. – Ken White Mar 13 '13 at 22:10
  • 1
    @KenWhite: also keep in mind that the linked duplicate was for Indy 10. This situation for Indy 9 requires a little bit of a different approach because of Indy 9's lack of deep MIME support. As for your comment about `LoadFromStream()` vs `LoadFromFile()`, `LoadFromFile()` calls `LoadFromStream()` internally, and `TIdText.Body` is just a `TStrings`, so it will load the file's RTF coding as-is. – Remy Lebeau Mar 13 '13 at 22:14
  • @Remy: Didn't catch the Indy 10 in the dupe. I knew you'd be along to answer this question, anyway. ;-) – Ken White Mar 13 '13 at 22:16
  • @whosrdaddy: yes, code adaptation, high impact! – Beto Neto Mar 14 '13 at 17:08
  • Upgrading to Indy10 is not that hard, you know... – whosrdaddy Mar 14 '13 at 18:50

1 Answers1

5

You are setting the TIdMessage.ContentType to the wrong value when the TIdAttachment is present. It needs to be set to 'multipart/mixed' instead because you are mixing the 'multipart/alternative' and 'application/x-zip-compressed' parts together at the same top-level MIME nesting level, whereas the 'text/...' parts are children of the 'multipart/alternative' part instead.

Have a look at the following blog article I wrote on the Indy website:

HTML Messages

The email structure you are trying to create is covered by the "Plain-text and HTML and attachments: Non-related attachments only" section. You would just replace HTML with RTF, and ignore the TIdText object for the 'multipart/alternative' part because TIdMessage in Indy 9 will create that internally for you (it is explicitly needed in Indy 10 because of its deeper MIME support than Indy 9 has).

Try this:

Message := TIdMessage.Create()
Message.Recipients.EMailAddresses := 'someone@domain.dotcom';

Message.ContentType := 'multipart/mixed';

with TIdText.Create(Message.MessageParts) do
begin
  ContentType := 'text/plain';
  Body.Text := 'You need an RTF reader to view this message';
end;

with TIdText.Create(Message.MessageParts) do
begin
  ContentType := 'text/richtext';
  Body.LoadFromFile('c:\bodymsg.rtf');
end;

TIdAttachment.Create(Message.MessageParts, 'c:\myattachment.zip');

// send...
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770