Link to home
Start Free TrialLog in
Avatar of rfwoolf
rfwoolfFlag for South Africa

asked on

using MAPI to send to a BCC address

This is a question that has been asked at least 3 times on EE, but the answer has been so vague that future idiots like me cannot understand them:
https://www.experts-exchange.com/questions/20625310/MAPI-How-to-set-the-receiver-address-into-BCC-field.html

I am trying to add a BCC field to an email generated using MAPI.


Below you will find the code used to add a recipient to the MAPI_TO part of the email.
I need to keep this, and ADD a recipient to the MAPI_BCC part of the email.

I can't seem to do both.

if (RecipientEmail <> '') then
    begin
    //to field
      lpRecipient.ulRecipClass := MAPI_TO;
      if (RecipientName = '') then
        lpRecipient.lpszName := PChar(RecipientEMail)
      else
        lpRecipient.lpszName := PChar(RecipientName);
      lpRecipient.lpszAddress := PChar(RecipientEmail);
      lpRecipient.ulReserved := 0;
      lpRecipient.ulEIDSize := 0;
      lpRecipient.lpEntryID := nil;
      nRecipCount := 1;
      lpRecips := @lpRecipient;

Open in new window

Avatar of rfwoolf
rfwoolf
Flag of South Africa image

ASKER

For your reference here is the full SendMail (MAPI) procedure:

function SendMail(const Subject, Body, FileName,
                  SenderName, SenderEMail,
                  RecipientName, RecipientEMail: string): Integer;
var
  Message: TMapiMessage;
  lpSender, lpRecipient: TMapiRecipDesc;
  FileAttach: TMapiFileDesc;
 
  SM: TFNMapiSendMail;
  MAPIModule: HModule;
begin
  FillChar(Message, SizeOf(Message), 0);
  with Message do
  begin
    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.ulReserved := 0;
      lpSender.ulEIDSize := 0;
      lpSender.lpEntryID := nil;
      lpOriginator := @lpSender;
    end;
 
    if (RecipientEmail <> '') then
    begin
    //to field
      lpRecipient.ulRecipClass := MAPI_TO;
      if (RecipientName = '') then
        lpRecipient.lpszName := PChar(RecipientEMail)
      else
        lpRecipient.lpszName := PChar(RecipientName);
      lpRecipient.lpszAddress := PChar(RecipientEmail);
      lpRecipient.ulReserved := 0;
      lpRecipient.ulEIDSize := 0;
      lpRecipient.lpEntryID := nil;
      nRecipCount := 2;
      lpRecips := @lpRecipient;
    end
    else
      lpRecips := nil;
 
    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
    Result := -1
  else
    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
        Result := 1;
    finally
      FreeLibrary(MAPIModule);
    end;
 
  if Result <> 0 then
    MessageDlg('Error sending mail (' + IntToStr(Result) + ').', mtError,
[mbOK], 0);
end;

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Russell Libby
Russell Libby
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rfwoolf

ASKER

Amazing stuff, thank you!! Sorry for the late acceptance.
Avatar of rfwoolf

ASKER

Thank you for the excellent solution!
No problem on the delay, glad you found it useful

Russell