【问题标题】:Why body and sender are empty?为什么 body 和 sender 都是空的?
【发布时间】:2020-07-19 21:22:56
【问题描述】:

我正在使用此代码从服务器读取电子邮件,它可以工作,除了 Sender.AddressBody.Text 是空的,为什么会这样?。代码如下:

var
  MsgCount : Integer;
  i        : Integer;
  FMailMessage :  TIdMessage;
begin
  Memo1.Lines.Clear;
  //The IdPop31 is on the form so it is constructing when the
  //form is created and so is Memo1.
  IdPOP31.Host      := 'server.com'; //Setting the HostName;
  IdPOP31.Username  := 'email@server.com';//Setting UserName;
  IdPOP31.Password  := 'xxxxxx';//Setting Password;
  IdPOP31.Port      := 110;//Setting Port;

  try
    IdPOP31.Connect();
    //Getting the number of the messages that server has.
    MsgCount := IdPOP31.CheckMessages;
    for i:= 1 to Pred(MsgCount) do
    begin
      try
        FMailMessage := TIdMessage.Create(nil);
        IdPOP31.Retrieve(i,FMailMessage);
        Memo1.Lines.Add('=================================================');
        Memo1.Lines.Add(FMailMessage.From.Address);
        Memo1.Lines.Add(FMailMessage.Recipients.EMailAddresses);
        Memo1.Lines.Add(FMailMessage.Subject);
        Memo1.Lines.Add(FMailMessage.Sender.Address);
        Memo1.Lines.Add(FMailMessage.Body.Text);
        Memo1.Lines.Add('=================================================');
      finally
        FMailMessage.Free;
      end;
    end;
  finally
    IdPOP31.Disconnect;
  end;
end;

【问题讨论】:

    标签: delphi indy indy10


    【解决方案1】:

    仅当电子邮件具有顶级 Sender 标头时才会填充 TIdMessage.Sender,这种情况很少见。通常,发件人位于 From 标头中。

    正文内容将存储在TIdMessage.BodyTIdMessage.MessageParts 中,具体取决于电子邮件的编码方式。通常,多件电子邮件(例如使用 MIME 编码的电子邮件,尤其是包含附件的电子邮件)将使用 TIdMessage.MessageParts,而简单电子邮件(例如纯文本电子邮件)将使用 TIdMessage.Body。因此,您需要根据需要检查两者。

    例如:

    var
      MsgCount, I: Integer;
      FMailMessage: TIdMessage;
      Body: TStrings;
    
      function FindTextBody(AParent: Integer): TStrings;
      var
        J: integer;
        Part: TIdMessagePart;
      begin
        Result := nil;
        // MIME parts are ordered from least complex to most complex, and can be nested,
        // so loop backwards through the parts, recursing through nested levels as needed...
        for J := Pred(FMailMessage.MessageParts.Count) downto (AParent+1) do
        begin
          Part := FMailMessage.MessageParts[J];
          if Part.ParentPart = AParent then
          begin
            if IsHeaderMediaType(Part.ContentType, 'multipart') then
            begin
              Result := FindTextBody(Part.Index);
              if Result <> nil then Exit;
            end
            else if IsHeaderMediaType(Part.ContentType, 'text') then
            begin
              Result := (Part as TIdText).Body;
              Exit;
            end;
          end;
        end;
      end;
    
    begin
      Memo1.Lines.Clear;
      //The IdPop31 is on the form so it is constructing when the
      //form is created and so is Memo1.
      IdPOP31.Host      := 'server.com'; //Setting the HostName;
      IdPOP31.Username  := 'email@server.com';//Setting UserName;
      IdPOP31.Password  := 'xxxxxx';//Setting Password;
      IdPOP31.Port      := 110;//Setting Port;
      try
        IdPOP31.Connect();
        //Getting the number of the messages that server has.
        MsgCount := IdPOP31.CheckMessages;
        for I := 1 to Pred(MsgCount) do
        begin
          FMailMessage := TIdMessage.Create(nil);
          try
            IdPOP31.Retrieve(I, FMailMessage);
            Memo1.Lines.Add('=================================================');
            Memo1.Lines.Add(FMailMessage.From.Address);
            Memo1.Lines.Add(FMailMessage.Recipients.EMailAddresses);
            Memo1.Lines.Add(FMailMessage.Subject);
            Memo1.Lines.Add(FMailMessage.Sender.Address);
            if FMailMessage.MessageParts.Count > 0 then
              Body := FindTextBody(-1)
            else
              Body := FMailMessage.Body;
            if Body <> nil then
              Memo1.Lines.Add(Body.Text);
            Memo1.Lines.Add('=================================================');
          finally
            FMailMessage.Free;
          end;
        end;
      finally
        IdPOP31.Disconnect;
      end;
    end;
    

    【讨论】:

    • 我试着这样做 lTextPart := TIdText.Create(FMailMessage.MessageParts); Memo1.Lines.Add(lTextPart.Body.text); 但我仍然没有得到正文它是空的
    • @Wel 在阅读电子邮件时,您不应该创建新的MessageParts 项目。您应该通过现有迭代,寻找您感兴趣的一项,即ContentType'text/...'开头的一项('text/plain''text/html' 等)。 MIME 电子邮件的结构可能非常复杂。例如看HTML Messages
    • 这是我正在阅读的页面,但它没有任何示例如何遍历 items 所以当我遍历 items[i] 并找到内容类型文本时,我可以使用什么属性来阅读文字?
    • 我只是研究了聪明的互联网组件,它运行良好,没有 indy 并发症
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-27
    • 1970-01-01
    • 2016-02-06
    • 2022-06-16
    • 2021-08-21
    相关资源
    最近更新 更多