清理了许多项目,如果您的表单实际上没有显示,这些都无济于事。
下面使用需要身份验证并在端口 25 上运行的 SMTP 服务器从第一代 Nexus 7 成功发送了一条消息。
如果您真的要连接到 Google 服务器上的端口 465,则必须使用此代码未解决的加密。
IdMessage1.From.Text := 'blah blah'; 的简单赋值 没有工作,所以你会看到我用 IdMessage1.From.Address 替换了其他 IdMessage1 属性的类似属性。
unit Unit1;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
IdExplicitTLSClientServerBase, IdMessageClient, IdSMTPBase, IdSMTP, IdMessage,
FMX.StdCtrls, FMX.Edit;
type
Ttestform = class(TForm)
IdSMTP1: TIdSMTP;
IdMessage1: TIdMessage;
btnMessageSend: TButton;
edtUsername: TEdit;
edtPassword: TEdit;
Label1: TLabel;
Label2: TLabel;
edtFrom: TEdit;
Label3: TLabel;
edtTo: TEdit;
Label4: TLabel;
edtServer: TEdit;
Label5: TLabel;
edtPort: TEdit;
Label6: TLabel;
procedure btnMessageSendClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
testform: Ttestform;
implementation
{$R *.fmx}
procedure Ttestform.btnMessageSendClick(Sender: TObject);
var
intPort: Integer;
begin
ShowMessage('In button click');
IdSMTP1.Heloname:='test';
IdSMTP1.Host:=edtServer.Text;
IdSMTP1.Username:=edtUsername.Text;
intPort := 25;
ShowMessage('Before strtoint');
TryStrToInt(edtPort.Text, intPort);
ShowMessage('After strtoint');
IdSMTP1.Port:= intPort;
IdSMTP1.Password:=edtPassword.Text;
if IdSMTP1.Connected=True then IdSMTP1.Disconnect;
IdMessage1.Clear;
IdMessage1.From.Address := edtFrom.Text;
IdMessage1.Recipients.Add;
IdMessage1.Recipients.Items[0].Address := edtTo.Text;
IdMessage1.Priority := mpHigh;
IdMessage1.Sender.Address:=edtFrom.Text;
IdMessage1.Subject := 'Subject';
IdMessage1.Body.Add('test');
IdSMTP1.Connect;
IdSMTP1.Send(IdMessage1);
end;
end.