var
  i:integer;
  MailCount:integer;
  idmsg:TIdMessage;
  tmpFileStm:TFileStream;
  tmpStm:TStream;
  tmpfile:TIdAttachment;
  tmpIDMsgParts:TIdMessageParts;
begin
try
  try
    idMsg:=TIdMessage.Create(nil);
    idMsg.Clear;
    idPOP31.Host:=edServer.Text;
    idPOP31.Port:=StrToInt(edPort.text);
    idPOP31.UserID:=edUserID.Text;
    idPOP31.Password:=edPasswd.Text;
    idPOP31.Connect;
    MailCount:=idPOP31.CheckMessages;
    Label6.Caption:=inttostr(MailCount);
    for i:=1 to MailCount do
    begin
      if not idPOP31.Retrieve(i,idmsg) then
        Showmessage('error');
      tmpIDMsgParts:=idMsg.MessageParts;
      tmpFile:=TIdAttachment.Create(tmpIDMsgParts);
      if not tmpFIle.SaveToFile('c:\test.jpg') then
        showmessage('save error');
    end;  except
    Showmessage('Error');
  end;
finally
  idMsg.Free;
  idPOP31.Disconnect;
end
这样在SaveFile的时候老是出错,别的信息象body subject都正确,为什么就是附件存不了?肯定有附件

解决方案 »

  1.   

    1、function Retrieve(const MsgNum: Integer; AMsg: TIdMessage): Boolean;
    获取信息
    2、遍历判断TIdMessage.MessageParts.Items中的每一项看是不是TIdAttachment类,如果是就TIdAttachment(TIdMessage.MessageParts.Items[x]).SaveToFile方法去保存
      

  2.   

    tmpIDMsgParts:=idMsg.MessageParts;
          tmpFile:=TIdAttachment.Create(tmpIDMsgParts);
          if not tmpFIle.SaveToFile('c:\test.jpg') then
            showmessage('save error');
    这一段可能有问题用(认真去想,你就会知道,一个邮件不只是有一个附件,可能有多个,而且IdPop3将正文内容也列入MessageParts中)
    for MsgPartIndex :=0 to idMsg.MessageParts.Items.Count do
    begin
      if (idMsg.MessageParts.Items[MsgPartIndex] is TIdAttachment) then
      begin{是附件}
        (idMsg.MessageParts.Items[MsgPartIndex] as TIdAttachment).SaveToFile('AT'+IntToStr(MsgPartIndex)+'-'+SaveFileName);
      end
      else begin{如果不是应该就是属于文本型一类的TIdText}
      end;
    end;