我用 idsmtp+idmessage
网上类似代码为:
procedure TForm1.Button8Click(Sender: TObject);
begin  
  try
    IdSMTP1.AuthenticationType:=atLogin; //设置登陆类型
    IdSMTP1.Username:='okmnji79513'; //设置登陆帐号
    IdSMTP1.Password:='xxxxx'; //设置登陆密码
    IdSMTP1.Host:='mx.eYou.com'; //设置SMTP地址
    IdSMTP1.Port:=25;
//    IdSMTP1.Port:=strtoint(Edit4.Text); //设置端口  必须转化为整型
    IdSMTP1.Connect;  //开始连接服务器
  except
    Showmessage('连接失败,请重试!');
    Exit; //连接失败 的话 退出该执行过程
  end;  IdMessage1.Body.Clear;  //先清空上次发送的内容
  IdMessage1.Subject:='Test';  //设置邮件发送的标题
  IdMessage1.Body.Add('Test neirong');  //设置邮件发送的主体
  IdMessage1.From.Address:='[email protected]'; //设置邮件的发件人  也就是说该邮件来自什么地方
  IdMessage1.Recipients.EMailAddresses:='[email protected]';  //收件人的地址  try
    if IdSMTP1.Authenticate then
      showmessage('验证成功')
    else
      showmessage('验证失败');
  except
    showmessage('error : 验证失败');
  end;  try
    idSMTP1.Send(IdMessage1);
    Showmessage('邮件发送成功!');
  except
    Showmessage('邮件发送失败!');
  end;
end;
试了几个网站(163,eyou,sina)都不成功。
网上基本说法是 网站禁止使用indy控件发的邮件。不知道如何才能使邮件发送成功???系统:xp家庭版sp2 + Delphi6(indy升级为版本9)后来 在网上看到用 NMSMTP做,自己用Base64编码。代码如下(1楼)错误依旧(还是标题上的错误)求助啊求助

解决方案 »

  1.   

    unit   Unit1;   
        
      interface   
        
      uses   
          Windows,   Messages,   SysUtils,   Classes,   Graphics,   Controls,   Forms,   Dialogs,   
          StdCtrls,   Psock,   NMsmtp,   ComCtrls;   
        
      type   
          TForm1   =   class(TForm)
        NMSMTP1: TNMSMTP;
        Label1: TLabel;
        Button1: TButton;
              {NMSMTP1:   TNMSMTP;
              Button1:   TButton;
              Label1:   TLabel;  } 
              procedure   Button1Click(Sender:   TObject);   
              procedure   NMSMTP1Connect(Sender:   TObject);   
              procedure   NMSMTP1InvalidHost(var   Handled:   Boolean);   
              procedure   NMSMTP1ConnectionFailed(Sender:   TObject);   
              procedure   NMSMTP1Status(Sender:   TComponent;   Status:   String);   
              procedure   NMSMTP1SendStart(Sender:   TObject);   
              procedure   NMSMTP1Success(Sender:   TObject);
          private   
              {   Private   declarations   }   
          public   
              {   Public   declarations   }   
          end;
      //BaseTable为BASE64码表   
      const   BaseTable:string='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';   
        
      var   
          Form1:   TForm1;   
          AuthSucc:boolean;//   是否需要密码验证   
          function   DecodeBase64(Source:string):string;   //解码函数
          function   FindInTable(CSource:char):integer;     //   
          function   EncodeBase64(Source:string):string;   //编码函数
      implementation   
        
      {$R   *.DFM}   
      //   
      function   FindInTable(CSource:char):integer;   
      begin   
          result:=Pos(string(CSource),BaseTable)-1;   
      end;   
      ////   
      function   DecodeBase64(Source:string):string;   
      var   
          SrcLen,Times,i:integer;   
          x1,x2,x3,x4,xt:byte;   
      begin   
          result:='';   
          SrcLen:=Length(Source);   
          Times:=SrcLen   div   4;   
          for   i:=0   to   Times-1   do   
          begin   
              x1:=FindInTable(Source[1+i*4]);   
              x2:=FindInTable(Source[2+i*4]);   
              x3:=FindInTable(Source[3+i*4]);   
              x4:=FindInTable(Source[4+i*4]);   
              x1:=x1   shl   2;   
              xt:=x2   shr   4;   
              x1:=x1   or   xt;   
              x2:=x2   shl   4;   
              result:=result+chr(x1);   
              if   x3=   64   then   break;   
              xt:=x3   shr   2;   
              x2:=x2   or   xt;   
              x3:=x3   shl   6;   
              result:=result+chr(x2);   
              if   x4=64   then   break;   
              x3:=x3   or   x4;   
              result:=result+chr(x3);   
          end;   
      end;   
      /////   
      function   EncodeBase64(Source:string):string;
      var   
          Times,LenSrc,i:integer;   
          x1,x2,x3,x4:char;   
          xt:byte;   
      begin   
          result:='';   
          LenSrc:=length(Source);   
          if   LenSrc   mod   3   =0   then   Times:=LenSrc   div   3   
          else   Times:=LenSrc   div   3   +   1;   
          for   i:=0   to   times-1   do   
          begin   
              if   LenSrc   >=   (3+i*3)   then   
              begin   
                  x1:=BaseTable[(ord(Source[1+i*3])   shr   2)+1];   
                  xt:=(ord(Source[1+i*3])   shl   4)   and   48;   
                  xt:=xt   or   (ord(Source[2+i*3])   shr   4);   
                  x2:=BaseTable[xt+1];   
                  xt:=(Ord(Source[2+i*3])   shl   2)   and   60;   
                  xt:=xt   or   (ord(Source[3+i*3])   shr   6);   
                  x3:=BaseTable[xt+1];   
                  xt:=(ord(Source[3+i*3])   and   63);   
                  x4:=BaseTable[xt+1];   
              end   
              else   if   LenSrc>=(2+i*3)   then   
              begin   
                  x1:=BaseTable[(ord(Source[1+i*3])   shr   2)+1];   
                  xt:=(ord(Source[1+i*3])   shl   4)   and   48;   
                  xt:=xt   or   (ord(Source[2+i*3])   shr   4);   
                  x2:=BaseTable[xt+1];   
                  xt:=(ord(Source[2+i*3])   shl   2)   and   60;   
                  x3:=BaseTable[xt+1];   
                  x4:='=';   
              end   else   
              begin   
                  x1:=BaseTable[(ord(Source[1+i*3])   shr   2)+1];   
                  xt:=(ord(Source[1+i*3])   shl   4)   and   48;   
                  x2:=BaseTable[xt+1];   
                  x3:='=';   
                  x4:='=';   
              end;   
              result:=result+x1+x2+x3+x4;   
          end;   
      end;   
      //////////   
      procedure   TForm1.Button1Click(Sender:   TObject);   
      var   MailTo,MailBody:TStringList;   
      begin   
      Nmsmtp1.Host   :='smtp.163.com';
      nmsmtp1.Port   :=25;   
      nmsmtp1.UserID   :='okmnji79513';//发信人的用户名,必须是真实的
      nmsmtp1.ReportLevel   :=1;   
      Nmsmtp1.TimeOut   :=10000;   
      nmsmtp1.Connect   ;         ///连接   
      if   AuthSucc=true   then   ////验证成功   
      begin   
          MailTo:=TStringList.Create;   
          MailTo.Add('[email protected]');
          MailBody.Add('Hello   it   is   a   test');   
          nmsmtp1.PostMessage.FromAddress:='[email protected]';   //发信人的电子邮件地址
          nmsmtp1.PostMessage.ToAddress   :=MailTo;   
          nmsmtp1.PostMessage.Body:=MailBody;   
          nmsmtp1.PostMessage.Subject   :='My   test';   
          Mailto.Clear   ;   
          //Mailto.Add('c:\a.txt');   
          //Mailto.Add('c:\b.txt');   
          //nmsmtp1.PostMessage.Attachments:=MailTo;   附件   
          MailTo.Free   ;   
          MailBody.Free;   
          nmsmtp1.SendMail;   
      end;   
      end;   
      procedure   TForm1.NMSMTP1Connect(Sender:   TObject);   
      begin   
      //////连接成功,下面用户认证过程   
          label1.caption:=nmsmtp1.Status;   
          if   nmsmtp1.ReplyNumber   =   250   then   
              label1.caption:=nmsmtp1.Transaction('auth   login');   //开始认证   
          if   nmsmtp1.ReplyNumber   =334   then   //返回值为334,让你输入用BASE64编码后的用户名   
              label1.caption:=nmsmtp1.Transaction(EncodeBase64('okmnji79513'));//   用户名aaaaa
          if   nmsmtp1.ReplyNumber   =334   then     //   返回值为334,让你输入用BASE64编码后的用户密码
              label1.caption:=nmsmtp1.Transaction(EncodeBase64('zhouchun'));   //密码为123456   
          if   nmsmtp1.ReplyNumber   =235   then   
          begin   
              label1.caption:='successful';   
              AuthSucc:=true;   
          end;   
          //showmessage(label1.caption);   
      end;   
        
      procedure   TForm1.NMSMTP1InvalidHost(var   Handled:   Boolean);   
      begin   
          label1.caption   :='Invalid   Host';   
      end;   
        
      procedure   TForm1.NMSMTP1ConnectionFailed(Sender:   TObject);   
      begin   
      label1.caption   :='connect   failed';   
      end;   
        
      procedure   TForm1.NMSMTP1Status(Sender:   TComponent;   Status:   String);   
      begin   
          label1.caption   :=nmsmtp1.Status   ;   
      end;   
        
      procedure   TForm1.NMSMTP1SendStart(Sender:   TObject);   
      begin   
          label1.Caption   :='start   send';   
      end;   
        
      procedure   TForm1.NMSMTP1Success(Sender:   TObject);   
      begin   
          label1.Caption:='send   success!';   
      end;   
        
    end.
      

  2.   

    一般网站都屏蔽了25的smtp和110的pop3端口吧。你可以自己安装一个邮件服务器来测试。或者用telnet连你试的哪些网站:telnet smtp.163.com 25手工发命令试一下:
    helo localhost
    auth login
    okmnji79513
    xxxxxmail from:[email protected]
    rcpt to:[email protected]还有保证EncodeBase64算法是对的, 另外鉴权的方式有多种,也许该邮件服务器没有启用login方式。
      

  3.   


    helo 163.com
    250 OK
    auth login
    334 dXNlcm5hbWU6
    okmnji79513535 Error: authentication failed
    如上面:手工时,输到账号'okmnji79513' 就报:535 Error: authentication failed然后我试着用Base64编码过的数据进行。用户名:'b2ttbmppNzk1MTM=' 密码:'emhvdWNodW4=',结果报:550 用户被锁定。如下:
    helo 163.com
    250 OK
    auth login
    334 dXNlcm5hbWU6
    b2ttbmppNzk1MTM=334 UGFzc3dv
    emhvdWNodW4=550 用户被锁定然后我在helo 163.com的后面加了句ehlo 163.com 结果同上(550 用户被锁定)。
    把'helo 163.com'换成'ehlo 163.com' 依然。如下:
    ehlo 163.com
    250-mail
    250-PIPELINING
    250-AUTH LOGIN PLAIN
    250-AUTH=LOGIN PLAIN
    250 8BITMIME
    auth login
    334 dXNlcm5hbWU6
    b2ttbmppNzk1MTM=334 UGFzc3dvcmQ6
    emhvdWNodW4=550 用户被锁定
      

  4.   


    网上大多 认为是 网站限制了indy控件不过 我不是很懂 是不是不用indy控件就行了?
    还有2楼朋友说的: '也许该邮件服务器没有启用login方式' 那如何确定 服务器使用何种方式?
      

  5.   

    现在有几点困惑:1、是否163等服务器 的 某些命令改了 或是 顺序变了,从而使indy控件无效了?2、是否只要改变indy中相关命令 或其 顺序,就可以了?但是 如何改?3、上面我 手工 输入命令时,有何错误?为何失败?求教
      

  6.   

    楼上的代码是没有错
    主要是免费邮箱服务器设置的问题,用旧的且使用过POP3收发的邮箱就没有问题。