Delphi-PRAXiS
Seite 2 von 2     12   

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Programmieren allgemein (https://www.delphipraxis.net/40-programmieren-allgemein/)
-   -   Delphi Email versenden mit Outlook Online bzw. Outlook Hybrid (https://www.delphipraxis.net/215095-email-versenden-mit-outlook-online-bzw-outlook-hybrid.html)

himitsu 15. Mai 2024 10:44

AW: Email versenden mit Outlook Online bzw. Outlook Hybrid
 
Liste der Anhänge anzeigen (Anzahl: 1)
Ne alte Version auf die Schnelle gefunden.
Kann aber nicht viel zu sagen. (war damals ein jetzt ehemaliger Kollege)
Im Kommentar steht:
Code:
OfficeAdapter.exe
  /mail /to=service@example.de /bcc=mail1@example.de /cc=mail2@example.de
  /cc=mail3@example.de /head="Titel" /body="Nachrichtentext ... "
  /file="C:\Test1.txt" /file="C:\Test2.pdf" ...
Wir verwenden auch nur das TO (k.A. ob BCC und CC funktionieren).

Wir nutzen eine Klasse, welche alle Texte und Dateien bekommt und dann, je nach Setting, irgendwas zum Senden nutzt.
So gibt es hier auch noch einen Code, welcher so mal vor vielen vielen Jahren aus Eurekalog kopiert wurde.
MSDN-Library durchsuchenMAPISendMail aus der mapi32.dll (im DLLPath in HKCU und HKLM unter SOFTWARE\Clients\Mail gesucht)

Und, wie gesagt, nutzen wir aktuell vorwiegend eine *.EML, wo per Hand über eine StringList ein HTML zusammengeklöppelt wird, dass dann mit den Dateianhängen als multipart/mixed über ein TIdMessage zusammengeführt, SaveToFile und via ShellExecute im Standard-Mailprogramm geöffnet.
Wichtig: Im Header muß
Delphi-Quellcode:
X-Unsent: 1
stehen, damit Outlook es auch zum Versenden lädt und nicht bloß anzeigt.

sh17 15. Mai 2024 10:52

AW: Email versenden mit Outlook Online bzw. Outlook Hybrid
 
Dankeschön, schau ich mir an

himitsu 15. Mai 2024 11:12

AW: Email versenden mit Outlook Online bzw. Outlook Hybrid
 
Wir hab sogar einen Automatik-Modus (der aber eigentlich kaum noch genutzt wird),
quasi mit folgenden Settings:
* per EML
* per MAPI
* per OfficeAdapter
* MAPI und wenn nicht OfficeAdapter
* OfficeAdapter und wenn nicht MAPI

Leider ging das mit der Automatik auch nicht immer, denn sowohl MAPISendMail als auch die .NET-API
behaupten manchmal es ging nicht, obwohl es erfolgreich versendet wurde,
aber auch es ging, obwohl nichts gesendet wurde.
Also entweder wurde das MailProgramm dann doppelt geöffnet oder garnicht. :freak:

sh17 15. Mai 2024 11:43

AW: Email versenden mit Outlook Online bzw. Outlook Hybrid
 
Wenn man darüber nachdenkt, eigentlich totaler Irrsinn, seit Urzeiten gibt es E-Mail und E-Mail-Clients und seit dem gibt es immer
noch nichts wirklich einheitliches. E-Mails zu versenden ist ja eine grundlegende Aufgabe, die in jeder Software benötigt wird.

KHJ 9. Okt 2025 10:49

AW: Email versenden mit Outlook Online bzw. Outlook Hybrid
 
Hier ein kurzes Snippet

Code:
procedure TForm1.btnSaveEMLClick(Sender: TObject);
var
  EMLContent: TStringList;
  AttachmentStream: TFileStream;
  EncodedAttachment: TStringStream;
  Lines: TArray<string>;
  Line: string;
  I: Integer;
  Boundary, FileName: string;
begin
  with TSaveDialog.Create(nil) do
    try
      Filter := 'EML-Dateien (*.eml)|*.eml';
      DefaultExt := 'eml';
      Title := 'E-Mail speichern';

      if Execute then
      begin
        FileName := FileName;
        Boundary := '----=_Part_' + FormatDateTime('yyyymmddhhnnss', Now);

        EMLContent := TStringList.Create;
        try
          EMLContent.Add('X-Unsent: 1'); // Wichtig!
          EMLContent.Add('From: ' + EditFrom.Text);
          EMLContent.Add('To: ' + EditTo.Text);
          EMLContent.Add('Cc: ' + EditCc.Text);
          EMLContent.Add('Bcc: ' + EditBcc.Text);
          EMLContent.Add('Subject: ' + EditSubject.Text);
          EMLContent.Add('MIME-Version: 1.0');
          EMLContent.Add('Content-Type: multipart/mixed; boundary="' + Boundary + '"');
          EMLContent.Add('');
          EMLContent.Add('This is a multi-part message in MIME format.');
          EMLContent.Add('');

          // Textteil
          EMLContent.Add('--' + Boundary);
          EMLContent.Add('Content-Type: text/plain; charset="utf-8"');
          EMLContent.Add('Content-Transfer-Encoding: 7bit');
          EMLContent.Add('');
          EMLContent.Add(MemoBody.Text);
          EMLContent.Add('');

          // Anhang
          if FileExists(EditAttachment.Text) then
          begin
            EMLContent.Add('--' + Boundary);
            EMLContent.Add('Content-Type: application/octet-stream; name="' + ExtractFileName(EditAttachment.Text) + '"');
            EMLContent.Add('Content-Transfer-Encoding: base64');
            EMLContent.Add('Content-Disposition: attachment; filename="' + ExtractFileName(EditAttachment.Text) + '"');
            EMLContent.Add('');

            AttachmentStream := TFileStream.Create(EditAttachment.Text, fmOpenRead or fmShareDenyWrite);
            EncodedAttachment := TStringStream.Create('', TEncoding.UTF8);
            try
              TNetEncoding.Base64.Encode(AttachmentStream, EncodedAttachment);

              // Inhalt als String holen und in Zeilen aufteilen
              Lines := EncodedAttachment.DataString.Split([sLineBreak]);

              // Zeilenweise in EMLContent einfügen
              for I := 0 to High(Lines) do
              begin
                Line := Trim(Lines[I]);
                if Line <> '' then
                  EMLContent.Add(Line);
              end;
            finally
              AttachmentStream.Free;
              EncodedAttachment.Free;
            end;
          end;

          EMLContent.Add('');
          EMLContent.Add('--' + Boundary + '--');
          EMLContent.SaveToFile(FileName);

          ShellExecute(0, 'open', PChar(FileName), nil, nil, SW_SHOWNORMAL);
        finally
          EMLContent.Free;
        end;
      end;
    finally
      Free;
    end;
end;

peterbelow 9. Okt 2025 11:50

AW: Email versenden mit Outlook Online bzw. Outlook Hybrid
 
Zitat:

Zitat von KHJ (Beitrag 1552613)
Hier ein kurzes Snippet

Ein gutes Beispiel wieso man with-Statements tunlichst vermeiden sollte. Was glaubst Du was

Delphi-Quellcode:
  Filename := Filename;
da macht?

KHJ 9. Okt 2025 12:57

AW: Email versenden mit Outlook Online bzw. Outlook Hybrid
 
Good catch!
Danke für den Hinweis.


Alle Zeitangaben in WEZ +1. Es ist jetzt 08:29 Uhr.
Seite 2 von 2     12   

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz