Generate HTML e-mail with embedded images in Delphi

安稳与你 提交于 2019-11-28 12:28:34

Read the following articles on Indy's website, they explain the proper way to populate a TIdMesaage for HTML:

HTML Messages

New HTML Message Builder class

These days I use Clever Component's email client, though not free.

The TurboPower Internet (OpenSource) controls worked great for me in the past.

function data64(const filename:string): ansistring;
// uses Classes, IdGlobalProtocols, EncdDecd;
const
  crlf = #13#10;
begin
  result := '';
  with TIdMimeTable.Create do
  try
    result := 'data:'
      + GetFileMIMEType(filename) + ';';
  finally
    Free;
  end;
  with TMemoryStream.Create do
  try
    LoadFromFile(filename);
    result := result + 'base64,' + crlf
      + EncodeBase64(Memory,Size);
  finally
    Free;
  end;
end;

We tried this years ago with Indy and embedded cid: images like this: https://forums.codegear.com/thread.jspa?threadID=17473

We never got it stable, each time there was another mail reader that barfed (if we got it working in Outlook, then Thunderbird didn't accept it, or Outlook Express, or, etc, etc).

In the end we did it with .NET using AspNetEmail and it worked like a charm.

--jeroen

It all depends on how many different remote mail clients you have to be able to support. I believe that the well-known Delphi libraries which support SMTP/Mime will do a reasonable job, but they may leave you with support problems when one of your mail users finds that their recipient cannot see a properly formatted e-mail.

I recommend visiting Jacob Palme's site which will give you an idea of some of the problems that you may encounter. It is a little out-of-date, perhaps, but in summary, however you build and send your complex MIME e-mail, you will encounter one or more mail clients which cannot handle the syntax properly. The site also has links to some useful examples of constructions which you can examine and test.

I do not mean to imply that you have to roll your own logic as we did: a good packaged solution will probably be successful in the great majority of cases. We wrote our own Delphi code to handle this some years ago, so I will leave it for others to give you up-to-date information about what is on the shelf now.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!