[英]Delphi Indy IdSMTP1.Send works on Nexus (4.3) but not Galaxy S3 (4.1) device with exception Unknow command
我正在使用以下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;
我嚴重懷疑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;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.