Delphi Indy IdSMTP1.Send适用于Nexus(4.3),但不适用于Galaxy S3(4.1)设备,但Unknow命令异常

时间:2014-02-21 17:32:12

标签: delphi smtp gmail indy

我正在使用下面的gmail服务器(Delphi XE5和Indy,SMTP)代码从应用内发送电子邮件。如果应用程序在Nexus 7(或Windows中的模拟器)上运行,则一切正常。但是在Galaxy S2上它引发了一个异常“无法识别的命令ix5msxxx.36 -gsmtp'

我在做错了什么。我找不到这种不规则行为的答案而没有运气。

try  //setup mail message
 IdMessage1                             := TIdMessage.Create(NIL);
 IdMessage1.From.Address                := 'XXX@gmail.com';  // change to thier emia
 IdMessage1.Recipients.EMailAddresses   := 'yyy@gmail.com';
 IdMessage1.Subject                     := 'hello';
 IdMessage1.Body.Text                   := 'hello again';
 if FileExists(datafilename) then
    IdAttachmentFile :=  TIdAttachmentFile.Create(IdMessage1.MessageParts, 
                         datafilename);
 except Showmessage('Could not create message, please try later');
 end; // of try to create an email

try
  IdSSLIOHandlerSocketOpenSSL1  :=       TIdSSLIOHandlersocketopenSSL.Create(NIL);
  IdSSLIOHandlerSocketOpenSSL1.Destination            := 'smtp.gmail.com:587';
  IdSSLIOHandlerSocketOpenSSL1.Host                   := 'smtp.gmail.com';
  IdSSLIOHandlerSocketOpenSSL1.Port                   := 587;
  IdSSLIOHandlerSocketOpenSSL1.SSLOptions.Method      := sslvTLSv1;
  IdSSLIOHandlerSocketOpenSSL1.SSLOptions.Mode        := sslmUnassigned;
  IdSSLIOHandlerSocketOpenSSL1.SSLOptions.VerifyMode  := [];
  IdSSLIOHandlerSocketOpenSSL1.SSLOptions.VerifyDepth := 0;                    
except Showmessage('Could createe SSL handle ');
end; // of try ssl

try
//setup SMTP
  IdSMTP1           := TIdSMTP.Create(NIL);
I dSMTP1.Host      := 'smtp.gmail.com';
  IdSMTP1.Port      :=  587; 
  IdSMTP1.IOHandler := IdSSLIOHandlerSocketOpenSSL1;
  IdSMTP1.UseTLS    := utUseExplicitTLS;
  IdSMTP1.Username  := 'xxx@gmail.com';
  IdSMTP1.password  := 'password';
except Showmessage('Could not send secure email connection, please try later');
end; // of try

try
  try
     IdSMTP1.Connect;// (1000) ;
     IdSMTP1.Send(IdMessage1) ;
  except on E:Exception do 
     showmessage('ERROR: ' + E.Message) ;
   end;
finally
  if IdSMTP1.Connected then IdSMTP1.Disconnect;
  IdSMTP1.Free ;
  IdSSLIOHandlerSocketOpenSSL1.Free;
  IdMessage1.Free
end;

1 个答案:

答案 0 :(得分:1)

我严重怀疑TIdSMTP正在发送ix5msxxx.36 -gsmtp命令。它看起来更像是一个可能的数据损坏问题,就像SSL层没有发送正确的数据一样。您可以将TIdLog...组件(例如TIdLogEvent)附加到TIdSMTP.Intercept属性,以验证TIdSMTP实际上是否正在发送有效的SMTP命令并接收有效的SMTP回复。

除此之外,我认为您的代码没有任何错误可能导致此类问题。不过,IT可以使用一些清理工作:

try
  IdMessage1 := nil;
  IdSSLIOHandlerSocketOpenSSL1 := nil;
  IdSMTP1 := nil;
  try
    //setup mail message
    try
      IdMessage1                             := TIdMessage.Create(nil);
      IdMessage1.From.Address                := 'XXX@gmail.com';  // change to their email
      IdMessage1.Recipients.EMailAddresses   := 'yyy@gmail.com';
      IdMessage1.Subject                     := 'hello';
      IdMessage1.Body.Text                   := 'hello again';
      if FileExists(datafilename) then
        IdAttachmentFile := TIdAttachmentFile.Create(IdMessage1.MessageParts, datafilename);
    except
      Exception.RaiseOuterException(Exception.Create('Could not create message, please try again later'));
    end;

    //setup TLS
    try
      IdSSLIOHandlerSocketOpenSSL1                        := TIdSSLIOHandlersocketopenSSL.Create(nil);
      IdSSLIOHandlerSocketOpenSSL1.SSLOptions.Method      := sslvTLSv1;
      IdSSLIOHandlerSocketOpenSSL1.SSLOptions.Mode        := sslmUnassigned;
      IdSSLIOHandlerSocketOpenSSL1.SSLOptions.VerifyMode  := [];
      IdSSLIOHandlerSocketOpenSSL1.SSLOptions.VerifyDepth := 0;                    
    except
      Exception.RaiseOuterException(Exception.Create('Could not create SSL handler, please try again later'));
    end; // of try ssl

    //setup SMTP
    try
      IdSMTP1           := TIdSMTP.Create(nil);
      IdSMTP1.IOHandler := IdSSLIOHandlerSocketOpenSSL1;
      IdSMTP1.UseTLS    := utUseExplicitTLS;
      IdSMTP1.Host      := 'smtp.gmail.com';
      IdSMTP1.Port      := 587; 
      IdSMTP1.Username  := 'xxx@gmail.com';
      IdSMTP1.password  := 'password';
    except
      Exception.RaiseOuterException(Exception.Create('Could not create SMTP handler, please try again later'));
    end; // of try

    try
      IdSMTP1.Connect;
      try
        IdSMTP1.Send(IdMessage1) ;
      finally
        IdSMTP1.Disconnect;
      end;
    except
      Exception.RaiseOuterException(Exception.Create('Could not send secure email, please try again later'));
    end;
  finally
    IdSMTP1.Free;
    IdSSLIOHandlerSocketOpenSSL1.Free;
    IdMessage1.Free;
  end;
except
  on E: Exception do
  begin
    if E.InnerException <> nil then
      ShowMessage('ERROR: ' + E.Message + #13#13 + E.InnerException.Message)
    else
      ShowMessage('ERROR: ' + E.Message);
  end;
end;