Delphi-PRAXiS
Seite 2 von 3     12 3      

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Netzwerke (https://www.delphipraxis.net/14-netzwerke/)
-   -   Delphi Indy: Googlemail smtp Einstellungen (https://www.delphipraxis.net/186912-indy-googlemail-smtp-einstellungen.html)

MrSpock 12. Okt 2015 15:54

AW: Indy: Googlemail smtp Einstellungen
 
Hab ich bereits aktiviert, weil es einmal nicht mehr mit Thunderbird funktioni hatte.

Sir Rufo 12. Okt 2015 17:24

AW: Indy: Googlemail smtp Einstellungen
 
Da ich ja - bekanntermassen faul bin - sieht so ein Mailversand bei mir so aus:
Delphi-Quellcode:
unit Forms.MainForm;

interface

uses
  SendMail,

  System.Generics.Collections,
  Winapi.Windows, Winapi.Messages,
  System.SysUtils, System.Variants, System.Classes,
  Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TMainForm = class( TForm )
    Button1: TButton;
    procedure Button1Click( Sender: TObject );
  private
    FMailer       : ISendMail;
    FConfigurations: TObjectList<TSendMailConfiguration>;
  public
    procedure AfterConstruction; override;
    procedure BeforeDestruction; override;
  end;

var
  MainForm: TMainForm;

implementation

{$R *.dfm}

uses
  SendMail.Impl.IndySendMail;

procedure TMainForm.AfterConstruction;
begin
  inherited;
  FMailer        := TIndySendMail.Create;
  FConfigurations := TObjectList<TSendMailConfiguration>.Create( True );
  FConfigurations.Add( TSendMailConfiguration.Create );

  FConfigurations[ 0 ].name       := 'gmail';
  FConfigurations[ 0 ].Host       := 'smtp.gmail.com';
  FConfigurations[ 0 ].UseSSL     := True;
  FConfigurations[ 0 ].UseStartTLS := True;
  FConfigurations[ 0 ].AuthType   := TSmtpAuthType.Password;
  FConfigurations[ 0 ].Username   := 'username';
  FConfigurations[ 0 ].Password   := 'password';
end;

procedure TMainForm.BeforeDestruction;
begin
  FConfigurations.Free;
  inherited;
end;

procedure TMainForm.Button1Click( Sender: TObject );
var
  LMail: TMailContainer;
begin
  LMail := TMailContainer.Create;
  try
    LMail.Receiver := 'receiver@example.de';
    LMail.Subject := 'Test Mail';
    LMail.Body    := 'moin moin';

    LMail.Sender := 'sender@gmail.com';
    FMailer.Send( FConfigurations[ 0 ], LMail );
  finally
    LMail.Free;
  end;
end;

end.
Und den Rest kann man immer ganz hübsch wieder verwenden und/oder bei Bedarf erweitern:
  • Basis
    Delphi-Quellcode:
    unit SendMail;

    interface

    {$SCOPEDENUMS ON}

    type
      TSmtpAuthType = ( None, Extern, KerberosV5, NTLM, MD5CR, SHA1CR, Password );
      TSmtpAuthTypes = set of TSmtpAuthType;

      TSendMailConfiguration = class
      private const
        DefaultPort           = 25;
        DefaultImplicitTLSPort = 465;
        DefaultExplicitTLSPort = 587;
      private
        FName          : string;
        FHost          : string;
        FPort          : Word;
        FUseSSL        : Boolean;
        FAuthType      : TSmtpAuthType;
        FUsername      : string;
        FPassword      : string;
        FTLSCertifcateId: string;
        FDomain        : string;
        FUseStartTLS   : Boolean;
        procedure SetUseSSL( const Value: Boolean );
        procedure SetDefaultPort;
        procedure SetUseStartTLS( const Value: Boolean );
      public
        constructor Create;
        property name: string read FName write FName;
        property TLSCertifcateId: string read FTLSCertifcateId write FTLSCertifcateId;
        property Host: string read FHost write FHost;
        property Port: Word read FPort write FPort;
        property UseSSL: Boolean read FUseSSL write SetUseSSL;
        property UseStartTLS: Boolean read FUseStartTLS write SetUseStartTLS;
        property AuthType: TSmtpAuthType read FAuthType write FAuthType;
        property Domain: string read FDomain write FDomain;
        property Username: string read FUsername write FUsername;
        property Password: string read FPassword write FPassword;
      end;

      TMailContainer = class
      private
        FSender : string;
        FReceiver: string;
        FSubject : string;
        FBody   : string;
      public
        property Sender : string read FSender write FSender;
        property Receiver: string read FReceiver write FReceiver;
        property Subject : string read FSubject write FSubject;
        property Body   : string read FBody write FBody;
      end;

    type
      ISendMail = interface
        [ '{9F6A6EC3-86B5-4E9C-A7B1-42FE1DC35B24}' ]
        procedure Send( Configuration: TSendMailConfiguration; Mail: TMailContainer );

        function GetSupportedAuthTypes: TSmtpAuthTypes;
        property SupportedAuthTypes: TSmtpAuthTypes read GetSupportedAuthTypes;
      end;

    implementation

    { TSendMailConfiguration }

    constructor TSendMailConfiguration.Create;
    begin
      inherited Create;
      SetDefaultPort;
    end;

    procedure TSendMailConfiguration.SetDefaultPort;
    begin
      if FUseSSL
      then
        begin
          if FUseStartTLS
          then
            FPort := DefaultExplicitTLSPort
          else
            FPort := DefaultImplicitTLSPort
        end
      else
        FPort := DefaultPort;
    end;

    procedure TSendMailConfiguration.SetUseSSL( const Value: Boolean );
    begin
      if FUseSSL <> Value
      then
        begin
          FUseSSL := Value;
          SetDefaultPort;
        end;
    end;

    procedure TSendMailConfiguration.SetUseStartTLS( const Value: Boolean );
    begin
      if FUseStartTLS <> Value
      then
        begin
          FUseStartTLS := Value;
          SetDefaultPort;
        end;
    end;

    end.
  • Indy-Implementierung
    Delphi-Quellcode:
    unit SendMail.Impl.IndySendMail;

    interface

    uses
      SendMail,

      System.Classes,
      System.Generics.Collections,
      System.SysUtils,

      IdBaseComponent,
      IdComponent,

      IdIOHandler,
      IdIOHandlerSocket,
      IdIOHandlerStack,

      IdSSL,
      IdSSLOpenSSL,
      IdExplicitTLSClientServerBase,

      IdUserPassProvider,

      IdSASL,
      IdSASL_CRAMBase,
      IdSASL_CRAM_MD5,
      IdSASL_CRAM_SHA1,
      IdSASLAnonymous,
      IdSASLDigest,
      IdSASLExternal,
      IdSASLLogin,
      IdSASLOTP,
      IdSASLPlain,
      IdSASLSKey,
      IdSASLUserPass,

      IdSMTP,
      IdSMTPBase,

      IdTCPConnection,
      IdTCPClient,
      IdMessageClient,
      IdMessage;

    type
      TIndySendMail = class( TInterfacedObject, ISendMail )
      private
        FSMTP                    : TIdSMTP;
        FSASLAnonymous           : TIdSASLAnonymous;
        FSASLCRAMMD5              : TIdSASLCRAMMD5;
        FSASLCRAMSHA1             : TIdSASLCRAMSHA1;
        FSASLDigest              : TIdSASLDigest;
        FSASLExternal            : TIdSASLExternal;
        FSASLLogin               : TIdSASLLogin;
        FSASLOTP                 : TIdSASLOTP;
        FSASLPlain               : TIdSASLPlain;
        FSASLSKey                : TIdSASLSKey;
        FUserPassProvider        : TIdUserPassProvider;
        FSSLIOHandlerSocketOpenSSL: TIdSSLIOHandlerSocketOpenSSL;
        FMessage                 : TIdMessage;
      private
        FSync: TObject;
        procedure Configure( Configuration: TSendMailConfiguration );
      private { ISendMail }
        function GetSupportedAuthTypes: TSmtpAuthTypes;
        procedure Send( Configuration: TSendMailConfiguration; Mail: TMailContainer );
      public
        constructor Create;
        destructor Destroy; override;
      end;

    implementation

    uses
      netAlike.Utils;

    { TIndySendMail }

    procedure TIndySendMail.Configure( Configuration: TSendMailConfiguration );
    begin
      if not( Configuration.AuthType in GetSupportedAuthTypes )
      then
        raise ENotSupportedException.Create( 'AuthType not supported' );

      FSMTP.SASLMechanisms.Clear;

      FSMTP.AuthType := satSASL;
      FSMTP.UseEhlo := True;

      FUserPassProvider.Username := Configuration.Username;
      FUserPassProvider.Password := Configuration.Password;

      FSASLExternal.AuthorizationIdentity := Configuration.TLSCertifcateId;

      case Configuration.AuthType of
        TSmtpAuthType.None:
          FSMTP.SASLMechanisms.Add.SASL := FSASLAnonymous;
        TSmtpAuthType.Extern:
          FSMTP.SASLMechanisms.Add.SASL := FSASLExternal;
        TSmtpAuthType.MD5CR:
          FSMTP.SASLMechanisms.Add.SASL := FSASLCRAMMD5;
        TSmtpAuthType.SHA1CR:
          FSMTP.SASLMechanisms.Add.SASL := FSASLCRAMSHA1;
        TSmtpAuthType.Password:
          FSMTP.SASLMechanisms.Add.SASL := FSASLLogin;
      else
        if Configuration.AuthType in GetSupportedAuthTypes
        then
          raise ENotImplemented.Create( 'AuthType not implemented' );
      end;

      if Configuration.UseSSL
      then
        begin
          FSMTP.IOHandler := FSSLIOHandlerSocketOpenSSL;
          if Configuration.UseStartTLS
          then
            begin
              FSMTP.UseTLS := utUseExplicitTLS;
            end
          else
            begin
              FSMTP.UseTLS := utUseImplicitTLS;
            end;
        end
      else
        begin
          FSMTP.IOHandler := nil;
          FSMTP.UseTLS   := utNoTLSSupport;
        end;

      FSMTP.Host := Configuration.Host;
      FSMTP.Port := Configuration.Port;
    end;

    constructor TIndySendMail.Create;
    begin
      FSync := TObject.Create;
      inherited;

      FSMTP                     := TIdSMTP.Create( nil );
      FSSLIOHandlerSocketOpenSSL := TIdSSLIOHandlerSocketOpenSSL.Create( FSMTP );
      FUserPassProvider         := TIdUserPassProvider.Create( FSMTP );
      FSASLExternal             := TIdSASLExternal.Create( FSMTP );
      FSASLCRAMMD5               := TIdSASLCRAMMD5.Create( FSMTP );
      FSASLCRAMSHA1              := TIdSASLCRAMSHA1.Create( FSMTP );
      FSASLDigest               := TIdSASLDigest.Create( FSMTP );
      FSASLOTP                  := TIdSASLOTP.Create( FSMTP );
      FSASLSKey                 := TIdSASLSKey.Create( FSMTP );
      FSASLLogin                := TIdSASLLogin.Create( FSMTP );
      FSASLPlain                := TIdSASLPlain.Create( FSMTP );
      FSASLAnonymous            := TIdSASLAnonymous.Create( FSMTP );
      FMessage                  := TIdMessage.Create( FSMTP );

      TEnumeratorUtil.ForEach<TIdSASLUserPass>( FSMTP,
        procedure( const c: TIdSASLUserPass )
        begin
          c.UserPassProvider := FUserPassProvider;
        end,
        function( const c: TIdSASLUserPass ): Boolean
        begin
          Result := ( c.UserPassProvider = nil );
        end );
    end;

    destructor TIndySendMail.Destroy;
    begin
      FreeAndNil( FSMTP );
      inherited;
      FreeAndNil( FSync );
    end;

    function TIndySendMail.GetSupportedAuthTypes: TSmtpAuthTypes;
    begin
      Result := [
      {} TSmtpAuthType.None,
      {} TSmtpAuthType.Extern,
      {} TSmtpAuthType.MD5CR,
      {} TSmtpAuthType.SHA1CR,
      {} TSmtpAuthType.Password ];
    end;

    procedure TIndySendMail.Send( Configuration: TSendMailConfiguration; Mail: TMailContainer );
    begin
      TMonitor.Enter( FSync );
      try

        Configure( Configuration );

        FMessage.Clear;

        FMessage.From.Text          := Mail.Sender;
        FMessage.Recipients.Add.Text := Mail.Receiver;
        FMessage.Subject            := Mail.Subject;
        FMessage.Body.Text          := Mail.Body;

        FSMTP.Connect;
        try
          FSMTP.Send( FMessage );
        finally
          FSMTP.Disconnect( True );
        end;

      finally
        TMonitor.Exit( FSync );
      end;
    end;

    end.

MrSpock 13. Okt 2015 07:14

AW: Indy: Googlemail smtp Einstellungen
 
Danke. Welchen Port nutzt du für googlemail ? So wie ich as sehe unterstützt dein Code auch die verschiedenen Authentisierungen. Das könnte wohl der Grund sein, dass es bei meinem Code nicht mit dem Email Versand funktioniert. Muss ich heute Abend mal testen.

Sir Rufo 13. Okt 2015 09:51

AW: Indy: Googlemail smtp Einstellungen
 
Der Port wird in Abhängigkeit der Einstellungen gewählt:
Delphi-Quellcode:
TSendMailConfiguration = class
  private const
    DefaultPort = 25;
    DefaultImplicitTLSPort = 465;
    DefaultExplicitTLSPort = 587;
  ...
end;

procedure TSendMailConfiguration.SetDefaultPort;
begin
  if FUseSSL
  then
    begin
      if FUseStartTLS
      then
        FPort := DefaultExplicitTLSPort // 587
      else
        FPort := DefaultImplicitTLSPort // 465
    end
  else
    FPort := DefaultPort; // 25
end;

MrSpock 14. Okt 2015 19:09

AW: Indy: Googlemail smtp Einstellungen
 
was ist netAlike.Utils ?

Sir Rufo 14. Okt 2015 19:20

AW: Indy: Googlemail smtp Einstellungen
 
Zitat:

Zitat von MrSpock (Beitrag 1318717)
was ist netAlike.Utils ?

Da habe ich ein paar nützliche Sachen drin, die ich von .net portiert habe.

z.B.
Delphi-Quellcode:
// Alle Kinder der Komponente FSMTP die vom Typ TIdSASLUserPass sind
TEnumeratorUtil.ForEach<TIdSASLUserPass>( FSMTP,
    // setzte den PasswordProvider
    procedure( const c: TIdSASLUserPass )
    begin
      c.UserPassProvider := FUserPassProvider;
    end,
    // wenn der noch nicht gesetzt wurde
    function( const c: TIdSASLUserPass ): Boolean
    begin
      Result := ( c.UserPassProvider = nil );
    end );

MrSpock 14. Okt 2015 19:22

AW: Indy: Googlemail smtp Einstellungen
 
Ja genau, dieser Codeteil lässt sich bei mir nicht übersetzen. :stupid:

OK, habe jetzt den UserPassProvider manuell hinzugefügt.

Dann meine Daten eingetragen.

Funktioniert leider nicht :-(

Connection Closed Gracefully

Sir Rufo 14. Okt 2015 19:38

AW: Indy: Googlemail smtp Einstellungen
 
Zitat:

Zitat von MrSpock (Beitrag 1318722)
Funktioniert leider nicht :-(

Connection Closed Gracefully

Das ist kein Kriterium, ob es funktioniert oder nicht, denn:
http://www.indyproject.org/kb/index....ngeidconnc.htm

MrSpock 14. Okt 2015 20:40

AW: Indy: Googlemail smtp Einstellungen
 
Bin mal im Einzelschritt durch.

Der Fehler kommt bei:

TIdSSLIOHandlerSocketOpenSSL.OpenEncodedConnection

Booah, ich krieg hier gleich 'n Fön. :-(

AOL klappt, mein Server klappt, f****ing googlemail klappt einfach nicht. Ich hab da jetzt schon 8 Stunden investiert :-(

Sir Rufo 15. Okt 2015 00:15

AW: Indy: Googlemail smtp Einstellungen
 
Liste der Anhänge anzeigen (Anzahl: 2)
Im Anhang Source (ohne meine Libs) und eine EXE.

Genau mit der EXE kann ich mails von meinem GMail-Account versenden.
(Delphi 10 Seattle, Indy 10, OpenSSL 1.0.2d)

Denn entweder sind deine GMail-Einstellungen nicht korrekt, oder OpenSSL falsch, oder oder oder ...


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

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