我正在使用Delphi编写一个Android程序来发送包含数据的电子邮件。因此,我的应用程序存在连接问题。
我有
将主机设置为smtp.gmail.com,
输入我的Gmail帐户的用户名和密码,
将有效信息输入TIdMessage的地址和正文字段,
制作并添加了附件,
根据在线示例设置TIdSSLIOHandlerSocketOpenSSL的SSL选项,
添加了所有SASL Mechanisms Indy产品。
我正在使用端口587并已明确连接到TLS。
type
TForm1 = class(TForm)
SendBtn: TButton;
IdSMTP1: TIdSMTP;
IdMessage1: TIdMessage;
IdSASLAnonymous1: TIdSASLAnonymous;
IdSASLCRAMMD51: TIdSASLCRAMMD5;
IdSASLCRAMSHA11: TIdSASLCRAMSHA1;
IdSASLDigest1: TIdSASLDigest;
IdSASLExternal1: TIdSASLExternal;
IdSASLLogin1: TIdSASLLogin;
IdSASLOTP1: TIdSASLOTP;
IdSASLOTP2: TIdSASLOTP;
IdSASLPlain1: TIdSASLPlain;
IdSASLSKey1: TIdSASLSKey;
IdSSLIOHandlerSocketOpenSSL1: TIdSSLIOHandlerSocketOpenSSL;
procedure SendBtnClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
Attachment : TIdAttachmentFile;
implementation
{$R *.fmx}
procedure TForm1.FormCreate(Sender: TObject);
begin
IdSMTP1.IOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(idSMTP1);
IdSMTP1.UseTLS := utUseExplicitTLS;
IdSMTP1.AuthType := satSASL;
IdSMTP1.SASLMechanisms.Add.SASL := IdSASLCRAMSHA11;
IdSMTP1.SASLMechanisms.Add.SASL := IdSASLAnonymous1;
IdSMTP1.SASLMechanisms.Add.SASL := IdSASLCRAMMD51;
IdSMTP1.SASLMechanisms.Add.SASL := IdSASLDigest1;
IdSMTP1.SASLMechanisms.Add.SASL := IdSASLExternal1;
IdSMTP1.SASLMechanisms.Add.SASL := IdSASLLogin1;
IdSMTP1.SASLMechanisms.Add.SASL := IdSASLOTP1;
IdSMTP1.SASLMechanisms.Add.SASL := IdSASLOTP2;
IdSMTP1.SASLMechanisms.Add.SASL := IdSASLPlain1;
IdSMTP1.SASLMechanisms.Add.SASL := IdSASLSKey1;
IdSSLIOHandlerSocketOpenSSL1.SSLOptions.Method := sslvTLSv1;
IdSSLIOHandlerSocketOpenSSL1.SSLOptions.Mode := sslmUnassigned;
IdSSLIOHandlerSocketOpenSSL1.SSLOptions.VerifyMode := [];
IdSSLIOHandlerSocketOpenSSL1.SSLOptions.VerifyDepth := 0;
end;
procedure TForm1.SendBtnClick(Sender: TObject);
begin
if IdSMTP1.Connected=True then IdSMTP1.Disconnect;
IdMessage1.From.Address := 'myemail@gmail.com';
IdMessage1.Recipients.EMailAddresses := 'other@gmail.com';
IdMessage1.BccList.Add.Address := '';
IdMessage1.CCList.Add.Address := '';
IdMessage1.Priority := mpHigh;
IdMessage1.Sender.Address := 'myemail@gmail.com';
IdMessage1.Subject := 'Test Data'; //Add Date/time
IdMessage1.Body.Add('Hello!');
Attachment := TIdAttachmentFile.Create(IdMessage1.MessageParts, (GethomePath+'/Test.txt'));
IdSMTP1.Connect;
IdSMTP1.Authenticate;
IdSMTP1.Send(IdMessage1);
IdSMTP1.Disconnect;
end;
失败于:
IdSMTP1.Connect;
以这种方式连接到Android是否存在已知问题?
答案 0 :(得分:1)
在这一行:
IdSMTP1.IOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(idSMTP1);
您正在创建并指定新的默认初始化SSLIOHandler
,而不是使用表单上的现有SSLIOHandler
。
该行应改为:
IdSMTP1.IOHandler := IdSSLIOHandlerSocketOpenSSL1;
事实上,您在FormCreate()
中所做的所有可以(并且应该)在设计时在表单设计器中完成。您不需要在代码中执行此操作。
另外,我在您的表单上看不到TIdUserPassProvider
。您使用的大多数SASL组件都需要一个用户名/密码。 TIdSMTP.UserName
和TIdSMTP.Password
属性仅在AuthType
为satDefault
而非satSASL
时使用。
除此之外,我建议在SendBtnClick()
中进行一些其他更改:
您应该致电IdMessage1.Clear()
,这样您就不会遗留以前发送的现有数据。您要将新值附加到IdMessage1.BccList
,IdMessage1.CCList
,IdMessage1.Body
和IdMessage1.MessageParts
,而不先清除旧值。
您无需致电Authenticate()
,Send()
会在内部为您做到这一点。
Send()
应位于try/finally
或try/except
块中,以便即使Disconnect()
失败也可以致电Send()
。
您没有正确配置TIdMessage
以便将文本和附件混合在一起。您应向TIdText
添加MessageParts
而非使用TIdMessage.Body
(但如果TIdMessage.ConvertPreamble
为真,则会转换为TIdMessage.Body
如果存在任何附件,请为您TIdText
。但无论哪种方式,您都需要将TIdMessage.ContentType
属性设置为'multipart/mixed'
,以便接收方知道有多个部分。
请改为尝试:
type
TForm1 = class(TForm)
SendBtn: TButton;
IdSMTP1: TIdSMTP;
IdMessage1: TIdMessage;
IdSASLAnonymous1: TIdSASLAnonymous;
IdSASLCRAMMD51: TIdSASLCRAMMD5;
IdSASLCRAMSHA11: TIdSASLCRAMSHA1;
IdSASLDigest1: TIdSASLDigest;
IdSASLExternal1: TIdSASLExternal;
IdSASLLogin1: TIdSASLLogin;
IdSASLOTP1: TIdSASLOTP;
IdSASLOTP2: TIdSASLOTP;
IdSASLPlain1: TIdSASLPlain;
IdSASLSKey1: TIdSASLSKey;
IdUserPassProvider1: TIdUserPassProvider;
IdSSLIOHandlerSocketOpenSSL1: TIdSSLIOHandlerSocketOpenSSL;
procedure SendBtnClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.fmx}
procedure TForm1.FormCreate(Sender: TObject);
begin
// all of this code can be handled at design-time instead!
IdSSLIOHandlerSocketOpenSSL1.SSLOptions.Method := sslvTLSv1;
IdSSLIOHandlerSocketOpenSSL1.SSLOptions.Mode := sslmUnassigned;
IdSSLIOHandlerSocketOpenSSL1.SSLOptions.VerifyMode := [];
IdSSLIOHandlerSocketOpenSSL1.SSLOptions.VerifyDepth := 0;
IdSMTP1.IOHandler := IdSSLIOHandlerSocketOpenSSL1;
IdSMTP1.UseTLS := utUseExplicitTLS;
IdSMTP1.AuthType := satSASL;
IdSASLCRAMSHA11.UserPassProvider := IdUserPassProvider1;
IdSASLCRAMMD51.UserPassProvider := IdUserPassProvider1;
IdSASLDigest1.UserPassProvider := IdUserPassProvider1;
IdSASLLogin1.UserPassProvider := IdUserPassProvider1;
IdSASLOTP1.UserPassProvider := IdUserPassProvider1;
IdSASLOTP2.UserPassProvider := IdUserPassProvider1;
IdSASLPlain1.UserPassProvider := IdUserPassProvider1;
IdSASLSKey1.UserPassProvider := IdUserPassProvider1;
IdSMTP1.SASLMechanisms.Add.SASL := IdSASLCRAMSHA11;
IdSMTP1.SASLMechanisms.Add.SASL := IdSASLAnonymous1;
IdSMTP1.SASLMechanisms.Add.SASL := IdSASLCRAMMD51;
IdSMTP1.SASLMechanisms.Add.SASL := IdSASLDigest1;
IdSMTP1.SASLMechanisms.Add.SASL := IdSASLExternal1;
IdSMTP1.SASLMechanisms.Add.SASL := IdSASLLogin1;
IdSMTP1.SASLMechanisms.Add.SASL := IdSASLOTP1;
IdSMTP1.SASLMechanisms.Add.SASL := IdSASLOTP2;
IdSMTP1.SASLMechanisms.Add.SASL := IdSASLPlain1;
IdSMTP1.SASLMechanisms.Add.SASL := IdSASLSKey1;
// end design-time capable hookups
IdSMTP1.Host := ...;
IdSMTP1.Port := ...;
IdUserPassProvider1.UserName := ...;
IdUserPassProvider1.Password := ...;
end;
procedure TForm1.SendBtnClick(Sender: TObject);
var
Text: TIdText;
Attachment : TIdAttachmentFile;
begin
if IdSMTP1.Connected then IdSMTP1.Disconnect;
IdMessage1.Clear;
IdMessage1.From.Address := 'myemail@gmail.com';
IdMessage1.Recipients.EMailAddresses := 'other@gmail.com';
IdMessage1.Priority := mpHigh;
IdMessage1.Sender.Address := 'myemail@gmail.com';
IdMessage1.Subject := 'Test Data'; //Add Date/time
//IdMessage1.Body.Add('Hello!');
Text := TIdText.Create(IdMessage1.MessageParts);
Text.ContentType := 'text/plain';
Text.Body.Add('Hello!');
Attachment := TIdAttachmentFile.Create(IdMessage1.MessageParts, (GethomePath+'/Test.txt'));
IdMessage1.ContextType := 'multipart/mixed';
IdSMTP1.Connect;
try
IdSMTP1.Send(IdMessage1);
finally
IdSMTP1.Disconnect;
end;
end;