我在用 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.

解决方案 »

  1.   

    //这是我以前发Mail的代码,可以的,现在没测试.
    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;
      

  2.   

    unit Unit1;interfaceuses
      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
      

  3.   

    没测试你的,下面这个帖子中的测试通过,你参考一下http://community.csdn.net/Expert/topic/4740/4740216.xml?temp=.3819239
      

  4.   

    也是出现 bad sequence of commands 的错误
      

  5.   

    bad sequence of commands 错误的命令序列
    因为现在的smtp 服务器都是要验证用户身份的。而以前的代码没有这个过程。看一下 ESMTP 的协议。先用 EHLO
    再用 base64 编码后的 username
    再发 base64 编码后的 password
    再是后来的。
      

  6.   

    我用DELPHI7带的indy9,是支持支持ESMTP的(就是发送邮件需要身份验证,并且发件人的邮件地址也好验证的用户名一致)。
    最好自己到http://www.nevrona.com/indy下载个indy9 DEMOS运行看看。我试过了是可以的,不过我的SMTP SERVER是自己安装的CMAILSERVR,也是支持ESMTP的
      

  7.   

    晕了,我下载了  http://www.projectindy.org/DemoDownloads/Indy9Demos_26Oct04.zip
    编译里面的 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  ,是不是我的电脑问题?哪位热心朋友试试那个例子?
      

  8.   

    [Email]中的EMAIL地址选项应该都需要设置正确的,[Pop3]应该关系不大,最好也设置好试试。实在不行可以换个别的邮件服务器测试,或者自己到http://www.yzsoft.com/下载一个测试版安装一个CMAILSERVER测试
      

  9.   

    [Pop3]
    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]
    这样还是出错。不过我申请了新浪的,发送邮件没有任何提示,但是目标邮箱没有收到邮件。
      

  10.   

    不要把发信事件放在连接事件中,应该在连接事件执行完后才发送,比如放个消息过程,当连接成功后抛出消息发信。或者在你的第一段代码IdSMTP1.Connect(10000);后加句
    if IdSMTP1.Connected then IdSMTP1.Send(IdMessage1);
      

  11.   

    楼上的,还是不行。
      IdSMTP1.Connect(10000);
      if IdSMTP1.Connected = True then
        IdSMTP1.Send(IdMessage1);看来是163的信箱对于发送比较严格,我的代码可能没有按照它的要求来做。再等等看,实在不行,先结了贴吧。。
      

  12.   

    关于命令顺序不对的问题:
    应该是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
      

  13.   

    兄弟,俺告诉你吧!!这事情我弄了一个月,终于明白了,很简单INDY9根本就有问题,你下一下INDY10,马上解决,还有一个地方,就是不要CONTTYPE那加什么TEXT/HTML,要不你的附件肯定是一片乱码