我在网上找了段代码,照着自己写了一下,都无法实现发邮件,参考的代码:控件
 smtp: TIdSMTP;
 MgeSend: TIdMessage;
代码
procedure TForm1.Button1Click(Sender: TObject);
begin
  SMTP.Host:='smtp.163.com';
  smtp.Username:='[email protected]';
  smtp.Password:='paswrd';
  smtp.Port:=25;
  smtp.Connect();
  MgeSend.Recipients.EMailAddresses:='[email protected]';
  MgeSend.From.Text :='[email protected]';
  MgeSend.Subject:='test';
  MgeSend.Body.Text:='发邮件测试';
  SMTP.Authenticate;
  Smtp.Send(mgeSend);
end; 我自己写的代码:unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdMessageClient, IdSMTP, IdMessage;type
  TForm1 = class(TForm)
    smtp: TIdSMTP;
    MgeSend: TIdMessage;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
    smtp.AuthenticationType:=atLogin; //设置登陆类型
    smtp.Username:='ibmdellloveno'; //设置登陆帐号
    smtp.Password:='aaaaaa123456'; //设置登陆密码
    smtp.Host:='smtp.163.com'; //设置SMTP地址
    smtp.Port:=25; //设置端口  必须转化为整型
    smtp.Connect;  //开始连接服务器
  MgeSend.Recipients.EMailAddresses:='[email protected]';
  MgeSend.From.Text :='[email protected]';
  MgeSend.Subject:='test';
  MgeSend.Body.Text:='发邮件测试';
  SMTP.Authenticate;
  Smtp.Send(mgeSend);
  end;
end.我用delphi7,不知道是不是版本的问题,谁能帮忙改一下,能发邮件就行,不用带附件,谢谢。

解决方案 »

  1.   

    163邮箱有限制
    给你转载一下
    以下是我正在使用的一只程序。可以正常发邮件。如果不能发,请检查一下防火墙,有可以是防火墙挡住了。 unit Unit1; interface uses 
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
      Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdTCPConnection, 
      IdTCPClient, IdMessageClient, IdNNTP, IdSMTP, IdMessage, inifiles, 
      ExtCtrls, DateUtils; type 
      TForm1 = class(TForm) 
        IdSMTP1: TIdSMTP; 
        IdMessage1: TIdMessage; 
        Label1: TLabel; 
        procedure FormCreate(Sender: TObject); 
        procedure Timer1Timer(Sender: TObject); 
      private 
        { Private declarations } 
      public 
        { Public declarations } 
      end; var 
      Form1: TForm1; implementation {$R *.dfm} function writelog(const logstr: string): integer; 
    var 
      logFilestr: textfile; 
      FileHandle: thandle; 
    begin 
      try 
        if not FileExists('c:\errorlog.log') then 
        begin 
          FileHandle := filecreate('c:\errorlog.log'); 
          FileClose(FileHandle); 
        end; 
        AssignFile(logFilestr, 'c:\errorlog.log'); 
        Reset(logFilestr); 
        if FileSize(logfilestr) > 1000 * 8 then begin 
          closefile(logfilestr); 
          deletefile('c:\errorlog.log'); 
          FileHandle := filecreate('c:\errorlog.log'); 
          FileClose(FileHandle); 
          AssignFile(logFilestr, 'c:\errorlog.log'); 
        end; 
        append(logFilestr); 
        WriteLn(logFilestr, logstr); 
        //WriteLn(logFilestr, ''); 
        Closefile(logFilestr); 
      finally   end; 
      result := 0; 
    end; procedure TForm1.FormCreate(Sender: TObject); 
    var 
      IniFile: TIniFile; 
      FileHandle: Thandle; 
      Filedate: TDateTime; 
    begin 
      label1.Caption := 'Start at ' + datetimetostr(now()); 
      IniFile := TIniFile.Create('.\mailer.ini'); 
      if inifile.ReadString('mailhead', 'checkfiledate', '') = '1' then 
      begin 
        if fileexists(inifile.ReadString('mailhead', 'Attachment', '')) then 
        begin 
          FileHandle := FileOpen(inifile.ReadString('mailhead', 'Attachment', ''), fmOpenRead); 
          FileDate := FileDateToDateTime(FileGetDate(FileHandle)); 
          FileClose(filehandle); 
          if dateof(filedate) <> dateof(now()) then begin 
            writelog(DateTimeToStr(now) + #9+ 'Today has no Error.'); 
            Application.Terminate; 
            Exit; 
          end; 
        end 
        else begin 
          writelog(DateTimeToStr(now) + #9+ 'No Errorlogfile.'); 
          Application.Terminate; 
          exit; 
        end;   end;   idsmtp1.Host := inifile.ReadString('mailserver', 'host', ''); 
      idsmtp1.Username := inifile.ReadString('mailserver', 'username', ''); 
      idsmtp1.Password := inifile.ReadString('mailserver', 'password', ''); 
      idsmtp1.AuthenticationType := atLogin; 
      idmessage1.From.Text := inifile.ReadString('mailhead', 'from', ''); 
      idmessage1.Recipients.EMailAddresses := inifile.ReadString('mailhead', 'Recipients', ''); 
      idmessage1.Subject := inifile.ReadString('mailhead', 'subject', ''); 
      idmessage1.Body.text := 'See the attachment.    --' + datetimetostr(now()); 
      if fileexists(inifile.ReadString('mailhead', 'Attachment', '')) then 
        TIdAttachment.Create(idMessage1.MessageParts, inifile.ReadString('mailhead', 'Attachment', '')); 
      if fileexists(inifile.ReadString('mailhead', 'Attachment1', '')) then 
        TIdAttachment.Create(idMessage1.MessageParts, inifile.ReadString('mailhead', 'Attachment1', '')); 
      if fileexists(inifile.ReadString('mailhead', 'Attachment2', '')) then 
        TIdAttachment.Create(idMessage1.MessageParts, inifile.ReadString('mailhead', 'Attachment2', '')); 
      if fileexists(inifile.ReadString('mailhead', 'Attachment3', '')) then 
        TIdAttachment.Create(idMessage1.MessageParts, inifile.ReadString('mailhead', 'Attachment3', '')); 
      try 
        idsmtp1.Connect; 
      except 
        begin 
    //      showmessage('connect error'); 
          writelog(DateTimeToStr(now) +  #9+'Can not connect host' + idsmtp1.Host); 
          Application.Terminate; 
          exit; 
        end; 
      end; 
    //if SMTP1.Connected() then 
    //  begin 
      try 
        idsmtp1.Send(idmessage1); 
      except 
      //  showmessage('send fail'); 
        writelog(DateTimeToStr(now) + #9+ 'Send failed.'); 
        Application.Terminate; 
        Exit; 
      end; 
      writelog(DateTimeToStr(now) + #9+'Send OK.'); 
      Application.Terminate; end; procedure TForm1.Timer1Timer(Sender: TObject); 
    begin 
      Close; 
    end; end. 
      

  2.   

    以下是纯API 发送邮件的过程,经本人今天测试可以发送,你自己要写上账号和密码
    unit mailapi;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs,winsock, StdCtrls,MAPI;type
      TForm1 = class(TForm)
        Button2: TButton;
        Edit1: TEdit;
        Button3: TButton;
        Edit2: TEdit;
        Edit3: TEdit;
        procedure FormCreate(Sender: TObject);
        procedure FormClose(Sender: TObject; var Action: TCloseAction);
        procedure Button2Click(Sender: TObject);
        procedure Button1Click(Sender: TObject);
        procedure Button3Click(Sender: TObject);  private
        { Private declarations }
      public
        { Public declarations }  end;var
      Form1: TForm1;
      buf:array[1..100] of Thandle;
      i:cardinal;implementation
    //////////////////////********************
    function sendmails():integer;stdcall;vars:tsocket;
    sbuf,buffer:array[0..255] of char;
    len,err,errorcode:integer;
    mailserver:sockaddr_in;
    begin
    s:=socket(AF_INET,SOCK_STREAM,0);
    mailserver.sin_family:=AF_INET;
    mailserver.sin_port:=htons(25);
    mailserver.sin_addr.S_addr:=inet_addr('220.181.12.11');
    len:=sizeof(mailserver);errorcode:=connect(s,mailserver,len);
         recv(s,sbuf,400,0);
    if errorcode<>invalid_socket thenbeginbuffer:='EHLO smtp.163.com'+#13#10;           //跟Smtp打招呼err:=send(s,buffer,length('EHLO smtp.163.com'+#13#10),0);
      recv(s,sbuf,400,0);
     buffer:='AUTH LOGIN'+#13#10;
     send(s,buffer,length('AUTH LOGIN'+#13#10),0);
     recv(s,sbuf,400,0); buffer:='MjZyZ2RAMTYzLmNvbQ=='+#13#10;             //这时是账号如:[email protected]
     err:=send(s,buffer,length('MjZyZ2RAMTYzLmNvbQ=='+#13#10),0);
     recv(s,sbuf,400,0);
     buffer:='密码=='+#13#10;      //这是密码   密码用要Base64编码加密
     err:=send(s,buffer,length('密码=='+#13#10),0);
         recv(s,sbuf,400,0);
       showmessage(pchar(@sbuf));buffer:='MAIL FROM:<[email protected]>'+#13#10;    //发件人
    send(s,buffer,length('MAIL FROM:<[email protected]>'+#13#10),0);
    recv(s,sbuf,400,0);
    showmessage(pchar(@sbuf));buffer:='RCPT TO:<[email protected]>'+#13#10;      //  收件人
    send(s,buffer,length('RCPT TO:<[email protected]>'+#13#10),0);
    recv(s,sbuf,400,0);
    showmessage(pchar(@sbuf));buffer:='DATA'+#13#10;        //以下是内容
    send(s,buffer,length('DATA'+#13#10),0);
    buffer:='FROM:<[email protected]>'+#13#10;
    send(s,buffer,length('FROM:<[email protected]>'+#13#10),0);
    buffer:='TO:<[email protected]>'+#13#10;send(s,buffer,length('TO:<[email protected]>'+#13#10),0);buffer:='SUBJECT:send a ail!'+#13#10+#13#10;     //主题send(s,buffer,length('SUBJECT:send a ail!'+#13#10+#13#10),0);buffer:='I LOVE THIS GAME!'+#13#10;   //正文send(s,buffer,length('I LOVE THIS GAME!'+#13#10),0);buffer:='.'+#13#10;send(s,buffer,length('.'+#13#10),0);buffer:='QUIT'+#13#10;send(s,buffer,length('QUIT'+#13#10),0);closesocket(s);
       Result:=1;
    endend;
    function   EnumChildProc( hwnd:Thandle ; lParam:integer):bool; stdcall;
    begin
        buf[i]:=hwnd;
        i:=i+1;
        if hwnd=0 then
        Result:=false
        else
         Result:=true;end;{$R *.dfm}
    procedure TForm1.Button1Click(Sender: TObject);
    begin
    sendmails();
    end;procedure TForm1.Button2Click(Sender: TObject);
    begin
    sendmails;//sendmessage(0,WM_QUERYENDSESSION,0,0) ;
    end;procedure TForm1.Button3Click(Sender: TObject);
    var
    hEdit,han1,han2,han3:Thandle;
    text:string;
    n:cardinal;
    name:array[0..50] of char;
    begin
    hEdit:=FindWindow(nil,Pchar(edit3.text));
    //hEdit:=FindWindow(nil,'Form2');
    //showmessage(inttostr(hEdit));
    han1:=FindWindowEx(hEdit,0,nil,nil);
    //han2:=FindWindowEx(hEdit,0,nil,nil);
    //han3:=FindWindowEx(hEdit,0,nil,nil);
    //showmessage(inttostr(han1));
    //showmessage(inttostr(han2));
    //showmessage(inttostr(han3)); text:= edit1.text;
    sendmessage(han1,WM_SETTEXT,0,integer(text)); EnumChildWindows( hEdit,@EnumChildProc,4);
     sleep(100);
     for n := 1 to 50 do
     begin
     if buf[n]<>0 then
     begin
       sendmessage( buf[Strtoint(edit2.text)],WM_SETTEXT,0,integer(text));
       GetClassName(buf[9],@name,50);
       button2.Caption:=name;
      // sleep(2000);
     end;
     end;
     
     i:=1;
    end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
    begin
    wsacleanup;
    end;procedure TForm1.FormCreate(Sender: TObject);
    varwsa:twsadata;begin
    i:=1;
    wsastartup($0202,wsa);
    end;end.
      

  3.   

    Delphi7自带的IdSMTP.PAS有问题,到网上找替代文件覆盖。
      

  4.   

    使用IDsmtp 連線發信時,會讓程式有停頓的狀況產生
    有什麼方式可以解決呢?
      

  5.   

    我给你指定个地址去下吧!绝对可以直接编译的!http://download.csdn.net/user/Happ_QJW928
      

  6.   

    http://download.csdn.net/user/Happ_QJW928这个地址可以直接下,很好的实例!直接现成的!
      

  7.   

    Happ_QJW928,你发的代码我下载了,是通过outlook实现的吧,有没有不通过outlook直接实现的?
    Internetroot,我写的代码是否有问题?我换个delphi2007试试。
      

  8.   

    把我使用的现成的代码给你吧,不过一定要结帖,千万不要忘记帮助你的人!
      //MailMessage是TIdMessage类型
      with MailMessage do
      begin
        Subject :='...'; //邮件主题
        Body.Text:='...';//邮件正文
        From.Address:='...';     //发信人地址
        Recipients.EMailAddresses:='...'; //收件人地址
      end;  //设置连接到服务器属性
      with IdSMTP do
      begin
        Host:='smtp.163.com'; //SMTP服务器地址
        Port:=25;             //SMTP服务器端口
        UserName:='...'; //用户账号
        Password:='...'; //用户密码
      end;
      //连接到服务器
      try
        IdSMTP.Connect();  //调用 Connect连接服务器
      except
        Application.MessageBox('connect error','error',MB_ICONERROR);
        Exit;
      end;
      //检测SMTP服务器是否需要验证
      if (IdSMTP.AuthSchemesSupported.IndexOf('LOGIN' )<>-1) then
        IdSMTP.AuthenticationType:=atlogin;    ////服务器要求验证
      try
        if not IdSMTP.Authenticate then
        begin
          Application.MessageBox('login error','error',MB_ICONERROR);
          IdSMTP.Disconnect;
          exit;
        end;
      except
        Application.MessageBox('error...','error',MB_ICONERROR);
        IdSMTP.Disconnect;
        exit;
      end;
      //发送信件
      try
        IdSMTP.Send(MailMessage);
      except
        Application.MessageBox('send error','error',MB_ICONERROR);
      end;
      IdSMTP.Disconnect;
      

  9.   

    http://topic.csdn.net/u/20081203/11/93043a17-d184-42da-b3e6-cf3d8c92026e.html
      

  10.   

    indy9的不能用的,indy10的demo没有问题
      

  11.   

    把我使用的现成的代码给你吧,不过一定要结帖,千万不要忘记帮助你的人! 
      //MailMessage是TIdMessage类型 
      with MailMessage do 
      begin 
        Subject :='...'; //邮件主题 
        Body.Text:='...';//邮件正文 
        From.Address:='...';    //发信人地址 
        Recipients.EMailAddresses:='...'; //收件人地址 
      end;   //设置连接到服务器属性 
      with IdSMTP do 
      begin 
        Host:='smtp.163.com'; //SMTP服务器地址 
        Port:=25;            //SMTP服务器端口 
        UserName:='...'; //用户账号 
        Password:='...'; //用户密码 
      end; 
      //连接到服务器 
      try 
        IdSMTP.Connect();  //调用 Connect连接服务器 
      except 
        Application.MessageBox('connect error','error',MB_ICONERROR); 
        Exit; 
      end; 
      //检测SMTP服务器是否需要验证 
      if (IdSMTP.AuthSchemesSupported.IndexOf('LOGIN' ) <>-1) then 
        IdSMTP.AuthenticationType:=atlogin;    ////服务器要求验证 
      try 
        if not IdSMTP.Authenticate then 
        begin 
          Application.MessageBox('login error','error',MB_ICONERROR); 
          IdSMTP.Disconnect; 
          exit; 
        end; 
      except 
        Application.MessageBox('error...','error',MB_ICONERROR); 
        IdSMTP.Disconnect; 
        exit; 
      end; 
      //发送信件 
      try 
        IdSMTP.Send(MailMessage); 
      except 
        Application.MessageBox('send error','error',MB_ICONERROR); 
      end; 
      IdSMTP.Disconnect; 
     
     
     
    这段代码我放在按钮事件下了,也加了哪两个控件,按下按钮就报错?
      

  12.   

    我的那个纯API的怎么会不行呢,你要自己改下IP 和帐号和密码啦,我都测试过可以的,不懂就要慢慢研究,要是不能通过的话我也 不会贴上来啦
      

  13.   


    又来这一手。私下交易,不过,我提醒楼主,他最后可能让你加分。或者他直接发一个exe的文件给你。mwy654321,对你bs加无语
      

  14.   

    mwy654321,快过年了。你这一年中,都是私下把代码发给对方的,从来没有在帖子中发过代码。这也不是什么惊天动地的代码,开源吧,走出自己的阴霾.
      

  15.   


    http://topic.csdn.net/u/20081222/18/7c3e511b-c8fc-4c1a-92a0-682d1d5a6031.html
    http://topic.csdn.net/u/20081223/15/261da121-e1d4-4032-afe2-67bf0e0814d3.html这是最近的两个贴子,“从来没有在帖子中发过代码”,不知道你在放什么狗屁。
      

  16.   

    我测试了你的代码,在delphi 2007中
    代码中这样设置:procedure TForm1.Button3Click(Sender: TObject);
    begin  IdSMTP1.AuthType:=atDefault;
      IdSMTP1.Username := '******@163.com';
      IdSMTP1.Password := '*****';
      IdSMTP1.Host := 'smtp.163.com';
      IdSMTP1.Port := 25; //如果转换错误默认25
      try
        IdSMTP1.Connect; //连接
      except
        ShowMessage('smtp failed to connect');
        Exit;
      end;  IdMessage1.Body.Text:=('shishi fa song you jian ');
      IdMessage1.From.Text := '*****@163.com';
      IdMessage1.Recipients.EMailAddresses :='[email protected]';
      IdMessage1.Subject := 'hao';
      try
          IdSMTP1.Send(IdMessage1);
      finally
        IdSMTP1.Disconnect;
      end;end;成功发送
      

  17.   

    你的问题因为没有给你解决,你就对别人攻击。你不仅骂过我,还骂过unsigned 僵哥,骂过Corn1玉米,注册多个马甲闹事,其目的就是想让这些星级以上的号跟你一起被封掉。你也回头看看你发过的那些贴子。本人,在今日之前一直对你的谩骂躲避,现在实在受不了你!
      

  18.   

    等我处理完mwy哪个杂种,我再试试
      

  19.   

    一上来就看见吵架贴,我说这2个人怎么一下就吵起来了,原来,昨天mwy654321 发的一帖就开干上了!
    过年果然就是要过热闹,我来做个总结:
     昨天那贴可以叫做 美女与野兽,今天这贴就只能叫美女抢糖果了!!!
      

  20.   

    qxf32,我运行你给的代码后提示:project project1.exe raised excption class eclassnotfound with message 'class tidsmtp not found'.
      

  21.   

    我是在delphi2007下运行的,有朋友告诉是要把用户名和密码转换为base64才行,我也试过还是不行,163和126的都试验过。