Jump to content
Ian Branch

Image attachment in a html email??

Recommended Posts

Hi Team,

D10.4.2, Indy 10.

Trying to get an example from the internet working before I try to plug in my own parameters..

unit Unit2;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, IdMessage, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdExplicitTLSClientServerBase,
  IdMessageClient, IdSMTPBase, IdSMTP, Vcl.StdCtrls, IdEMailAddress, IdGlobal, IdAttachment, IdText, IdIOHandler, IdIOHandlerSocket, IdIOHandlerStack,
  IdSSL, IdSSLOpenSSL;

type
  TForm2 = class(TForm)
    Button1: TButton;
    IdSMTP1: TIdSMTP;
    IdMessage1: TIdMessage;
    IdSSLIOHandlerSocketOpenSSL1: TIdSSLIOHandlerSocketOpenSSL;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;
  lSMTP: TIdSMTP;
  lMessage: TIdMessage;
  IdSSLIOHandlerSocketOpenSSL1: TIdSSLIOHandlerSocketOpenSSL; // to allow SSL authenticate //

implementation

{$R *.dfm}

procedure TForm2.Button1Click(Sender: TObject);
var
  lTextPart: TIdText;
  lImagePart: TIdAttachment;
begin
  // ... here we need to set required properties for SMTP component. for moe visit How to send Email in Delphi?//
  lSMTP.Host := 'xxx';
  lSMTP.AuthType := satDefault;
  lSMTP.Username := 'xxxx';
  lSMTP.Password := 'xxxx';

  lSMTP.Port := 587;

  lMessage.From.Address := 'yourGmailaddress@gmail.com';
  lMessage.Subject := 'My test email with HTML body';
  lMessage.Recipients.Add.Address := 'receipent@gmail.com';
  lMessage.Body.Clear;

  // here we have initiated TIdtext object for plain text message in body//
  lTextPart := TIdText.Create(lMessage.MessageParts);
  lTextPart.Body.Text := 'This is a plain text message';
  lTextPart.ContentType := 'text/plain';

  // here we have initiated another TIdtext object for HTML text message in body//
  lTextPart := TIdText.Create(lMessage.MessageParts);
  lTextPart.ContentType := 'text/html';
  lTextPart.Body.Text := '<html><body><b>Hello World</b><a href="htmlbodyIMG0000.JPG" ></body></html>';

  // here we have initiated TIDAttachment object to add image file as attachment to mail as we have use this image in above HTML body message //
  lImagePart := TIdAttachment.Create(lMessage.MessageParts, 'htmlbodyIMG0000.JPG');
  lImagePart.ContentType := 'image/jpg';
  lImagePart.Headers.Add('Content-ID: <htmlbodyIMG0000.JPG>'); // if an image is used //

  // ... write your code to send a message for more visit How to send Email in Delphi? //
  lSMTP.Connect();
  try
    lSMTP.Send(lMessage);
    ShowMessage('Email sent');
  finally
    lSMTP.Disconnect();

  end;
  //
end;

I have the following showing..

image.thumb.png.7af7265140294d11b65e89ea4b208781.png

The 'note' for TIdAttachment is as follows..

image.thumb.png.32371df29a9294d1651b0baa3286672b.png

The 'note' for 'htmlbodyIMG0000.JPG' is as follows..

image.png.15d41e0c449deb28515ad8a361423fe7.png

 

The code is from 2016.  Has there been a change around TIdAttachment since?

If so, what should the line say/read?

 

Regards & TIA,

Ian

 

 

Share this post


Link to post
Quote

Has there been a change around TIdAttachment since?

Yes.  TIdAttachment is an abstract base class in Indy 10, it has no such constructor.  You are looking for TIdAttachmentFile instead.  I suspect the code you are trying to compile is actually meant for Indy 9, not 10.  In Indy 9, TIdAttachment was a concrete class that only supported files.  TIdAttachment was redesigned in Indy 10 to be a base class with new descendants introduced (TIdAttachmentFile, TIdAttachmentMemory, etc).

 

Also, the structure of your email is not setup correctly for HTML, with images.  The article Lajos linked to explains in detail how it should be setup.  And Indy has a TIdMessageBuilderHtml class to help with this task, see https://www.indyproject.org/2008/01/16/new-html-message-builder-class/

Edited by Remy Lebeau
  • Like 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×