Board index » delphi » Best way to send email w/attachments?

Best way to send email w/attachments?

It seems, from searching the posts here that most people like the Indy
components when working with email. I did download and install them and looked
at the demos. Seem very nice, but extreme overkill for what I need. I do not
need ftp, pop3, telnet, etc, etc. All I need is a method/componant to send an
email with an attachment or two (text, pdf, or excel). That's it. I do not
want to use MAPI or anything to do with LookOut, oops sorry, OutLook, or any
other program. I would like it to be self contained and not rely on external
programs to be installed/setup properly.

Is Indy still the best way to go?

- Gary

 

Re:Best way to send email w/attachments?


Gary,

Indy will do this - just use the IdSMTP component with a IdMessage.

There have been plenty of posts here asking how to send emails with
attachments, so I would search for those either using your newsreader or
google groups.

Regards,

Mike

Quote
"Gary MacKay" <g...@edisoninfo.com> wrote in message

news:3e81b904$1@newsgroups.borland.com...
Quote
> It seems, from searching the posts here that most people like the Indy
> components when working with email. I did download and install them and
looked
> at the demos. Seem very nice, but extreme overkill for what I need. I do
not
> need ftp, pop3, telnet, etc, etc. All I need is a method/componant to send
an
> email with an attachment or two (text, pdf, or excel). That's it. I do not
> want to use MAPI or anything to do with LookOut, oops sorry, OutLook, or
any
> other program. I would like it to be self contained and not rely on
external
> programs to be installed/setup properly.

> Is Indy still the best way to go?

> - Gary

Re:Best way to send email w/attachments?


In article <3e81b90...@newsgroups.borland.com>, g...@edisoninfo.com
says...

Quote
> It seems, from searching the posts here that most people like the Indy
> components when working with email. I did download and install them and looked
> at the demos. Seem very nice, but extreme overkill for what I need.

Delphi will only link in the units you need. So your app won't have
HTTP, FTP, NNTP etc. support unless you're actually using it.

M.

Re:Best way to send email w/attachments?


Gary:

I've attached some code that uses Indy's TidSMTP to mail html and text with
attachments.

Regards,
Peter

{ **************************************************************** }
procedure TfrmMain.btnSendClick(Sender: TObject);
var
  i: Integer;
  MsgText, MsgHtml: TIdText;
  sFile: String;
  anAttachment: TIdAttachment;
  sErr: String;
  iSent: Integer;
  iBlankParm: Integer;
  F: TextFile;
  sLine: String;

   { **************************************************************** }
  procedure initAttach;
  var
    i: Integer;
  begin
    IdMessage.MessageParts.Clear;
    MsgText := TIdText.Create(IdMessage.MessageParts, nil);
    MsgText.ContentType := 'text/plain';
    if edtText.Text = '' then MsgText.Body.Text := 'HTML Viewer required to
view this email.'
    else MsgText.Body.LoadFromFile(edtText.Text);

    if FAttach.Count > 0 then
    begin
      for i := 0 to FAttach.Count -1 do
      begin
        anAttachment := TIdAttachment.Create(IdMessage.MessageParts,
FAttach[i]);
        anAttachment.ExtraHeaders.Values['Content-ID'] := FAttachNames[i];
        if m{*word*198}bug.Checked then Log('DEBUG: Created attachment ['+
FAttachNames[i] + '] for '+ FAttach[i]);
      end;
    end;
  end;

  { **************************************************************** }
  procedure doHTML;
  begin
    MsgHtml := TIdText.Create(IdMessage.MessageParts, nil);
    MsgHtml.ContentType := 'text/html';
    sFile := FHomeDir+'\msg.html';
    PrepHtml(edtHTML.Text, sFile, False);
    MsgHtml.Body.LoadFromFile(sFile);
  end;

  { **************************************************************** }
  function SendOne: Boolean;
  var
    sEmail: String;
  begin
    if IdMessage.Recipients.EMailAddresses = ''
    then sEmail := IdMessage.Recipients.Items[0].Name +
IdMessage.Recipients.Items[0].Address
    else sEmail := IdMessage.Recipients.EMailAddresses;
    try
      SMTP.Send(IdMessage);
      Log('send to '+sEmail+' OK');
      sb.Panels[0].Text := 'sent to '+ sEmail;
      sb.Refresh;
      Result := True;
    except
      on E: Exception do
      begin
        sErr := SocketErrMsg(E.Message);
        Log('send to '+sEmail);
        Log('ERROR: '+sErr);
        sb.Panels[0].Text := sErr;
        Result := False;
      end;
    end;
  end;

{ **************************************************************** }
begin
  if (edtHTML.Text = '') then
  begin
    sb.Panels[0].Text := 'No HTML message specified';
    Exit;
  end;

  if edtFrom.Text = '' then
  begin
    sb.Panels[0].Text := 'No From specified';
    Exit;
  end;

  if edtSubject.Text = '' then
  begin
    sb.Panels[0].Text := 'No Subject specified';
    Exit;
  end;

  if edtTo.Text = '' then
  begin
    sb.Panels[0].Text := 'No To specified';
    Exit;
  end;

  for i := 0 to FAttach.Count -1 do
  begin
    if FAttach[i] = '' then
    begin
      sb.Panels[0].Text := 'No file specified for attachment
'+FAttachNames[i];
      Exit;
    end;
  end;

  iBlankParm := 0;
  for i := 0 to FParm.Count -1 do if FParm[i] = '' then inc(iBlankParm);
  if iBlankParm > 0 then
  begin
    i := MessageDlg(IntToStr(iBlankParm)+' parameters are blank, Do you want
to continue?',mtWarning,[mbYes, mbNo],0);
    if i <> mrYes then
    begin
      sb.Panels[0].Text := IntToStr(iBlankParm)+' parameters are blank';
      Exit;
    end;
  end;

  sb.Panels[0].Text := 'Sending '+edtHTML.text+' ...';
  sb.Refresh;
  IDMessage.Clear;
  IdMessage.From.Text := edtFrom.Text;
  IdMessage.Subject := edtSubject.Text;
  IdMessage.ContentType := 'multipart/related';

  iSent := 0;
  sb.Panels[1].Text := '';
  try
    Log('sending '+edtHTML.Text);
    Screen.Cursor := crHourGlass;
    try
      SMTP.Connect;
    except
      on E: Exception do
      begin
        sErr := SocketErrMsg(E.Message);
        sb.Panels[0].Text := sErr;
        Exit;
      end;
    end;

    if FToList then
    begin
      AssignFile(F,edtTo.Text);
      Reset(F);
      while (not Eof(F)) do
      begin
        Readln(F,sLine);
        IdMessage.Recipients.EMailAddresses := sLine;
        if FSpecialParms then setSpecialParms;
        initAttach;
        doHTML;
        if SendOne then inc(iSent);
      end;
      CloseFile(F);
    end
    else
    begin
      IdMessage.Recipients.EMailAddresses := edtTo.Text;
      if FSpecialParms then setSpecialParms;
      initAttach;
      doHTML;
      if SendOne then inc(iSent);
    end;

  finally
    Screen.Cursor := crDefault;
    SMTP.Disconnect;
    sb.Panels[1].Text := IntToStr(iSent)+' emails sent';
    Log(sb.Panels[1].Text);
  end;
end;

Other Threads