Re: mail with attachment

Giganews Newsgroups
Subject: Re: mail with attachment
Posted by:  Remy Lebeau \(Indy Team\) (no.spam@no.spam.com)
Date: Tue, 10 Feb 2009

"Mieke" <jaja…@planet.nl> wrote in message
news:E26492A2AD75E340jaja…@planet.nl...

> I use indy 10.1.5I (delphi2007) and can't get it working.

That is because you are not setting up the TIdMessage the correct way for
Indy 10.  It should be more like the following:

{code}
var
    TextPart: TIdText;
    Attachment: TIdAttachmentFile;
    sDocument: string;
begin
    sDocument := 'c:\mailtest.doc';

    MyMessage.Clear;
    MyMessage.IsEncoded  := True ;
    MyMessage.CharSet    := 'iso-8859-1';
    MyMessage.Encoding    := meMIME;
    MyMessage.ContentType := 'multipart/mixed';
    MyMessage.Subject := 'testmail';

    MyMessage.From.Text := ..afzender
    MyMessage.Recipients.Add.Address := 'te…@test.nl';

    TextPart := TIdText.Create(MyMessage.MessageParts, nil);
    TextPart.ContentType := 'multipart/alternative';
    TextPart.ParentPart := -1;

    TextPart := TIdText.Create(MyMessage.MessageParts, nil);
    TextPart.ContentType := 'text/plain';
    TextPart.Body.Text := 'BodyPlain';
    TextPart.ParentPart := 0;

    TextPart := TIdText.Create(MyMessage.MessageParts, nil);
    TextPart.ContentType := 'text/html';
    TextPart.Body.Text := 'BodyHTML';
    TextPart.ParentPart := 0;

    Attachment := TIdAttachmentFile.Create(MyMessage.MessageParts,
sDocument);
    Attachment.ContentDisposition := 'attachment';
    Attachment.ContentType := GetMimeTypeFromFile(sDocument);
    Attachment.FileName := ExtractFileName(sDocument);
    Attachment.ParentPart := -1;

    smtp.Host                    := ..host
    smtp.Port                    :=  ..poost
    smtp.Connect;
    try
        smtp.Send(MyMessage);
    finally
        smtp.Disconnect;
    end;
end;
{code}

With that said, I suggest you upgrade to a newer newer Indy 10 snapshot so
that you can use the new TIdMessageBuilderHtml class to help you, ie:

{code}
var
    MsgBuilder: TIdMessageBuilderHtml;
    sDocument: string;
begin
    sDocument := 'c:\mailtest.doc';

    MyMessage.Clear;
    MyMessage.CharSet    := 'iso-8859-1';
    MyMessage.From.Text := ..afzender
    MyMessage.Recipients.Add.Address := 'te…@test.nl';

    MsgBuilder := TIdMessageBuilderHtml.Create;
    try
        MsgBuilder.PlainText := 'BodyPlain';
        MsgBuilder.Html := 'BodyHTML';
        MsgBuilder.Attachments.Add(sDocument);
        MsgBuilder.Subject := 'testmail';
        MsgBuilder.FillMessage(MyMessage);
    finally
        MsgBuilder.Free;
    end;

    smtp.Host                    := ..host
    smtp.Port                    :=  ..poost
    smtp.Connect;
    try
        smtp.Send(MyMessage);
    finally
        smtp.Disconnect;
    end;
end;
{code}

--
Remy Lebeau (TeamB)

Replies

In response to

mail with attachment posted by Mieke on Tue, 10 Feb 2009