我在用 IdSMTP 发送邮件的时候出现了 Error: send HELO/EHLO first 错误
程序很简单,代码在下面。出现这个错误是怎么回事呢?应该怎么解决?请大家帮帮忙看看代码。另外,大家能不能贴一下测试成功的发送邮件的简单代码呢?
还有,为了方便大家调试代码,我专门申请了一个126的信箱,用户名和密码都在代码中。
最终我也是用126的SMTP发送邮件的,因为朋友想让我写个发送邮件的程序,用来把他家里经常开着的机器的一些情况及时发送到邮箱中。所以我写这个绝对不是发送垃圾邮件的。unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdMessage, IdBaseComponent, IdComponent, IdTCPConnection,
IdTCPClient, IdMessageClient, IdSMTP, StdCtrls, IdAntiFreezeBase,
IdAntiFreeze;type
TForm1 = class(TForm)
Button1: TButton;
IdSMTP1: TIdSMTP;
IdMessage1: TIdMessage;
IdAntiFreeze1: TIdAntiFreeze;
procedure Button1Click(Sender: TObject);
procedure IdSMTP1WorkEnd(Sender: TObject; AWorkMode: TWorkMode);
procedure IdSMTP1Connected(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
IdMessage1.From.Address := '[email protected]';
IdMessage1.From.Name := '测试用';
IdMessage1.Recipients.EMailAddresses := '[email protected]';
IdMessage1.Subject := '测试用';
IdMessage1.Body.Add('测试一下');
IdSMTP1.Host := 'smtp.126.com';
IdSMTP1.Username := 'lknasdfo';
IdSMTP1.Password := '111111';
IdSMTP1.AuthenticationType := atLogin;
IdSMTP1.Connect(10000);
end;procedure TForm1.IdSMTP1WorkEnd(Sender: TObject; AWorkMode: TWorkMode);
begin
IdSMTP1.Disconnect;
end;procedure TForm1.IdSMTP1Connected(Sender: TObject);
begin
IdSMTP1.Send(IdMessage1);
end;end.
程序很简单,代码在下面。出现这个错误是怎么回事呢?应该怎么解决?请大家帮帮忙看看代码。另外,大家能不能贴一下测试成功的发送邮件的简单代码呢?
还有,为了方便大家调试代码,我专门申请了一个126的信箱,用户名和密码都在代码中。
最终我也是用126的SMTP发送邮件的,因为朋友想让我写个发送邮件的程序,用来把他家里经常开着的机器的一些情况及时发送到邮箱中。所以我写这个绝对不是发送垃圾邮件的。unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdMessage, IdBaseComponent, IdComponent, IdTCPConnection,
IdTCPClient, IdMessageClient, IdSMTP, StdCtrls, IdAntiFreezeBase,
IdAntiFreeze;type
TForm1 = class(TForm)
Button1: TButton;
IdSMTP1: TIdSMTP;
IdMessage1: TIdMessage;
IdAntiFreeze1: TIdAntiFreeze;
procedure Button1Click(Sender: TObject);
procedure IdSMTP1WorkEnd(Sender: TObject; AWorkMode: TWorkMode);
procedure IdSMTP1Connected(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
IdMessage1.From.Address := '[email protected]';
IdMessage1.From.Name := '测试用';
IdMessage1.Recipients.EMailAddresses := '[email protected]';
IdMessage1.Subject := '测试用';
IdMessage1.Body.Add('测试一下');
IdSMTP1.Host := 'smtp.126.com';
IdSMTP1.Username := 'lknasdfo';
IdSMTP1.Password := '111111';
IdSMTP1.AuthenticationType := atLogin;
IdSMTP1.Connect(10000);
end;procedure TForm1.IdSMTP1WorkEnd(Sender: TObject; AWorkMode: TWorkMode);
begin
IdSMTP1.Disconnect;
end;procedure TForm1.IdSMTP1Connected(Sender: TObject);
begin
IdSMTP1.Send(IdMessage1);
end;end.
解决方案 »
- 如何获取FTP上文件夹包括子文件夹内文件信息
- ●●●【哪里有《魔兽争霸III冰封王座1.18》英文版下载?】●●●
- update 更新数据后,如何返回知道更新的记录数
- 关于存储过程,救命啊!插入数据慢!在线等!
- 如何模拟鼠标左键点击动作
- 连接到TServerSocket上的Socket的RemoteHost是否需要在客户端设置?
- 求助~
- 一个关于[locate]的问题
- 高兴散分,我加星了,变成二颗星了!(发财,周星星)
- 怎样用Delphi 7 把Delphi完整源代码做成软件?
- 如何写控件编辑器(应该叫这个名字吧)
- ADOConnection+ADOTable+Datasource+DBGrid 問題
procedure TForm11.BtnSendMailClick(Sender: TObject);
begin
IdSMTP1.AuthenticationType:=atLogin;
IdSMTP1.Host:=MailServerCombo.Text;
IdSMTP1.Port:=25;
IdMessage1.From.Address:=MailFromEdit.Text;
IdSMTP1.Username:=MailUsernameEdit.Text;
IdSMTP1.Password:=MailPasswordEdit.Text; IdMessage1.Recipients.EMailAddresses:=MailToEdit.Text; IdMessage1.Subject:=MailSubjectEdit.Text;
IdMessage1.Body.Text:=MailBodyMemo.Text; if FileExists(MailAttachmentEdit.Text) then
TIdAttachment.Create(IdMessage1.MessageParts,MailAttachmentEdit.Text);
try
try
BtnSendMail.Enabled:=false;
IdSMTP1.Connect(1000);
IdSMTP1.Send(IdMessage1);
Application.MessageBox('您的邮件已经成功发送到目标邮箱!','成功',mb_ok+mb_iconinformation);
except on E:Exception do
StatusBar1.Panels[0].Text:=(E.Message);
end;
finally
if IdSMTP1.Connected then
IdSMTP1.Disconnect;
BtnSendMail.Enabled:=True;
end;
end;
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, IdMessage, IdBaseComponent, IdComponent,
IdTCPConnection, IdTCPClient, IdMessageClient, IdSMTP;type
TForm11 = class(TForm)
BtnSendMail: TButton;
IdSMTP1: TIdSMTP;
IdMessage1: TIdMessage;
procedure BtnSendMailClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form11: TForm11;implementation{$R *.dfm}procedure TForm11.BtnSendMailClick(Sender: TObject);
begin
IdSMTP1.AuthenticationType:=atLogin;
IdSMTP1.Host:='smtp.126.com';
IdSMTP1.Port:=25;
IdMessage1.From.Address:='[email protected]';
IdSMTP1.Username:='lknasdfo';
IdSMTP1.Password:='111111'; IdMessage1.Recipients.EMailAddresses:='[email protected]'; IdMessage1.Subject:='aaaaa';
IdMessage1.Body.Text:='bb'; //if FileExists(MailAttachmentEdit.Text) then
// TIdAttachment.Create(IdMessage1.MessageParts,MailAttachmentEdit.Text);
try
try
BtnSendMail.Enabled:=false;
IdSMTP1.Connect(1000);
IdSMTP1.Send(IdMessage1);
Application.MessageBox('您的邮件已经成功发送到目标邮箱!','成功',mb_ok+mb_iconinformation);
except on E:Exception do
Showmessage(E.Message);
end;
finally
if IdSMTP1.Connected then
IdSMTP1.Disconnect;
BtnSendMail.Enabled:=True;
end;end;end.
这是我按照楼上的改的,主要是把用文本框赋值的内容改成了直接用字符串赋值。可是也是出错,提示 bad sequence of commands
因为现在的smtp 服务器都是要验证用户身份的。而以前的代码没有这个过程。看一下 ESMTP 的协议。先用 EHLO
再用 base64 编码后的 username
再发 base64 编码后的 password
再是后来的。
最好自己到http://www.nevrona.com/indy下载个indy9 DEMOS运行看看。我试过了是可以的,不过我的SMTP SERVER是自己安装的CMAILSERVR,也是支持ESMTP的
编译里面的 MailClient ,并且写好了smtp所需的内容,配置文件如下
[Pop3]
ServerName=pop3.server.com
ServerPort=110
ServerUser=your_login
ServerPassword=your_password
[Smtp]
ServerName=smtp.126.com
ServerPort=25
ServerUser=lknasdfo
ServerPassword=111111
SMTPAuthenticationType=1
[Email]
[email protected]
可还是 bad sequence of commands ,是不是我的电脑问题?哪位热心朋友试试那个例子?
ServerName=pop3.126.com
ServerPort=110
ServerUser=lknasdfo
ServerPassword=111111
[Smtp]
ServerName=smtp.126.com
ServerPort=25
ServerUser=lknasdfo
ServerPassword=111111
SMTPAuthenticationType=1
[Email]
[email protected]
这样还是出错。不过我申请了新浪的,发送邮件没有任何提示,但是目标邮箱没有收到邮件。
if IdSMTP1.Connected then IdSMTP1.Send(IdMessage1);
IdSMTP1.Connect(10000);
if IdSMTP1.Connected = True then
IdSMTP1.Send(IdMessage1);看来是163的信箱对于发送比较严格,我的代码可能没有按照它的要求来做。再等等看,实在不行,先结了贴吧。。
应该是163服务器连接后要求先发送HELO或者/EHLO握手才能发送别的命令,本来这是个可选的握手信号(注意:在RFC821中,HELO是一个可选择性命令,如果服务器不要求该命令的话,你可以把它忽略掉。),可能是INDY的控件把这个省了,你自己可以在连接后发送这个命令应该就可以了。
NMSMTP使用Transaction方法,INDY的传送数据方法你自己查查吧。
请参考
http://www.cnpaf.net/Class/SMTP/0532918532645032.html
http://www.cnpaf.net/Class/SMTP/0532918532611370.html