We help IT Professionals succeed at work.

Attach file while sending e-mail

ben_scraft
ben_scraft asked
on
Medium Priority
262 Views
Last Modified: 2010-04-06
How to attach file while sending e-mail, i'm using smtp for sending e-mail, is it possible if i want to attach other file using smtp, not fttp ?
Comment
Watch Question

Commented:
Yes it's possible. What SMTP component are you using ?
CERTIFIED EXPERT

Commented:
For TNMSMTP:

NMSMTP1.PostMessage.Attachments.Add('c:\path\YourFileName');

Regards, Geo
Mohammed NasmanSoftware Developer
CERTIFIED EXPERT

Commented:
Hello

  you can find delphi demo for sending emails with attachments
\Demos\FastNet\Smtp

and if you have delphi 6 or indy component, there's also

mail client sample with attachment
\Demos\Indy\MailClient
Commented:
Hi ben,

procedure TForm1.Button1Click(Sender: TObject);
begin
  NMSMTP1.Host := 'mail.host.com';
  NMSMTP1.UserID := 'Username';
  NMSMTP1.Connect;
  NMSMTP1.PostMessage.FromAddress := 'webmaster@swissdelphicenter.ch';
  NMSMTP1.PostMessage.ToAddress.Text := 'user@host.com';
  NMSMTP1.PostMessage.ToCarbonCopy.Text := 'AnotherUser@host.com';
  NMSMTP1.PostMessage.ToBlindCarbonCopy.Text := 'AnotherUser@host.com';
  NMSMTP1.PostMessage.Body.Text := 'This is the message';
  NMSMTP1.PostMessage.Attachments.text := 'c:\File.txt';
  NMSMTP1.PostMessage.Subject := 'Mail subject';
  NMSMTP1.SendMail;
  showmessage('Mail sent !');
  NMSMTP1.Disconnect;
end;

Best Regards

Cesario

Author

Commented:
Hi Cesario,
actually i found the component T_SMTP already, but may i know are you using standard component to do that ?
But by the way thank's for trying to help me, i hope next time you could still help me

Best Regards


Benny

Mohammed NasmanSoftware Developer
CERTIFIED EXPERT

Commented:
NMSMTP1 come with delphi professioanl or enterprise edition, you seems that you have the standard edition, but you can download free internet component from indy, you can donwload it form
http://www.nevrona.com/indy/

and there's a full email client sample, and many more for internet

Commented:
Hi Benny,

The TNMSMTP is standart Component  and comes with Delphi.




If you do not want to rely on Outlook to send an email but you know that MAPI is installed, then you can also send mails with the following handy routine SendMailMAPI().

Note that MAPI is not always installed with Windows.


program MAPIMail;

function SendMailMAPI(const Subject, Body, FileName, SenderName, SenderEMail,
                  RecepientName, RecepientEMail: String) : Integer;
var
  message: TMapiMessage;
  lpSender,
  lpRecepient: TMapiRecipDesc;
  FileAttach: TMapiFileDesc;
  SM: TFNMapiSendMail;
  MAPIModule: HModule;
begin
  FillChar(message, SizeOf(message), 0);
  with message do
  begin
    if (Subject<>'') then
    begin
      lpszSubject := PChar(Subject)
    end;
    if (Body<>'') then
    begin
      lpszNoteText := PChar(Body)
    end;
    if (SenderEMail<>'') then
    begin
      lpSender.ulRecipClass := MAPI_ORIG;
      if (SenderName='') then
      begin
        lpSender.lpszName := PChar(SenderEMail)
      end
      else
      begin
        lpSender.lpszName := PChar(SenderName)
      end;
      lpSender.lpszAddress := PChar(SenderEMail);
      lpSender.ulReserved := 0;
      lpSender.ulEIDSize := 0;
      lpSender.lpEntryID := nil;
      lpOriginator := @lpSender;
    end;
    if (RecepientEMail<>'') then
    begin
      lpRecepient.ulRecipClass := MAPI_TO;
      if (RecepientName='') then
      begin
        lpRecepient.lpszName := PChar(RecepientEMail)
      end
      else
      begin
        lpRecepient.lpszName := PChar(RecepientName)
      end;
      lpRecepient.lpszAddress := PChar(RecepientEMail);
      lpRecepient.ulReserved := 0;
      lpRecepient.ulEIDSize := 0;
      lpRecepient.lpEntryID := nil;
      nRecipCount := 1;
      lpRecips := @lpRecepient;
    end
    else
    begin
      lpRecips := nil
    end;
    if (FileName='') then
    begin
      nFileCount := 0;
      lpFiles := nil;
    end
    else
    begin
      FillChar(FileAttach, SizeOf(FileAttach), 0);
      FileAttach.nPosition := Cardinal($FFFFFFFF);
      FileAttach.lpszPathName := PChar(FileName);
      nFileCount := 1;
      lpFiles := @FileAttach;
    end;
  end;
  MAPIModule := LoadLibrary(PChar(MAPIDLL));
  if MAPIModule=0 then
  begin
    Result := -1
  end
  else
  begin
    try
      @SM := GetProcAddress(MAPIModule, 'MAPISendMail');
      if @SM<>nil then
      begin
        Result := SM(0, Application.Handle, message, MAPI_DIALOG or
                     MAPI_LOGON_UI, 0);
      end
      else
      begin
        Result := 1
      end;

    finally
      FreeLibrary(MAPIModule);
    end;
  end
  if Result<>0 then
  begin
    MessageDlg('Error sending mail ('+IntToStr(Result)+').', mtError, [mbOk],
               0)
  end;
end;

begin

end.

Author

Commented:
Actually i'm using Delphi3 not Delphi5, but anyway thank's for your help, but if you know how to use standard component for delphi 3 to attach, i also still want to know, thank's

Regards

Ben