我在使用TNMSmtp控件时怎么老是出现Authentication failure,该怎么解决? 多谢!!!

解决方案 »

  1.   

    看看这段代码,有什么地方不懂的再问:
    unit frmSendEMail;interfaceuses
      Windows, NMsmtp, classes, Sysutils, Forms;type
      Tevendesc = class
      private
        class procedure NMSMTPConnect(Sender: TObject);
      end;  function SendM_E_Mail(Host: String; Port: Integer; FromAddress: String; ToAddressList: TStringList;
    aUserName, aPassword: string; Subject: String = ''; Body: TStringList = nil): Boolean;
      function SendA_E_Mail(Host: String; Port: Integer; FromAddress: String; ToAddress: String;
    aUserName, aPassword: string; Subject: String = ''; Body: TStringList = nil): Boolean; implementationconst
      BaseTable = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
    var
      fNMSMTP: TNMSMTP;
      fUserName, fPassword: String;
      
    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;function GeneralFromEmailAddress(aHost: String; aUserName: String): String;
    var
      iPos: Integer;
      tempString: String;
    begin
      Result := '';
      if Pos('.', aHost) > 0 then
        tempString := Copy(aHost, Pos('.', aHost) + 1, Length(aHost) - Pos('.' , aHost));
      if Pos('.', tempString) > 0 then
        tempString := Copy(tempString, Pos('.', tempString) + 1, Length(tempString) - Pos('.' , tempString));
      if Pos('.', tempString) > 0 then
        Exit;
      iPos := Pos('.', aHost);
      if iPos > 0 then
      begin
        Result := aUserName + '@' + Copy(aHost, iPos + 1, Length(aHost) - iPos);
      end;
    end;procedure SetHost(aNMSMTP: TNMSMTP; Host: String; Port: Integer; UserID: String);
    begin
      aNMSMTP.Host := Host;
      aNMSMTP.Port := Port;
      if UserID <> '' then
        aNMSMTP.UserID := UserID;
      aNMSMTP.TimeOut := 1000;
      aNMSMTP.OnConnect := Tevendesc.NMSMTPConnect;
    end;procedure SetConnection(aNMSMTP: TNMSMTP);
    begin
      if not aNMSMTP.Connected then
        aNMSMTP.Connect;
    end;procedure SetAndSendMail(aNMSMTP: TNMSMTP; FromAddress: String;
    Subject: String = ''; Body: TStringList = nil);
    begin
      //if (aNMSMTP.ReplyNumber = 235) or (aNMSMTP.ReplyNumber = 500) or (fUserName = '') then
      //begin
      try
        aNMSMTP.Charset := 'GB2312_CHARSET ';
        aNMSMTP.PostMessage.FromAddress := FromAddress;
        aNMSMTP.PostMessage.Subject := Subject;
        if Body <> nil then
          aNMSMTP.PostMessage.Body.Text := Body.Text;
        aNMSMTP.PostMessage.Date := DateToStr(Now());
        aNMSMTP.SendMail;
        if aNMSMTP.Connected then
          aNMSMTP.Disconnect;
      except  end;
      //end;
      if (aNMSMTP.ReplyNumber <> 235) and (fUserName = '') then
        ;//Application.MessageBox('', '', 0);
    end;function SendM_E_Mail(Host: String; Port: Integer; FromAddress: String; ToAddressList: TStringList;
      aUserName, aPassword: string; Subject: String = ''; Body: TStringList = nil): Boolean;
    var
      aFromAddress: String;
    begin
      fUserName := aUserName;
      fPassword := aUserName;
      fNMSMTP := TNMSMTP.Create(nil);
      try
        try
          SetHost(fNMSMTP, Host, Port, aUserName);
          SetConnection(fNMSMTP);
          fNMSMTP.PostMessage.ToAddress.Text := ToAddressList.Text;
          aFromAddress := GeneralFromEmailAddress(Host, fUserName);
          if aFromAddress = '' then
            aFromAddress := FromAddress;
          SetAndSendMail(fNMSMTP, aFromAddress, Subject, Body);
          Result := True;
        except
          Result := False;
        end;
      finally
        fNMSMTP.Free;
      end;
    end;function SendA_E_Mail(Host: String; Port: Integer; FromAddress: String; ToAddress: String;
      aUserName, aPassword: string; Subject: String = ''; Body: TStringList = nil): Boolean;
    var
      aFromAddress: String;
    begin
      fUserName := aUserName;
      fPassword := aPassword;
      fNMSMTP := TNMSMTP.Create(nil);
      try
        try
          SetHost(fNMSMTP, Host, Port, aUserName);
          SetConnection(fNMSMTP);
          fNMSMTP.PostMessage.ToAddress.Text := ToAddress;
          aFromAddress := GeneralFromEmailAddress(Host, fUserName);
          if aFromAddress = '' then
            aFromAddress := FromAddress;
          SetAndSendMail(fNMSMTP, aFromAddress, Subject, Body);
          Result := True;
        except
          Result := False;
        end;
      finally
        fNMSMTP.Free;
      end;
    end;{ Tevendesc }class procedure Tevendesc.NMSMTPConnect(Sender: TObject);
    begin
      if fNMSMTP.ReplyNumber <> 500 then
      begin
        if fNMSMTP.ReplyNumber = 250 then
          fNMSMTP.Transaction('auth login');//开始认证
        if fNMSMTP.ReplyNumber = 334 then
        begin
          fNMSMTP.Transaction(EncodeBase64(fUserName));  // 处理用户名
          fNMSMTP.Transaction(EncodeBase64(fPassword));  // 处理密码
        end;
      end;
    end;end.