0

此处的上一个问题中,我了解了如何使用 INDY SMTP (TIdSMTP) 通过 Office 365 帐户发送邮件。我也为许多其他人找到了它,它适用于几乎所有常见的电子邮件提供商。但我不知道如何将它与我的本地 Exchange Server 一起使用。前段时间有 indy 附带的 SASL-NTLM 组件,但似乎它们已被删除。我需要 NTLM 连接到本地 Exchange Server。但如果没有 NTLM,我无法弄清楚如何做到这一点。

4

1 回答 1

1

我最近也在与 Indy 和我的 Exchange 服务器作斗争。SaslNtlm 组件不在 Delphi XE5 附带的 Indy10 版本中。它甚至不在 Indy Protocols 文件夹的源文件中。

幸运的是,使用 SMTP 客户端进行 NTLM 身份验证所需的许多东西都是可用的。有一个名为 IdAuthenticationSSPI 的单元实现了整个 NTLM 交换。剩下要做的就是从 TIdSASL 实现一个自定义后代,它与 TIndySSPINTLMClient 对象交互。

  TSaslNtlm = class(TIdSASL)
  public
    constructor Create(AOwner: TComponent);
    destructor Destroy; override;

    function StartAuthenticate(const AChallenge, AHost, AProtocolName : string): string; override;
    function ContinueAuthenticate(const ALastResponse, AHost, AProtocolName : string): string; override;
    function IsReadyToStart: Boolean; override;

    class function ServiceName: TIdSASLServiceName; override;

  private
    FSSPIClient: TIndySSPINTLMClient;
  end;

该类的实现如下:

constructor TSaslNtlm.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FSSPIClient := TIndySSPINTLMClient.Create;
end;

destructor TSaslNtlm.Destroy;
begin
  FSSPIClient.Free;
  inherited;
end;

function TSaslNtlm.StartAuthenticate(const AChallenge, AHost,
                                     AProtocolName: string): string;
begin
  FSSPIClient.SetCredentials(AHost, '', '');
  Result := BytesToStringRaw(FSSPIClient.InitAndBuildType1Message);
end;

function TSaslNtlm.ContinueAuthenticate(const ALastResponse, AHost,
                                        AProtocolName: string): string;
var LastMsg: TIdBytes;
begin
  LastMsg := ToBytes(ALastResponse, Indy8BitEncoding
                     {$IFDEF STRING_IS_ANSI}, Indy8BitEncoding{$ENDIF});
  Result := BytesToStringRaw(FSSPIClient.UpdateAndBuildType3Message(LastMsg));
end;

function TSaslNtlm.IsReadyToStart: Boolean;
begin
  Result := True;
end;

class function TSaslNtlm.ServiceName: TIdSASLServiceName;
begin
  Result := 'NTLM';
end;

然后只需将这种 SASL 机制添加到 SMTP 客户端即可:

smtp.AuthType := satSASL;
ntml := TSaslNtlm.Create(smtp);
with smtp.SASLMechanisms.Add do begin
  DisplayName := ntlm.ServiceName;
  SASL := ntlm;
end;
于 2017-07-10T08:34:07.323 回答