Re: Message with attachment sometimes puts attachment in body of mail

Giganews Newsgroups
Subject: Re: Message with attachment sometimes puts attachment in body of mail
Posted by:  Remy Lebeau (TeamB) (no.spam@no.spam.com)
Date: Thu, 8 Dec 2005

"Ross McMillan" <nospam@gmail.com> wrote in message
news:8B460256DAE4E240nospam@gmail.com...

> I'm sending a plain text email with a PDF file as an attachment.
<snip>

You are not configuring the attachment info at all, and you are placing the
message text in the wrong place as well.  When you attach files, you need to
place your text into the MessageParts collection as well, not in the main
Body property.

Try this code instead:

    uses
        IdException;

    type
        EAttachmentFileNotFound = class(EIdException);

    EmailJob := TEmailJob.Create;
    try
        with EmailJob do
        begin
            ejMessage.Clear;
            ejMessage.From.Text := ejFromAddress;
            ejMessage.Recipients.EmailAddresses := ejToAddress;
            ejMessage.Subject := ejSubject;

            if (parAttachmentFileID1 <> '') or (parAttachmentFileID2 <> '')
then
            begin
                with TIdText.Create(ejMessage.MessageParts, nil) do
                begin
                    Body.Text := ejBody;
                    ContentType := 'text/plain';
                    // set other properties as needed...
                end;
                if parAttachmentFileID1 <> '' then
                begin

EAttachmentFileNotFound.IfFalse(FileExists(parAttachmentFileID1));
                    with TIdAttachmentFile.Create(Message.MessageParts,
parAttachmentFileID1) do
                    begin
                        ContentType :=
GetMimeTypeFromFile(parAttachmentFileID1);
                        // set other properties as needed...
                    end;
                end;
                if parAttachmentFileID2 <> '' then
                begin

EAttachmentFileNotFound.IfFalse(FileExists(parAttachmentFileID2));
                    with TIdAttachmentFile.Create(Message.MessageParts,
parAttachmentFileID2) do
                    begin
                        ContentType :=
GetMimeTypeFromFile(parAttachmentFileID2);
                        // set other properties as needed...
                    end;
                end;
                ejMessage.ContentType := 'multipart/mixed';
            end else
            begin
                ejMessage.Body.Text := ejBody;
                ejMessage.ContentType := 'text/plain';
            end;

            EmailPool.Add(EmailJob);
            ejResult := pterOK;
        end;
    except
        on E: Exception do
        begin
            EmailJob.Free;
            if E is EAttachmentFileNotFound then
                ejResult := pterAttachmentFileNotFound
            else
                // set ejResult to something else...
        end;
    end;

Gambit

Replies

In response to

Message with attachment sometimes puts attachment in body of mail posted by Ross McMillan on Fri, 9 Dec 2005