Delphi-PRAXiS
Seite 1 von 2  1 2      

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Netzwerke (https://www.delphipraxis.net/14-netzwerke/)
-   -   Delphi [INDY] TIdSMTP und Anhänge (https://www.delphipraxis.net/178180-%5Bindy%5D-tidsmtp-und-anhaenge.html)

ByTheTime 21. Dez 2013 13:26

[INDY] TIdSMTP und Anhänge
 
Hallo,
ich kann mit meiner Mail-Unit mittlerweile ziemlich viel, allerdings noch keine Anhänge verschicken. Meine Recherche sah bis jetzt so aus:

Delphi-Quellcode:
with IdMessage do
      begin
        ContentType := 'multipart/*'; // !
        From.Address := ASender;
        for i := 0 to ARecipients.Count - 1 do
          Recipients.Add.Address := ARecipients.Strings[i];
        for i := 0 to AAttachment.Count - 1 do//!
          TIdAttachmentFile.Create(IdMessage.MessageParts, // !
            AAttachment.Strings[i]); // !
        IdMessage.MessageParts.Add(); // !
        Subject := ASubject;
        Body := ABody;
      end;
Allerdings geht das nicht. Mein Test Programm gibt einfach keine Rückmeldung. Was habe ich den hier falsch gemacht??? :gruebel:

Gruß,
Lukas

ByTheTime 22. Dez 2013 13:43

AW: [INDY] TIdSMTP und Anhänge
 
Okay, nochmal etwas genauer :)
Hier ist eine Lösung dazu, die aber bei mir nicht funkt :(

Delphi-Quellcode:
with IdMessage do
  begin
    ContentType := 'multipart/*'; // Wichtig für Anhang!

    From.Address := ASender;
    for i := 0 to ARecipients.Count - 1 do
      Recipients.Add.Address := ARecipients.Strings[i];

    for i := 0 to AAttachment.Count - 1 do // Wichtig für Anhang!
      TIdAttachmentFile.Create(IdMessage.MessageParts, // Wichtig für Anhang!
         AAttachment.Strings[i]); // Wichtig für Anhang!
    IdMessage.MessageParts.Add(); // Wichtig für Anhang!

    Subject := ASubject;
    Body := ABody;
  end;
Die mit " // Wichtig für Anhang!" markierten Zeilen habe ich aus dem verlinkten Thread entnommen, allerdings geht das schief :/

Ich hoffe es findet sich eine Lösung :)

ByTheTime 24. Dez 2013 10:39

AW: [INDY] TIdSMTP und Anhänge
 
Okay, nach viel hin und her und unzähligen Fehlversuchen, kann ich nun einen Anhang schicken :) Hier die Lösung

Delphi-Quellcode:
    with IdMessage do
      begin
        ContentType := 'multipart/' + ExtractFileExt(FileToSend);

        From.Address := ASenderAdress;
        From.Name := ASenderName;
        Subject := ASubject;

        Recipients.EMailAddresses := ARecipients;

        IdText := TIdText.Create(MessageParts, nil);
        IdText.ContentType := 'text/plain';
        IdText.Body.Text := ABody;
      end;

      if FileToSend <> '' then
      begin
        Attachment := nil;

        Attachment := TIdAttachmentFile.Create(IdMessage.MessageParts,
          FileToSend);
        Attachment.FileName := ExtractFileName(FileToSend);
        Attachment.ContentType := 'application/' + ExtractFileExt(FileToSend);
      end;
Irgendwann finde ich auch schon noch raus, wie es mit mehreren geht :D

P.S: Mit hilfe hiervon!

ByTheTime 24. Dez 2013 11:39

AW: [INDY] TIdSMTP und Anhänge
 
Okay, nach viel gesuche, ging es nun doch relativ schnell :)

Delphi-Quellcode:
    with IdMessage do
      begin
        ContentType := 'multipart/*';

        From.Address := ASenderAdress;
        From.Name := ASenderName;

        Subject := ASubject;

        Recipients.EMailAddresses := ARecipients;

        IdText := TIdText.Create(MessageParts, TStringList.Create);
        IdText.ContentType := 'text/plain';
        IdText.Body.Text := ABody;
      end;

      if Trim(Files) <> EmptyStr then
      begin
        try
          IdAttachmentList := TStringList.Create;
          IdAttachmentList.CommaText := Files;

          for i := 0 to IdAttachmentList.Count - 1 do
          begin
            IdMessage.IsEncoded := True;

            IdAttachment := TIdAttachmentFile.Create(IdMessage.MessageParts,
              IdAttachmentList.Strings[i]);
            IdAttachment.FileName :=
              ExtractFileName(IdAttachmentList.Strings[i]);
            IdAttachment.ContentType := 'application/octet-stream';
            IdAttachment.OpenLoadStream;
            IdAttachment.CloseLoadStream;
          end;
        finally
          IdAttachmentList.Free;
        end;
      end;
Hierund hier gab es noch was schönes :)

Für Verbesserungen, Optimierungen und Vorschläge bin ich gerne offen :)

Gruß,
Lukas

MartinK 18. Okt 2020 11:01

AW: [INDY] TIdSMTP und Anhänge
 
Da mir dieser Thread sehr geholfen hat, anbei meine Version einer entsprechenden kompletten "Function".
Diese beinhaltet ebenso die inzwischen häufig benötigte SSL Authentifizierung der Mail-Provider.
Getestet mit Delphi 10.2.

Delphi-Quellcode:
Function TForm1.SendSMTPEmailWAttachment(SenderName, SenderEmailAdress,
  SenderEmailPassword, SMTPHost, SendTo, SendAsCc, SendAsBCC, ReceiptAdress,
  aSubject, aBody: String; SMTPPort: Integer;Files:TStringList): boolean;

var
  IdSMTP        : TiDSMTP;
  IdMessage     : TIdMessage;
  IdText        : TIDText;
  IdSSLHandler  : TIdSSLIOHandlerSocketOpenSSL;
  IdAttachment  : TIdAttachmentFile;
  I             : Integer;

begin
  Result := FALSE;
  //----------------
  if (SendTo = '') AND (SendAsCc = '') AND (SendAsBCc = '')
    then exit;
  //----------------


  Result := True;
  // --------------
  //Creates
  IdSMTP                                       := TiDSMTP                     .Create(Application);
  IdMessage                                    := TIdMessage                  .Create(Application);
  IdSSLHandler                                 := TIdSSLIOHandlerSocketOpenSSL .Create(Application);



  // --------------
  //SSL Handler
  IdSSLHandler.SSLOptions      .Method        := sslvSSLv23; // sslvTLSv1;
  IdSSLHandler.SSLOptions      .Mode          := sslmUnassigned;
  IdSSLHandler.SSLOptions      .VerifyMode    := [];
  IdSSLHandler.SSLOptions      .VerifyDepth   := 0;
  IdSMTP                       .IOHandler     := IdSSLHandler;

  // --------------
  //Prepare the MessageObject
  with IdMessage do
    begin
      //--------------------------------------------
      ContentType                     := 'multipart/*';
      From            .Address       := SenderEmailAdress;
      From            .Name          := SenderName;
      Subject                         := aSubject;
      Recipients      .EMailAddresses := SendTo;                 // To
      CCList          .EMailAddresses := SendAsCc;               // CC
      BCCList         .EMailAddresses := SendAsBCC;              // BCC
      ReceiptRecipient .Text          := ReceiptAdress;          // to get a receipt -> From.Text;
      ReplyTo         .EMailAddresses := SenderEmailAdress;

      //Text/Body is within an IdText Instance
      IdText := TIdText.Create(MessageParts, TStringList.Create);
      IdText.ContentType := 'text/plain';
      IdText.Body.Text := ABody;
    end;

  // --------------
  //Add Attachments handed over in the TStringList called "Files"
  for I:= 0 to (Files.Count -1) do
    begin
      if (FileExists(Files.Strings[I])) AND (Files.Strings[I] <> EmptyStr)
        then begin
               try
                 IdMessage.IsEncoded := True;
                  //----------------------------------------------
                 IdAttachment                 := TIdAttachmentFile.Create(IdMessage.MessageParts,       Files.Strings[I]);
                 IdAttachment.FileName        := ExtractFileName(Files.Strings[I]);
                 IdAttachment.ContentType     := 'application/octet-stream';
                 IdAttachment.OpenLoadStream;
                 IdAttachment.CloseLoadStream;
               finally
               end;
        end;
    end;


  // --------------
  IdSMTP     .Username                        := SenderEmailAdress;
  IdSMTP     .Password                        := SenderEmailPassword;
  IdSMTP     .Host                            := SMTPHost;
  IdSMTP     .Port                            := SMTPPort;
  IdSMTP     .UseTLS                          := utUseExplicitTLS;       // utUseRequireTLS , utUseImplicitTLS; utUseExplicitTLS
  IdSMTP     .AuthType                        := satDefault;

  //---------------
  //Connect & Send
  Result := True;
  try
    IdSMTP.Connect;
    IdSMTP.Send(IdMessage);
  Except
    Result := False;
  end;
  if (Result = True)
    then IdSMTP.Disconnect;

  // --------------
  //Free memory
  IdText       .Free;
  IdMessage    .Free;
  IdSSLHandler .Free;
  IdSMTP       .Free;
end;
Viel Spaß damit
Martin

haentschman 18. Okt 2020 11:58

AW: [INDY] TIdSMTP und Anhänge
 
Hallöle...:P
Zitat:

anbei meine Version einer entsprechenden kompletten "Function"
...und nun packe das Ganze in eine Klasse, und nicht in die Form, in eine separate Unit. :zwinker:

PS:
Bitte lasse das WITH weg. :?

Redeemer 18. Okt 2020 19:08

AW: [INDY] TIdSMTP und Anhänge
 
Zitat:

Zitat von MartinK (Beitrag 1475671)
IdSSLHandler.SSLOptions .Method := sslvSSLv23; // sslvTLSv1;

Auf keinen Fall mehr TLS 1.0.

MartinK 19. Okt 2020 09:35

AW: [INDY] TIdSMTP und Anhänge
 
Zitat:

Zitat von haentschman (Beitrag 1475673)
Hallöle...:P
Zitat:

anbei meine Version einer entsprechenden kompletten "Function"
...und nun packe das Ganze in eine Klasse, und nicht in die Form, in eine separate Unit. :zwinker:

PS: Bitte lasse das WITH weg. :?

Danke für den Hinweis. Das darf gerne jemand machen für den das wichtig ist.
Ich bin wohl schon etwas zu alt und ehrlich gesagt auch zu busy um mich mit dem Thema Klassen-Erstellung zu beschäftigen. Geht auch ohne ;)

was stört Dich am "WITH" ?

vG Martin

haentschman 19. Okt 2020 10:01

AW: [INDY] TIdSMTP und Anhänge
 
Zitat:

was stört Dich am "WITH" ?
https://stackoverflow.com/questions/312321/debugging-problems-with-with-
statement-in-delphi-2006

Zitat:

The debugger will not show the values of the variables with in a class or record
...genau deshalb. :wink:

Zitat:

Geht auch ohne.
„Ein Leben ohne Mops (Klassen :zwinker:) ist möglich, aber sinnlos“ :P

MartinK 19. Okt 2020 15:40

AW: [INDY] TIdSMTP und Anhänge
 
Ok.... wer so toll des "Herrn und Meisters" Zitate verwendet(Der Mops stammt ja von Loriot) UND auch noch gleich Argument mitliefert, dem gebe ich doch gerne mein Gehör :)

Delphi-Quellcode:
Function TForm1.SendSMTPEmailWAttachment(SenderName, SenderEmailAdress,
  SenderEmailPassword, SMTPHost, SendTo, SendAsCc, SendAsBCC, ReceiptAdress,
  aSubject, aBody: String; SMTPPort: Integer;Files:TStringList): boolean;

var
  IdSMTP : TiDSMTP;
  IdMessage : TIdMessage;
  IdText : TIDText;
  IdSSLHandler : TIdSSLIOHandlerSocketOpenSSL;
  IdAttachment : TIdAttachmentFile;
  I : Integer;

begin
  Result := FALSE;
  //----------------
  if (SendTo = '') AND (SendAsCc = '') AND (SendAsBCc = '')
    then exit;
  //----------------


  Result := True;
  // --------------
  //Creates
  IdSMTP := TiDSMTP .Create(Application);
  IdMessage := TIdMessage .Create(Application);
  IdSSLHandler := TIdSSLIOHandlerSocketOpenSSL .Create(Application);


  // --------------
  //Stuff the SSL Handler
  IdSSLHandler.SSLOptions .Method := sslvSSLv23; //or another version....
  IdSSLHandler.SSLOptions .Mode := sslmUnassigned;
  IdSSLHandler.SSLOptions .VerifyMode := [];
  IdSSLHandler.SSLOptions .VerifyDepth := 0;
  IdSMTP .IOHandler := IdSSLHandler;

  // --------------
  //Prepare the MessageObject
  IdMessage.ContentType := 'multipart/*';
  IdMessage.From .Address := SenderEmailAdress;
  IdMessage.From .Name := SenderName;
  IdMessage.Subject := aSubject;
  IdMessage.Recipients .EMailAddresses := SendTo; // To
  IdMessage.CCList .EMailAddresses := SendAsCc; // CC
  IdMessage.BCCList .EMailAddresses := SendAsBCC; // BCC
  IdMessage.ReceiptRecipient .Text := ReceiptAdress; // to get a receipt -> From.Text;
  IdMessage.ReplyTo .EMailAddresses := SenderEmailAdress;

  //Text/Body is within an IdText Instance
  IdMessage.IdText := TIdText.Create(MessageParts, TStringList.Create);
  IdMessage.IdText.ContentType := 'text/plain';
  IdMessage.IdText.Body.Text := ABody;

  // --------------
  //Add Attachments handed over in the TStringList called "Files"
  for I:= 0 to (Files.Count -1) do
    begin
      if (FileExists(Files.Strings[I])) AND (Files.Strings[I] <> EmptyStr)
        then begin
               try
                 IdMessage.IsEncoded := True;
                  //----------------------------------------------
                 IdAttachment := TIdAttachmentFile.Create(IdMessage.MessageParts, Files.Strings[I]);
                 IdAttachment.FileName := ExtractFileName(Files.Strings[I]);
                 IdAttachment.ContentType := 'application/octet-stream';
                 IdAttachment.OpenLoadStream;
                 IdAttachment.CloseLoadStream;
               finally
               end;
        end;
    end;


  // --------------
  IdSMTP .Username := SenderEmailAdress;
  IdSMTP .Password := SenderEmailPassword;
  IdSMTP .Host := SMTPHost;
  IdSMTP .Port := SMTPPort;
  IdSMTP .UseTLS := utUseExplicitTLS; // utUseRequireTLS , utUseImplicitTLS; utUseExplicitTLS
  IdSMTP .AuthType := satDefault;

  //---------------
  //Connect & Send
  Result := True;
  try
    IdSMTP.Connect;
    IdSMTP.Send(IdMessage);
  Except
    Result := False;
  end;
  if (Result = True)
    then IdSMTP.Disconnect;

  // --------------
  //Free memory
  IdText .Free;
  IdMessage .Free;
  IdSSLHandler .Free;
  IdSMTP .Free;
end;


Alle Zeitangaben in WEZ +1. Es ist jetzt 11:15 Uhr.
Seite 1 von 2  1 2      

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