Einzelnen Beitrag anzeigen

ByTheTime

Registriert seit: 24. Sep 2011
Ort: Frankfurt
297 Beiträge
 
Delphi XE2 Architect
 
#1

Problem mit E-Mail senden

  Alt 2. Feb 2012, 22:01
Hallo,
ich habe Probleme beim senden einer Mail... Ich habe mal folgendes Beispiel augegraben: http://www.delphipraxis.net/157337-e...anhaengen.html
Leider funzt es bei mir nicht ganz :S Ja, ich habe natürlich Copy&Paste betrieben, hört sich meistens schon so an, das darin der Fehler liegt, doch ich konnte keinen finden und zerbreche mir den Kopf daran:

Mein Code:
Delphi-Quellcode:
unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Mapi, Vcl.StdCtrls;


type
  TStrArray = array of String;
  TForm1 = class(TForm)
    Mail1: TButton;
    procedure Mail1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

const MapiRecipClassStrings : array[1..3] of String =('to','cc','bcc');

function SendEMail(Handle:THandle; Mail:TStrings):Cardinal;

implementation

{  **** Verwendung ***

procedure TTestForm.Mail1Click(Sender: TObject);
var
  mail: TStringList;
begin
  mail:= TStringList.Create;
  try
      mail.values['to0']:= 'fifi@abc.de';
      mail.values['to1']:= 'otto@abc.de';
      mail.values['to2']:= 'Maria@abc.de';
      mail.values['to3']:= 'Joseph@abc.de';
      mail.values['cc0']:= 'detlef@abc.de';
      mail.values['cc1']:= 'Mordo@abc.de';
      mail.values['bcc0']:= 'Klaus@abc.de';
      mail.values['subject']:= 'subject';
      mail.values['body']:= 'text text text';
      mail.values['attachment0']:= 'C:\Anhang1.txt';
      mail.values['attachment1']:= 'C:\Anhang2.txt';
      mail.values['attachment2']:= 'C:\Anhang3.txt';
    SendEMail(Self.Handle, mail);
  finally
    mail.Free;
  end;
end;

}


function SendEMail(Handle:THandle; Mail:TStrings):Cardinal;
var
  MapiMessage: TMapiMessage;
  Receip: array of TMapiRecipDesc;
  Attachments: array of TMapiFileDesc;
  i,
  j,
  mc,
  ReceipCount,
  AttachCount: Integer;
  FileName: string;
  MAPI_Session: Cardinal;
  WndList: Pointer;
  MapiRecipClassCounter : array[1..3] of Integer;

  function GetMailValue (AMapiRecipClass, Index : Integer) : string;
  begin
   result:=Mail.Values[MapiRecipClassStrings[AMapiRecipClass]+IntToStr(index)];
  end;


begin
Result:= MAPI_E_FAILURE;
  if (MapiLogon(Handle, PAnsiChar(''), PAnsiChar(''), MAPI_LOGON_UI or MAPI_NEW_SESSION, 0, @MAPI_Session) <> SUCCESS_SUCCESS)
  then
    MessageBox(Handle, PChar('Fehler beim Versuch eMails zu versenden'), PChar('Error'), MB_ICONERROR or MB_OK)
  else
    begin
      for MC:=MAPI_TO to MAPI_BCC do
       begin
        MapiRecipClassCounter[MC]:=0;
        for j:= 0 to MaxInt do // Empfänger zählen
         begin
          if GetMailValue(MC,j) = 'then break;
          Inc(MapiRecipClassCounter[MC]);
         end;
       end;
      ReceipCount:=MapiRecipClassCounter[MAPI_TO]+
                   MapiRecipClassCounter[MAPI_CC]+
                   MapiRecipClassCounter[MAPI_BCC]; // Gesamtempfängerzahl errechnen
      SetLength(Receip,ReceipCount); // Empfängerarray dimensionieren
      i:=0;
      if ReceipCount>0 then
       begin
        for MC:=MAPI_TO to MAPI_BCC do
         if MapiRecipClassCounter[MC]>0 then
          for j:=0 to MapiRecipClassCounter[MC]-1 do
           begin
            Receip[i].ulReserved:= 0;
            Receip[i].ulRecipClass:= MC;
            Receip[i].lpszName:= StrNew(PAnsiChar(AnsiString(GetMailValue(MC,j))));
            Receip[i].lpszAddress:= StrNew(PAnsiChar(AnsiString('SMTP:' + GetMailValue(MC,j))));
            Receip[i].lpEntryID:=nil;
            Receip[i].ulEIDSize:= 0;
            inc(i);
           end;
        end;
      MapiMessage.nRecipCount:= ReceipCount; // Empfängeranzahl in Message setzen
      MapiMessage.lpRecips:= Pointer(Receip); // Pointer auf Empfängerarray übergeben

      if Mail.Values['subject']<>'then MapiMessage.lpszSubject:= PAnsiChar(AnsiString(Mail.Values['subject']));
      if Mail.Values['body']<>'then MapiMessage.lpszNoteText:= PAnsiChar(AnsiString(Mail.Values['body']));

      AttachCount:= 0; // Anhänge verarbeiten
      for i:= 0 to MaxInt do // zählen
       begin
        if Mail.Values['attachment' + IntToStr(i)] = 'then break;
        Inc(AttachCount);
       end;
      SetLength(Attachments, AttachCount); // array dimensinieren

      if AttachCount>0 then
       begin
        for i:=0 to AttachCount-1 do
         begin
          FileName:= Mail.Values['attachment' + IntToStr(i)]; // Einträge erzeugen
          Attachments[i].ulReserved:= 0;
          Attachments[i].flFlags:= 0;
          Attachments[i].nPosition:= ULONG($FFFFFFFF);
          Attachments[i].lpszPathName:= StrNew(PAnsiChar(AnsiString(FileName)));
          Attachments[i].lpszFileName:= StrNew(PAnsiChar(AnsiString(ExtractFileName(FileName))));
          Attachments[i].lpFileType:= nil;
         end;
        MapiMessage.nFileCount := AttachCount; // Anzahl der Anhänge in der Message setzen
        MapiMessage.lpFiles := Pointer(Attachments); // Pointer auf AnhangsArray übergeben
      end;

      WndList:= DisableTaskWindows(0); // "semimodal" machen
      try
        Result:= MapiSendMail(MAPI_Session, Handle, MapiMessage, MAPI_DIALOG, 0);// senden der Mail
      finally
        EnableTaskWindows(WndList);
        for i:=0 to ReceipCount-1 do // aufräumen
         begin
          StrDispose(Receip[i].lpszName);
          StrDispose(Receip[i].lpszAddress);
         end;
        for i:=0 to AttachCount-1 do
         begin
          StrDispose(Attachments[i].lpszPathName);
          StrDispose(Attachments[i].lpszFileName);
         end;
        Finalize(Attachments); // richtig aufräumen
        Finalize(Receip);
      end;
      MapiLogOff(MAPI_Session, Handle, 0, 0);
    end;
end;

procedure TForm1.Mail1Click(Sender: TObject);
var
  mail: TStringList;
begin
  mail:= TStringList.Create;
  try
      mail.values['to0']:= 'test@test.de';
      mail.values['subject']:= 'TEST!';
      mail.values['body']:= 'DIE TEST E-MAIL!!!';
      mail.values['attachment0']:= 'D:\TEST.txt';
    SendEMail(Self.Handle, mail);
  finally
    mail.Free;
  end;
end;

end.
Lukas
  Mit Zitat antworten Zitat