使用INDY 10 SMTP与Office365 [英] Using INDY 10 SMTP with Office365

查看:1087
本文介绍了使用INDY 10 SMTP与Office365的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不熟悉INDY SMTP组件。我想用INDY和Office 365发送邮件。这是一个很好的主题,帮助我很多:
但是我没有想到如何使用SASL。 Office365地址是smtp.office365.com,端口587和TLS。所以我添加了一个SMTP和一个OpenSSL-IOHandler到我的窗体并设置了属性。但是我没有工作,应用程序刚刚冻结。我需要知道如何使用SASL与Office365。



谢谢。

解决方案

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



以下代码对我有用我刚刚尝试(所有这些设置也可以在设计时设置):


  1. 设置 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;
    结束
    ShowMessage('OK');
    除了
    对于E:异常do
    begin
    ShowMessage(Format('Failed!'#13'[%s]%s',[E.ClassName,E.Message ]));
    加注
    结束
    结束
    finally
    idSMTP1.Free;
    结束


  2. 设置 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;
    结束
    ShowMessage('OK');
    除了
    对于E:异常do
    begin
    ShowMessage(Format('Failed!'#13'[%s]%s',[E.ClassName,E.Message ]));
    加注
    结束
    结束
    finally
    idSMTP1.Free;
    结束


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

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


I'm not familar with the INDY SMTP component. I want to send a mail with INDY and Office 365. Here is a nice topic which helped me a lot: What do the SMTP Indy component security and authentication properties do? But I did not figured out how to use SASL. Office365 adress is smtp.office365.com with port 587 and TLS. So I added an SMTP and an OpenSSL-IOHandler to my form and setted the properties. But I didn't work, the app is just freezing. I need to know how to use SASL with Office365.

Thanks.

解决方案

Office365 only supports the LOGIN SASL on TLS port 587.

The following code works fine for me when I just tried it (all of these settings can also be set up at design-time as well):

  1. setting the TIdSMTP.AuthType property to satDefault, which uses the SMTP AUTH LOGIN command:

    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;
    

  2. setting the TIdSMTP.AuthType property to satSASL and using TIdSASLLogin, which uses the same SMTP AUTH LOGIN command:

    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.ClassName, E.Message]));
            raise;
          end;
        end;
      finally
        idSMTP1.Free;
      end;
    

Update: Office365 no longer supports SSL v3, you must use TLS v1.x now:

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

这篇关于使用INDY 10 SMTP与Office365的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆