AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Programmierung allgemein Netzwerke Delphi [INDY] TIdSMTP und Anhänge
Thema durchsuchen
Ansicht
Themen-Optionen

[INDY] TIdSMTP und Anhänge

Ein Thema von ByTheTime · begonnen am 21. Dez 2013 · letzter Beitrag vom 3. Nov 2020
Antwort Antwort
Seite 1 von 2  1 2      
ByTheTime

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

[INDY] TIdSMTP und Anhänge

  Alt 21. Dez 2013, 13:26
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???

Gruß,
Lukas
Lukas
  Mit Zitat antworten Zitat
ByTheTime

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

AW: [INDY] TIdSMTP und Anhänge

  Alt 22. Dez 2013, 13:43
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
Lukas
  Mit Zitat antworten Zitat
ByTheTime

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

AW: [INDY] TIdSMTP und Anhänge

  Alt 24. Dez 2013, 10:39
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

P.S: Mit hilfe hiervon!
Lukas
  Mit Zitat antworten Zitat
ByTheTime

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

AW: [INDY] TIdSMTP und Anhänge

  Alt 24. Dez 2013, 11:39
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
Lukas

Geändert von ByTheTime (24. Dez 2013 um 11:40 Uhr) Grund: Code formatiert ;)
  Mit Zitat antworten Zitat
MartinK

Registriert seit: 21. Jun 2009
Ort: Germering (Germany)
89 Beiträge
 
Delphi 10.2 Tokyo Enterprise
 
#5

AW: [INDY] TIdSMTP und Anhänge

  Alt 18. Okt 2020, 11:01
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
Martin Kuhn
  Mit Zitat antworten Zitat
Benutzerbild von haentschman
haentschman

Registriert seit: 24. Okt 2006
Ort: Seifhennersdorf / Sachsen
5.292 Beiträge
 
Delphi 12 Athens
 
#6

AW: [INDY] TIdSMTP und Anhänge

  Alt 18. Okt 2020, 11:58
Hallöle...
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.

PS:
Bitte lasse das WITH weg.
  Mit Zitat antworten Zitat
Redeemer

Registriert seit: 19. Jan 2009
Ort: Kirchlinteln (LK Verden)
1.017 Beiträge
 
Delphi 2009 Professional
 
#7

AW: [INDY] TIdSMTP und Anhänge

  Alt 18. Okt 2020, 19:08
IdSSLHandler.SSLOptions .Method := sslvSSLv23; // sslvTLSv1;
Auf keinen Fall mehr TLS 1.0.
Janni
2005 PE, 2009 PA, XE2 PA
  Mit Zitat antworten Zitat
MartinK

Registriert seit: 21. Jun 2009
Ort: Germering (Germany)
89 Beiträge
 
Delphi 10.2 Tokyo Enterprise
 
#8

AW: [INDY] TIdSMTP und Anhänge

  Alt 19. Okt 2020, 09:35
Hallöle...
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.

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
Martin Kuhn
  Mit Zitat antworten Zitat
Benutzerbild von haentschman
haentschman

Registriert seit: 24. Okt 2006
Ort: Seifhennersdorf / Sachsen
5.292 Beiträge
 
Delphi 12 Athens
 
#9

AW: [INDY] TIdSMTP und Anhänge

  Alt 19. Okt 2020, 10:01
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.

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

Geändert von haentschman (19. Okt 2020 um 10:05 Uhr)
  Mit Zitat antworten Zitat
MartinK

Registriert seit: 21. Jun 2009
Ort: Germering (Germany)
89 Beiträge
 
Delphi 10.2 Tokyo Enterprise
 
#10

AW: [INDY] TIdSMTP und Anhänge

  Alt 19. Okt 2020, 15:40
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;
Martin Kuhn
  Mit Zitat antworten Zitat
Antwort Antwort
Seite 1 von 2  1 2      


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 07:30 Uhr.
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