大家好,我是DELPHI小菜,学着人家的例子写了个发邮件的程序,刚写好时是能用的,后来又改了下,就不能用了,再改回去也不能用了。请大家帮帮忙看看源码错在哪里,谢谢!还有个问题就是,我不知道怎么加附件,请高手指点。unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, IdComponent, IdTCPServer, IdSMTPServer,
  IdBaseComponent, IdMessage, IdTCPConnection, IdTCPClient,
  IdMessageClient, IdSMTP, ComCtrls, Buttons;type
  TForm1 = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    Edit5: TEdit;
    Edit6: TEdit;
    Button1: TButton;
    IdMessage1: TIdMessage;
    IdSMTP1: TIdSMTP;
    Memo1: TMemo;
    StatusBar1: TStatusBar;
    Label8: TLabel;
    Edit7: TEdit;
    SpeedButton1: TSpeedButton;
    OpenDialog1: TOpenDialog;
    procedure Button1Click(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
IdSMTP1.AuthenticationType:=atLogin;
IdSMTP1.Username:=Edit3.Text;
IdSMTP1.Password:=Edit4.Text;
IdSMTP1.Host:=Edit1.Text;
IdSMTP1.Port:=StrToInt(Edit2.Text);
try
IdSMTP1.Connect(1000);
except
StatusBar1.Panels[2].Text:='连接SMTP服务器失败!';
exit;
end;
try
With IdMessage1 do
begin
body.Clear;
body.Add(Memo1.Text);
From.Address:=Edit3.text;
Recipients.EMailAddresses:=Edit5.Text;
Subject:=Edit6.Text;end;
IdSMTP1.Send(IdMessage1);
except
StatusBar1.Panels[2].Text:=' 发送邮件到' + Edit5.Text + '失败!';
IdSMTP1.Disconnect;
exit;
end;
StatusBar1.Panels[2].Text:='已发送到' + Edit5.Text;
IdSMTP1.Disconnect;end;end.

解决方案 »

  1.   

    function TMailSender.SendEmail(const MailFrom, MailTo, Subject, Content,
      Attachment: WideString): Boolean;
    var
      dwConnectionTypes:DWORD;
      aAttachList : TStringList;
      Smtp: TIdSmtp;
      AttPart : TIDAttachment;
      Mess : TIDMessage;
      i : Integer;
    begin
      aAttachList := nil;
      Smtp := nil;
      AttPart :=  nil;
      Mess := nil;
      Result := False;
      Smtp:= TIdSmtp.Create(nil);
      try
        Smtp.Disconnect;
    {    if not InternetGetConnectedState(@dwConnectionTypes,0) then
        begin
          FMessage := 'Error: Please check the network';
          exit;
        end
        else}
        begin
          if FAuthorised then
            Smtp.AuthenticationType := atLogin
          else
            Smtp.UseEhlo := false;
          Smtp.Host := FHostName;
          Smtp.Port := FPort;
          Smtp.Username := FUserName;
          Smtp.Password := FPassword;
          Smtp.Connect(2000);      Mess := TIDMessage.Create(Application);
          Mess.From.Text := MailFrom;
          Mess.Recipients.EMailAddresses := MailTo;
          Mess.Subject := Subject;
          Mess.Body.Add(Content);
          Mess.MessageParts.Clear;
          if (Attachment<>'') then
          begin
            aAttachList := TStringList.Create;
            aAttachList.Text := Attachment;
            for i := 0 to aAttachList.Count - 1 do
            begin
              if FileExists(aAttachList.Strings[i]) then
                AttPart := TIDAttachment.Create(Mess.MessageParts,aAttachList.Strings[i]);
            end;
          end;
          Smtp.Send(Mess);
          Result := true;
        end;
      except
        on E : Exception do
        begin
          FMessage := E.Message;
          Result := false;
        end;
      end;
      if Assigned(aAttachList) then FreeAndNil(aAttachList);
      if Assigned(AttPart) then FreeAndNil(AttPart);
      if Assigned(Mess) then FreeAndNil(Mess);
      if Assigned(Smtp) then FreeAndNil(Smtp);
      

  2.   

    你把源码发过来吗!
    [email protected]