Zmiana 1 elementu tabeli zmienia poprzedni

0

Witam, szukałem metody na wysyłanie e-maila za pomocą domyślnego programu pocztowego, w końcu znalazłem natomiast natrafiłem na problem związany z załącznikami, przy przypadku z 1 załącznikiem metoda działa w porządku, natomiast jeżeli mam np. 2 załączniki, występuje dziwny problem. Przy pierwszej iteracji poprawnie ustawia dane dla 1 elementu tablicy, natomiast przy 2 iteracji ustawia zarówno dla 1 jak i dla 2 elementu. Nie wiem w czym problem proszę o pomoc.

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);
  
  { Upgrade }
  Att[p1].lpszPathName := pAnsichar(AnsiString(aAtts[p1]));
  Att[p1].lpszFileName := '';
end;
0

Pokaż jakiś większy fragment

0

Cała procedura pochodzi z: http://jkgdelphi001.blogspot.com/2014/12/how-to-send-email-in-delphi.html
natomiast nie działa tamten fragment

function SendMAPIEmail(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;
  sError: String;
begin
  try
    FillChar(Msg, SizeOf(Msg), 0);
    { get the length of all arrays passed to this function }
    LenTo := length(aTo);
    if Trim(aCC[0]) <> '' then
      LenCC := length(aCC)
    else
      LenCC := 0;
    if Trim(aBCC[0]) <> '' then
      LenBCC := length(aBCC)
    else
      LenBCC := 0;
    if Trim(aAtts[0]) <> '' then
      LenAtts := length(aAtts)
    else
      LenAtts := 0;
    { ... }
    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;
      { Upgrade }
      Recips[p1].lpszName := pAnsichar(AnsiString(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;
      { Upgrade }
      Recips[p1 + p2].lpszName := pAnsichar(AnsiString(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;
      { Upgrade }
      Recips[p1 + p2 + p3].lpszName := pAnsichar(AnsiString(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);
      { Upgrade }
      Att[p1].lpszPathName := pAnsichar(AnsiString(aAtts[p1]));
      Att[p1].lpszFileName := '';
      Att[p1].lpFileType := nil;
    end;
    { fill the message }
    with Msg do
    begin
      ulReserved := 0;
      if subject <> '' then
        { Upgrade }
        lpszSubject := pAnsichar(AnsiString(subject));
      if body <> '' then
        { Upgrade }
        lpszNoteText := pAnsichar(AnsiString(body));
      if SenderEmail <> '' then
      begin
        lpSender.ulRecipClass := MAPI_ORIG;
        if SenderName = '' then
          lpSender.lpszName := pAnsichar(AnsiString(SenderEmail))
        else
          lpSender.lpszName := pAnsichar(AnsiString(SenderName));
        lpSender.lpszAddress := pAnsichar(AnsiString(SenderEmail));
        lpSender.ulEIDSize := 0;
        lpSender.lpEntryID := nil;
        lpOriginator := @lpSender;
      end
      else
        Msg.lpOriginator := nil;
      Msg.lpszMessageType := nil;
      Msg.lpszDateReceived := nil;
      Msg.lpszConversationID := nil;
      Msg.flFlags := 0;
      Msg.nRecipCount := LenTo + LenCC + LenBCC;
      Msg.lpRecips := @Recips[0];
      Msg.nFileCount := LenAtts;
      Msg.lpFiles := @Att[0];
    end;
    MAPIModule := LoadLibrary(PWideChar(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
      case Result of
        MAPI_E_ACCESS_DENIED:
          sError := 'Access denied.';
        MAPI_E_AMBIGUOUS_RECIPIENT:
          sError := 'Ambiguous recipient.';
        MAPI_E_ATTACHMENT_NOT_FOUND:
          sError := 'Attachment not found.';
        MAPI_E_ATTACHMENT_OPEN_FAILURE:
          sError := 'Attachment open failure.';
        MAPI_E_ATTACHMENT_WRITE_FAILURE:
          sError := 'Attachment write failure.';
        MAPI_E_BAD_RECIPTYPE:
          sError := 'Bad recipient type.';
        MAPI_E_DISK_FULL:
          sError := 'Disk full.';
        MAPI_E_FAILURE:
          sError := 'Failure';
        MAPI_E_INSUFFICIENT_MEMORY:
          sError := 'Insufficient Memory.';
        MAPI_E_INVALID_EDITFIELDS:
          sError := 'Invalid Editfields.';
        MAPI_E_INVALID_MESSAGE:
          sError := 'Invalid message.';
        MAPI_E_INVALID_RECIPS:
          sError := 'Invalid recipients.';
        MAPI_E_INVALID_SESSION:
          sError := 'Invalid session.';
        MAPI_E_LOGIN_FAILURE:
          sError := 'Login failure.';
        MAPI_E_MESSAGE_IN_USE:
          sError := 'Message in use.';
        MAPI_E_NETWORK_FAILURE:
          sError := 'Network failure.';
        MAPI_E_NO_MESSAGES:
          sError := 'No messages.';
        MAPI_E_NOT_SUPPORTED:
          sError := 'Not supported.';
        MAPI_E_TEXT_TOO_LARGE:
          sError := 'Text too large.';
        MAPI_E_TOO_MANY_FILES:
          sError := 'Too many files.';
        MAPI_E_TOO_MANY_RECIPIENTS:
          sError := 'Too many recipients.';
        MAPI_E_TOO_MANY_SESSIONS:
          sError := 'Too many sessions.';
        MAPI_E_TYPE_NOT_SUPPORTED:
          sError := 'Type not supported.';
        MAPI_E_UNKNOWN_RECIPIENT:
          sError := 'Unknown Recipient';
        MAPI_E_USER_ABORT:
          sError := 'User Aborted';
      end;
      if sError <> '' then
      begin
        MessageDlg('Could not send email.  ' + sError, mtInformation, [mbOk], 0);
  Exit;
      end;
    end;
  finally
  end;
end;
0

A jesteś pewien, że to nie jest kwestia samego MAPI?
"[The use of Simple MAPI is discouraged. It may be altered or unavailable in subsequent versions of Windows.]"
https://msdn.microsoft.com/en-us/library/windows/desktop/dd296734(v=vs.85).aspx
https://msdn.microsoft.com/en-us/library/windows/desktop/dd296730%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396#

0

Na 100% bo jeszcze przed samym odpaleniem funkcji WINAPI podczas debugowania coś jest nie tak, iteruje po załącznikach przypisuje je do tablicy rekordów MapiFileDesc i podczas 2 iteracji zmienia mi zarówno w 1 rekordzie jak i 2, podejrzewam, że coś jest nie tak z wskaźnikami.

Udało się zrobić, podrzucam link z rozwiązaniem:
http://stackoverflow.com/questions/14794431/how-to-pass-string-to-an-array-of-pansichar

1 użytkowników online, w tym zalogowanych: 0, gości: 1