Newsgroups : Borland : borland.public.delphi.internet.winsock : 2006 Oct : Re: How to add attachment to SMTP email?

www.cryer.info
Managed Newsgroup Archive

Re: How to add attachment to SMTP email?

Subject:Re: How to add attachment to SMTP email?
Posted by:"Remy Lebeau (TeamB)" (no.spam@no.spam.com)
Date:Tue, 17 Oct 2006 22:04:41

"Debby Beatty" <datapathdesign@earthlink.net> wrote in message
news:45359bd4$1@newsgroups.borland.com...

> How do I add an attachment?

Create an instance of a TIdAttachment class and add it to the
TIdMessage.MessageParts collection.

> I have this working, but don't know what to do next:

Try this:

    procedure TMyEMail.SendMail(vFromAddress, vFromName, vToAddress,
vSubject, vBodyText, vLogFile, vAttachment: string);
    begin
        IdMessage.Clear;

        IdMessage1.From.Address := vFromAddress;
        IdMessage1.From.Name := vFromName;
        IdMessage1.Recipients.EMailAddresses := vToAddress;
        IdMessage1.Subject := vSubject;

        if vAttachment <> '' then
        begin
            with TIdText.Create(IdMessage1.MessageParts, nil) do
            begin
                Body.Text := vBodyText;
                ContentType := 'text/plain';
            end;

            // if you are using Indy 10, then use TIdAttachmentFile instead
            with TIdAttachment.Create(IdMessage1.MessageParts, vAttachment)
do
            begin
                ContentType := GetMimeTypeFromFile(vAttachment);
            end;

            IdMessage1.ContentType := 'multipart/mixed';
        end else
        begin
            IdMessage1.Body.Add(vBodyText);
            IdMessage1.ContentType := 'text/plain';
        end;

        try
            SMTP.Connect;
            try
                SMTP.Send(IdMessage1);
            finally
                SMTP.Disconnect;
            end;
        except
            on E:Exception do
                Application.MessageBox(PChar(E.Message), 'ERROR: ' , MB_OK);
        end;

        if vLogFile <> '' then
            WriteToLog(vFromAddress, vToAddress, vSubject, vLogFile);
    end;


Gambit

Replies:

In response to:

www.cryer.info
Managed Newsgroup Archive