Trying to send email with attached related and attached unrelated files

Giganews Newsgroups
Subject: Trying to send email with attached related and attached unrelated files
Posted by:  Connie McBride (ccoki…@iwon.com)
Date: Tue, 9 Aug 2005

I can get the HTML to work correctly with the embedded graphics.
I can't seem to get the unrelated attachments to work.  I am obviously
missing something, just don't know what, yet.
Following is my code.
I have tried setting the parent part of the attachments to 0, 1, etc.  Every
time I set it, I get a list index out of bounds(0).
If I set it to -1, then the html no longer works.

procedure TdlgEmail.EmailMessage;//creates the email
var
  fName : string;
  sList : tStringList;
  x : integer;
  SearchRec : tSearchRec;
  op : string;
  pPart : integer;
begin
//initialize  addresses, SMTP settings, etc

  if not PDFFormat then
      fname := UserFPath + 'rpt001.htm'
  else
      fName := UserFPath + 'rpt001.pdf';

  sList := tStringList.Create;
  if lbAttach.Items.Count > 0 then //contains a list of optional
attachments
      emMessage.ContentType := 'multipart/alternative'
  else
      emMessage.ContentType := 'multipart/related';

  if (PDFFormat) and
      (FileExists(fName)) then
  begin
      if reBody.Lines.Count > 0 then
      begin
        slist.Insert(0, reBody.Text);
        with TIdText.Create(emMessage.MessageParts, sList) do
          ContentType:='text/html';
      end;
      with TIdText.Create(emMessage.MessageParts, sList) do
          ContentType:='multipart/related';
      CreateAttachment(fName)
  end
  else
  begin
      sList.Clear;
      sList.LoadFromFile(fName);
      if reBody.Lines.Count > 0 then
        slist.Insert(0, reBody.Text);
      with TIdText.Create(emMessage.MessageParts, sList) do
        ContentType:='text/html';

      with TIdText.Create(emMessage.MessageParts, nil) do
        ContentType:='multipart/related';
      sList.Clear;
      if FindFirst(UserFPath + 'rpt*.jpg', faAnyFile, SearchRec) = 0 then
      repeat
        sList.Add(UserFPath + SearchRec.Name)
      until FindNext(SearchRec) <> 0;
      FindClose(SearchRec);
      for x := 0 to sList.Count - 1 do
      begin
        if FileExists(sList[x]) then
            CreateAttachment(sList[x]);
      end;
  end;

//This is where the problem is.

  if lbAttach.Items.Count > 0 then
  begin
      with TIdText.Create(emMessage.MessageParts, nil) do
        ContentType:='text/plain';

      with TIdText.Create(emMessage.MessageParts, nil) do
        ContentType:='multipart/mixed';
      for x := 0 to lbAttach.Items.Count - 1 do
      begin
        if FileExists(lbAttach.Items[x]) then
            CreateAttachment(lbAttach.Items[x], 0);
      end;
  end;
  doMail;
//clean up stuff
end;

procedure tdlgEMail.CreateAttachment(fName : string; pPart : integer
= -1);//creates the attachments
var
  aStr : string;
begin
  with TIdAttachmentFile.Create(emMessage.MessageParts, fName) do
  begin
    ContentID := fName;
    ContentType:=GetMIMETypeFromFile(fName);
    aStr := ContentType;
    Filename:=ExtractFilename(fName);
    ParentPart := pPart;
  end;
end;

function TdlgEmail.DoMail(disconnect : boolean = true) : boolean;//send the
email
begin
  result := false;
  try
      try
        if not SMTP.Connected then
            SMTP.Connect;
        if SMTP.Connected then
        begin
            SMTP.Send(emMessage);------> get an index out of bounds(0)
            result := true;
        end
      except
        on E:Exception do
        begin
            MessageDlg('There has been an error sending mail.'+ #13 +
                      'Please establish an internet connection and verify
all'+ #13 +
                      'email addresses before trying again.' + #13 + #13 +
                      E.Message, mtError,[mbOK,mbCancel],0);
        end;
      end;
  finally
      Screen.Cursor := crDefault;
      if (SMTP.Connected) and
        (Disconnect) then
        SMTP.Disconnect;
  end;
end;

Replies