Board index » delphi » Sending D5-Email

Sending D5-Email

Hi,

I'am using Delphi 5. I would like to silently fill several (approx. 50)
emails into the outbox of my default email manager.  My emails could run
into 2-3 pages each. Can I do this ? Can I send an attachment and CC address
too ?

Currently I use the following code for tiny messages.

shellexecute(handle , 'open' ,
   pChar(
         'mailto: a...@xx.com '+
         '?subject=Documents Cleared '+
         '&Body=This is the list of passengers ...'
) , '' , '' , SW_SHOW );

Problems with above code
1) Large emails are not sent
2) Outlook express gets loaded unneccessarily, If I do not use the 'open'
parameter then the outbox is not filled.

Regards
Allan

 

Re:Sending D5-Email


There is no need to use shell execute

there are some great email email components
for instance the Indy or the default NMSMTP component in the FASTNET Tab on
your component pallet

otherwise just call your Email Api ( Mapi )
that way your email client will handle the send-recieve of the emails

personaly
i prever to use Indy message

Fons Seesink
Actisoft

"Allan Fernandes" <Softsm...@mantraonline.com> schreef in bericht
news:3b94c678_1@dnews...

Quote
> Hi,

> I'am using Delphi 5. I would like to silently fill several (approx. 50)
> emails into the outbox of my default email manager.  My emails could run
> into 2-3 pages each. Can I do this ? Can I send an attachment and CC
address
> too ?

> Currently I use the following code for tiny messages.

> shellexecute(handle , 'open' ,
>    pChar(
>          'mailto: a...@xx.com '+
>          '?subject=Documents Cleared '+
>          '&Body=This is the list of passengers ...'
> ) , '' , '' , SW_SHOW );

> Problems with above code
> 1) Large emails are not sent
> 2) Outlook express gets loaded unneccessarily, If I do not use the 'open'
> parameter then the outbox is not filled.

> Regards
> Allan

Re:Sending D5-Email


You can change this function to meet your needs.

Bruno

Re:Sending D5-Email


Be sure MAPI is active on machine.

// Send to MAPI using array of consts
// You can change this function to work with TStrings instead array of conts
easy
function SendToMAPI(const aTo,aCC,aBCC,aAtts: array of string;
                    const body,subject,SenderName,SenderEmail: string;
                    ShowError: boolean = true):Integer;
var
  SM: TFNMapiSendMail;
  MAPIModule: HModule;

  Msg: MapiMessage;
  lpSender: MapiRecipDesc;
  Recips: array of MapiRecipDesc;
  Att: array of MapiFileDesc;
  p1,p2,p3,LenTo,LenCC,LenBCC,LenAtts: integer;
  sErro: string;
begin
  try
    FillChar(Msg, SizeOf(Msg), 0);
    { get the length of all array passed to this function }
    LenTo := Length(aTo);
    LenCC := Length(aCC);
    LenBCC := Length(aBCC);
    LenAtts := Length(aAtts);
    { ... }
    Setlength(Recips,LenTo+LenCC+LenBCC);
    Setlength(Att,LenAtts);
    { to }
    for p1 := 0 to LenTo - 1 do begin
      FillChar(Recips[p1], SizeOf(Recips[p1]), 0);
      Recips[p1].ulReserved := 0;
      Recips[p1].ulRecipClass := MAPI_TO;
      Recips[p1].lpszName := pchar(aTo[p1]);
      Recips[p1].lpszAddress := '';
    end;
    { cc }
    for p2 := 0 to LenCC - 1 do begin
      FillChar(Recips[p1+p2], SizeOf(Recips[p1+p2]), 0);
      Recips[p1+p2].ulReserved := 0;
      Recips[p1+p2].ulRecipClass := MAPI_CC;
      Recips[p1+p2].lpszName := pchar(aCC[p2]);
      Recips[p1+p2].lpszAddress := '';
    end;
    { bcc }
    for p3 := 0 to LenBCC - 1 do begin
      FillChar(Recips[p1+p2+p3], SizeOf(Recips[p1+p2+p3]), 0);
      Recips[p1+p2+p3].ulReserved := 0;
      Recips[p1+p2+p3].ulRecipClass := MAPI_BCC;
      Recips[p1+p2+p3].lpszName := pchar(aBCC[p3]);
      Recips[p1+p2+p3].lpszAddress := '';
    end;
    { atts }
    for p1 := 0 to LenAtts - 1 do begin
      FillChar(Att[p1], SizeOf(Att[p1]), 0);
      Att[p1].ulReserved := 0;
      Att[p1].flFlags := 0;
      Att[p1].nPosition := Cardinal($FFFFFFFF); // ULONG(-1);
      Att[p1].lpszPathName := pchar(aAtts[p1]);
      Att[p1].lpszFileName := '';
      Att[p1].lpFileType := 0;
    end;
    { fill the message }
    with Msg do begin
      ulReserved := 0;
      if subject <> '' then
        lpszSubject := pChar(subject);
      if body <> '' then
        lpszNoteText := pchar(body);
      if SenderEmail <> '' then begin
        lpSender.ulRecipClass := MAPI_ORIG;
        if SenderName = '' then
          lpSender.lpszName := pchar(SenderEmail)
        else
          lpSender.lpszName := pchar(SenderName);
        lpSender.lpszAddress := pchar(SenderEmail);
        lpSender.ulEIDSize := 0;
        lpSender.lpEntryID := nil;
        lpOriginator := @lpSender;
      end
      else
        Msg.lpOriginator := 0;
      Msg.lpszMessageType := 0;
      Msg.lpszDateReceived := 0;
      Msg.lpszConversationID := 0;
      Msg.flFlags := 0;
      Msg.nRecipCount := LenTo + LenCC + LenBCC;
      Msg.lpRecips := @Recips[0];
      Msg.nFileCount := LenAtts;
      Msg.lpFiles := @Att[0];
    end;
    MAPIModule := LoadLibrary(PChar(MAPIDLL));
    if MAPIModule = 0 then
      Result := -1
    else
      try
        @SM := GetProcAddress(MAPIModule, 'MAPISendMail');
        if @SM <> nil then begin
          Result := SM(0, Application.Handle, Msg, MAPI_DIALOG or
MAPI_LOGON_UI, 0);
        end
        else
          Result := 1;
      finally
        FreeLibrary(MAPIModule);
      end;
    if result <> SUCCESS_SUCCESS then begin
      // Here I know that exist better way to get error strings direct from
api calls
      // If someone know how do this, please email me
      case result of
        MAPI_E_AMBIGUOUS_RECIPIENT:         sErro :=
'"MAPI_E_AMBIGUOUS_RECIPIENT"';
        MAPI_E_ATTACHMENT_NOT_FOUND:        sErro :=
'"MAPI_E_ATTACHMENT_NOT_FOUND"';
        MAPI_E_ATTACHMENT_OPEN_FAILURE:     sErro :=
'"MAPI_E_ATTACHMENT_OPEN_FAILURE"';
        MAPI_E_BAD_RECIPTYPE:               sErro :=
'"MAPI_E_BAD_RECIPTYPE"';
        MAPI_E_FAILURE:                     sErro := '"MAPI_E_FAILURE"';
        MAPI_E_INSUFFICIENT_MEMORY:         sErro :=
'"MAPI_E_INSUFFICIENT_MEMORY"';
        MAPI_E_LOGIN_FAILURE:               sErro :=
'"MAPI_E_LOGIN_FAILURE"';
        MAPI_E_TEXT_TOO_LARGE:              sErro :=
'"MAPI_E_TEXT_TOO_LARGE"';
        MAPI_E_TOO_MANY_FILES:              sErro :=
'"MAPI_E_TOO_MANY_FILES"';
        MAPI_E_TOO_MANY_RECIPIENTS:         sErro :=
'"MAPI_E_TOO_MANY_RECIPIENTS"';
        MAPI_E_UNKNOWN_RECIPIENT:           sErro :=
'"MAPI_E_UNKNOWN_RECIPIENT"';
        MAPI_E_USER_ABORT:                  sErro := '"MAPI_E_USER_ABORT"';
      end;
      if ShowError then
        MessageDlg('Error sending mail (' + sErro + ').', mtError,[mbOK],
0);
    end;
  finally
  end;
end;

Use like this:

 SendToMAPI(
    ['to_...@email.com.br','to_...@email.com.br'],  // To
    ['cc_...@email.com.br','cc_...@email.com.br'],  // CC
    [],                                             // BCC
    ['c:\autoexec.bat','c:\config.sys'],            // Atts
    'Corpo da mensagem',                            // Body
    'Assunto',                                      // Subject
    'Bruno Lovatti',                                // Sender Name
    'blova...@vix.terra.com.br',                    // Sender Email
    true                                            // Show Error or you
want to catch the error?
            );

Bruno Lovatti

Re:Sending D5-Email


Dear Bruno & Fons and friends,

I used the example Bruno has detailed out and my job has been accomplished.
I was curious to know about Indy. The whole newsgroup is flooded with it. I
went to the site but it seems it is for Delphi 6 only. I have Delphi 5 ?

Million Thanks
Allan

Re:Sending D5-Email


"Allan Fernandes" <Softsm...@mantraonline.com> wrote in news:3b96f828_2@dnews:

Quote
> I used the example Bruno has detailed out and my job has been accomplished.
> I was curious to know about Indy. The whole newsgroup is flooded with it. I
> went to the site but it seems it is for Delphi 6 only. I have Delphi 5 ?

Read further down. Indy works for D4, 5, and 6.

--
Chad Z. Hower (Kudzu) - http://www.pbe.com/Kudzu/
Current Location: St. Petersburg, Russia
      "Programming is an art form that fights back"

Other Threads