加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

delphi – 在Office365中使用INDY 10 SMTP

发布时间:2020-12-15 09:48:26 所属栏目:大数据 来源:网络整理
导读:我不熟悉INDY SMTP组件.我想用INDY和Office 365发送邮件.这是一个很好的主题,它帮助了我很多: What do the SMTP Indy component security and authentication properties do? 但我没想出如何使用SASL. Office365地址是smtp.office365.com,端口为587和TLS.所
我不熟悉INDY SMTP组件.我想用INDY和Office 365发送邮件.这是一个很好的主题,它帮助了我很多: What do the SMTP Indy component security and authentication properties do?
但我没想出如何使用SASL. Office365地址是smtp.office365.com,端口为587和TLS.所以我在表单中添加了一个SMTP和一个OpenSSL-IOHandler并设置了属性.但我没有工作,应用程序只是冻结.我需要知道如何在Office365中使用SASL.

谢谢.

解决方法

Office365仅支持TLS端口587上的LOGIN SASL.

当我尝试它时,以下代码对我来说很好(所有这些设置也可以在设计时设置):

>将TIdSMTP.AuthType属性设置为satDefault,它使用SMTP AUTH LOGIN命令:

var
  idSMTP1: TIdSMTP;
begin
  idSMTP1 := TIdSMTP.Create(nil);
  try
    idSMTP1.IOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(idSMTP1);
    idSMTP1.UseTLS := utUseExplicitTLS;
    TIdSSLIOHandlerSocketOpenSSL(idSMTP1.IOHandler).SSLOptions.Method := sslvSSLv3;

    idSMTP1.Host := 'smtp.office365.com';
    idSMTP1.Port := 587;

    idSMTP1.AuthType := satDefault;
    idSMTP1.Username := ...;
    idSMTP1.Password := ...;

    try
      idSMTP1.Connect;
      try
        idSMTP1.Authenticate;
      finally
        idSMTP1.Disconnect;
      end;
      ShowMessage('OK');
    except
      on E: Exception do
      begin
        ShowMessage(Format('Failed!'#13'[%s] %s',[E.ClassName,E.Message]));
        raise;
      end;
    end;
  finally
    idSMTP1.Free;
  end;

>将TIdSMTP.AuthType属性设置为satSASL并使用TIdSASLLogin,它使用相同的SMTP AUTH LOGIN命令:

var
  idSMTP1: TIdSMTP;
  idSASLLogin: TIdSASLLogin;
  idUserPassProvider: TIdUserPassProvider;
begin
  idSMTP1 := TIdSMTP.Create(nil);
  try
    idSMTP1.IOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(idSMTP1);
    idSMTP1.UseTLS := utUseExplicitTLS;
    TIdSSLIOHandlerSocketOpenSSL(idSMTP1.IOHandler).SSLOptions.Method := sslvSSLv3;

    idSMTP1.Host := 'smtp.office365.com';
    idSMTP1.Port := 587;

    idSASLLogin := TIdSASLLogin.Create(idSMTP1);
    idUserPassProvider := TIdUserPassProvider.Create(idSASLLogin);

    idSASLLogin.UserPassProvider := idUserPassProvider;
    idUserPassProvider.Username := ...;
    idUserPassProvider.Password := ...;

    idSMTP1.AuthType := satSASL;
    idSMTP1.SASLMechanisms.Add.SASL := idSASLLogin;

    try
      idSMTP1.Connect;
      try
        idSMTP1.Authenticate;
      finally
        idSMTP1.Disconnect;
      end;
      ShowMessage('OK');
    except
      on E: Exception do
      begin
        ShowMessage(Format('Failed!'#13'[%s] %s',E.Message]));
        raise;
      end;
    end;
  finally
    idSMTP1.Free;
  end;

更新:Office365不再支持SSL v3,您必须立即使用TLS v1.x:

(idSMTP1.IOHandler).SSLOptions.Method := sslvTLSv1;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读