Using INDY SMTPServer

后端 未结 1 577
抹茶落季
抹茶落季 2021-01-06 15:56

following code is what i modified(to work with 10.5.8) form the sample in order to send email independently using the smtp client described in Indy Mail server

fol

相关标签:
1条回答
  • 2021-01-06 16:36

    To pass the email on to another system (Gmail in this case), use the TIdSMTPRelay component, eg:

    procedure TForm1.IdSMTPServer1RcptTo(ASender: TIdSMTPServerContext;                
      const AAddress: string; AParams: TStrings; var VAction: TIdRCPToReply;                
      var VForward: string);                
    begin                
      if (AAddress represents a user in your network) then
        VAction := rAddressOk
      else if (AAddress is outside of your network) then
        VAction := rWillForward;                    
    end;
    
    procedure TForm1.IdSMTPServer1MsgReceive(ASender: TIdSMTPServerContext; AMsg: TStream; var LAction: TIdDataReply); 
    var 
      LMsg : TIdMessage; 
      LMsgClient: TIdMessageClient;
      LStream : TFileStream; 
      LRelayRecipients: TIdEMailAddressList;
      LRelay: TIdSMTPRelay;
      I: Integer;
    begin 
      // When a message is received by the server, this event fires. 
      // The message data is made available in the AMsg : TStream. 
      // In this example, we will save it to a temporary file, and load it using 
      // IdMessage to parse some header elements. 
    
      LStream := TFileStream.Create(ExtractFilePath(Application.exename) + 'test.eml', fmCreate); 
      Try 
        LStream.CopyFrom(AMsg, 0); 
      Finally 
        LStream.Free; 
      End; 
      AMsg.Position := 0;
    
      LMsg := TIdMessage.Create; 
      Try 
        //LMsg.LoadFromFile(ExtractFilePath(Application.exename) + 'test.eml', False); 
    
        // Do not use the TIdMessage.LoadFrom...() methods here! The email
        // data contained in the AMsg stream does not escape leading periods
        // on lines of text. That escaping is only used when the email was
        // transmitted over the socket, as a period character has special
        // meaning to the SMTP protocol.  TIdSMTPServer removes the escaping
        // from the data before firing this event. However, the LoadFrom...()
        // methods (which uses TIdMessageClient internally) require the
        // data to be escaped!  This is a known limitation in Indy's core
        // architecture and will be addressed in Indy 11.  Until then,
        // use TIdMessageClient manually so the necessary escaping can be
        // re-introduced into the data while it is being parsed...
    
        LMsgClient := TIdMessageClient.Create(nil);
        try
          LMsgClient.IOHandler := TIdIOHandlerStreamMsg.Create(LMsgClient, AMsg);
          with TIdIOHandlerStreamMsg(LMsgClient.IOHandler) do
          begin
            FreeStreams := False;
            EscapeLines := True;
          end;
          LMsgClient.IOHandler.Open;
          LMsgClient.ProcessMessage(LMsg, False);
        finally
          LMsgClient.Free;
        end;
    
        // process LMsg as needed...
    
        // save the email for local users and forward it to other SMTP servers as needed...
    
        LRelayRecipients := nil;
        try
          for I := 0 to ASender.RCPTList.Count-1 do
          begin
            if (ASender.RCPTList[I] represents a user in your network) then
            begin
              // save AMsg in the user's mailbox somewhere on the local
              // machine/network where an POP3/IMAP4 client can retreive
              // it from later...
            end else
            begin
              if not Assigned(LRelayRecipients) then LRelayRecipients := TIdEMailAddressList.Create(nil);
              LRelayRecipients.Add.Assign(ASender.RCPTList[I]);
            end;
          end;
    
          if Assigned(LRelayRecipients) then
          begin
            LRelay := TIdSMTPRelay.Create(nil);
            try
              // you must supply the IP/Host of a DNS server that
              // will be used for determining the SMTP server of
              // each recipient domain...
              LRelay.DNSServer := ...;
    
              LRelay.Send(LMsg, LRelayRecipients);
            finally
              LRelay.Free;
            end;
          end;
        finally
          LRelayRecipients.Free;
        end;
    
      Finally 
        LMsg.Free; 
      End; 
    end; 
    
    0 讨论(0)
提交回复
热议问题