delphi - 使用 INDY SMTP 服务器

标签 delphi indy

以下代码是我对示例进行修改(与 10.5.8 一起使用)的代码,以便使用 Indy Mail server 中描述的 smtp 客户端独立发送电子邮件

以下内容来自 INDY smtpserver 示例

procedure TForm1.btnServerOnClick(Sender: TObject);
 begin

 IdSMTPServer1.active := true;
end;

procedure TForm1.btnServerOffClick(Sender: TObject);
begin

 IdSMTPServer1.active := false;
end;

procedure TForm1.IdSMTPServer1MailFrom(ASender: TIdSMTPServerContext;
  const AAddress: string; AParams: TStrings; var VAction: TIdMailFromReply);
begin
// Here we are testing the MAIL FROM line sent to the server.
 // MAIL FROM address comes in via AAddress. VAction sets the return action to the server.

 // The following actions can be returned to the server:
 { mAccept, mReject }

 // For now, we will just always allow the mail from address.
 VAction := mAccept;
end;

procedure TForm1.IdSMTPServer1MsgReceive(ASender: TIdSMTPServerContext;
  AMsg: TStream; var LAction: TIdDataReply);
var
 LMsg : TIdMessage;
 LStream : TFileStream;
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 the load it using
// IdMessage and parse some header elements.

LStream := TFileStream.Create(ExtractFilePath(Application.exename) + 'test.eml', fmCreate);
Try
 LStream.CopyFrom(AMsg, 0);
Finally
 FreeAndNil(LStream);
End;

LMsg := TIdMessage.Create;
Try
 LMsg.LoadFromFile(ExtractFilePath(Application.exename) + 'test.eml', False);
 ToLabel.Caption := LMsg.Recipients.EMailAddresses;
 FromLabel.Caption := LMsg.From.Text;
 SubjectLabel.Caption := LMsg.Subject;
 Memo1.Lines := LMsg.Body;
Finally
 FreeAndNil(LMsg);
End;

end;


procedure TForm1.IdSMTPServer1UserLogin(ASender: TIdSMTPServerContext;
  const AUsername, APassword: String; var VAuthenticated: Boolean);
begin
 // This event is fired if a user attempts to login to the server
 // Normally used to grant relay access to specific users etc.
 VAuthenticated := True;
end;



procedure TForm1.IdSMTPServer1RcptTo(ASender: TIdSMTPServerContext;
  const AAddress: string; AParams: TStrings; var VAction: TIdRCPToReply;
  var VForward: string);
begin
   VAction := rAddressOk;    
end;

procedure TForm1.IdSMTPServer1Received(ASender: TIdSMTPServerContext;
  var AReceived: string);
begin
//
end;

我必须向该项目添加什么才能用作电子邮件发送服务器。

我没有对原始示例进行太多更改(仅出于兼容性目的),因为我无法理解如何发送和接收电子邮件背后的大多数概念。请解释一下如何制作此代码以将电子邮件发送到此问题 Indy Mail server 中给出的目的地(例如:someone@gmail.com) (smtp客户端),

最佳答案

要将电子邮件传递到另一个系统(本例中为 Gmail),请使用 TIdSMTPRelay 组件,例如:

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; 

关于delphi - 使用 INDY SMTP 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8499524/

相关文章:

delphi - 发送附有内存流的电子邮件

delphi - ADOConnection字符串的确切特定部分

database - 如何从 TDBAdvListView 中删除选定的数据库记录?

delphi - 为什么 RAD Studio Seattle 10 安装 Windows 10 SDK? (它可以与较新的 SDK 一起使用吗?)

delphi - 如何使用 TIdHTTP 中的字符串创建 IHTMLDocument2?

delphi - 在 delphi 中执行一个 curl 代码以连接到 stripe payment

delphi - 为什么在某些情况下使用 idSMTP 发送的电子邮件无法正确转至新行?

delphi - Delphi 的免费软件数据透视表组件?

delphi - clDefault 颜色如何工作?

Delphi Indy - 如何为 Indy 10 的 SSL-TCP 客户端/服务器链接获取 SSL 证书