求救:TNMSMTP如何发送邮件???出现“需要SMTP认证”错误怎么解决???

解决方案 »

  1.   

    TNMSMTP发送邮件是需要对发送着自己的邮件地址和对方的邮件地址都要进行验证,如果发送着自己的邮件地址不正确,发信过程也会失败
    其余的你只需要填好表头就行了
    try
      connect;
    except
      showmessage('不能连接服务器');
      exit;
    end;
    postmessage.fromaddress:=FromAdd_Edit.text;
    postmessage.FromName:=FromName_Edit.text;
    Postmessage.Subject:=Sub_Edit.text;
    postmessage.ToAddress.Add(TAddr_Edit.text);
    postmessage.body.assign(Memo1.Lines);
    try
      try
      sendMail;
      finally
      Disconnect;
    except
      Begin
        showmessage('发送邮件失败');
        exit;
    end;
    end;
      

  2.   

    TNMSMTP发送邮件是需要对发送着自己的邮件地址和对方的邮件地址都要进行验证,如果发送着自己的邮件地址不正确,发信过程也会失败
    其余的你只需要填好表头就行了
    try
      connect;
    except
      showmessage('不能连接服务器');
      exit;
    end;
    postmessage.fromaddress:=FromAdd_Edit.text;
    postmessage.FromName:=FromName_Edit.text;
    Postmessage.Subject:=Sub_Edit.text;
    postmessage.ToAddress.Add(TAddr_Edit.text);
    postmessage.body.assign(Memo1.Lines);
    try
      try
      sendMail;
      finally
      Disconnect;
    except
      Begin
        showmessage('发送邮件失败');
        exit;
    end;
    end;
      

  3.   

    需要SMTP认证的意思是需要身份验证;
    你可以用USERID属性和Verify方法。
      

  4.   

    如果要求用密码验证就用TidSMTP
      

  5.   

    我也是用事先try一下connect,有问题就报错退出或启动定时过一段时间再try connect,直到成功为止。
      

  6.   

    刚好最近也在做这个,使用过程还没有发现什么问题,多指点:
    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.