Delphi Indy IdSMTP1.Send works on Nexus (4.3) but not Galaxy S3 (4.1) device with exception Unknow command

StackOverflow https://stackoverflow.com/questions/21941220

  •  14-10-2022
  •  | 
  •  

Question

I am sending an email from inside an app using gmail server (Delphi XE5 and Indy, SMTP) code below. All is fine if the app is running on a Nexus 7 (or the emulator in windows). But on Galaxy S2 it raises an exception "unrecognized command ix5msxxx.36 -gsmtp'

what am I doing wrong. I have searched for answers for this irregular behavior with not luck.

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;

Était-ce utile?

La solution

I seriously doubt that TIdSMTP is sending a ix5msxxx.36 -gsmtp command. It looks more like a possible data corruption issue, as if the SSL layer is not sending the right data. You can attach a TIdLog... component, such as TIdLogEvent, to the TIdSMTP.Intercept property to verify that TIdSMTP is actually sending valid SMTP commands and receiving valid SMTP replies.

Other than that, I don't see anything wrong with your code that could cause such a problem. IT could use some cleanup, though:

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;
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top