Einzelnen Beitrag anzeigen

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, 16: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