![]() |
[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:
Allerdings geht das nicht. Mein Test Programm gibt einfach keine Rückmeldung. Was habe ich den hier falsch gemacht??? :gruebel:
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; Gruß, Lukas |
AW: [INDY] TIdSMTP und Anhänge
Okay, nochmal etwas genauer :)
![]()
Delphi-Quellcode:
Die mit " // Wichtig für Anhang!" markierten Zeilen habe ich aus dem verlinkten Thread entnommen, allerdings geht das schief :/
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; Ich hoffe es findet sich eine Lösung :) |
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:
Irgendwann finde ich auch schon noch raus, wie es mit mehreren geht :D
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; P.S: Mit hilfe ![]() |
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; ![]() ![]() Für Verbesserungen, Optimierungen und Vorschläge bin ich gerne offen :) Gruß, Lukas |
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:
Viel Spaß damit
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; Martin |
AW: [INDY] TIdSMTP und Anhänge
Hallöle...:P
Zitat:
PS: Bitte lasse das WITH weg. :? |
AW: [INDY] TIdSMTP und Anhänge
Zitat:
|
AW: [INDY] TIdSMTP und Anhänge
Zitat:
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 |
AW: [INDY] TIdSMTP und Anhänge
Zitat:
![]() statement-in-delphi-2006 Zitat:
Zitat:
|
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 10:57 Uhr. |
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 by Thomas Breitkreuz