收邮件 下载附件

uses
  IdMessage, IdMessageParts, IdAttachment, IdGlobalProtocols, ...;

procedure SaveAttachmentsFromFile(FileName: String)
var    
  IdMessage: TIdMessage;
  MsgPart: TidMessagePart;
  HeaderMediaType: String;
  AttachtmentFileName: String
begin
  // Create Message
  IdMessage := TIdMessage.Create(Self);
  try
    // Load eml file
    IdMessage.LoadFromFile(FileName);
    // Get Header Media Type
    HeaderMediaType := ExtractHeaderMediaType(IdMessage.ContentType);
    // Multipart
    if HeaderMediaType = 'multipart' then begin
      for I := IdMessage.MessageParts.Count-1 downto 0 do begin
        MsgPart := IdMessage.MessageParts[I];
        if (MsgPart is TIdAttachment) then begin
          // Save File
          AttachtmentFileName:= TidAttachment(MsgPart).FileName;
          TidAttachment(MsgPart).SaveToFile(AttachtmentFileName);
        end;
      end;
    end;
  finally
    IdMessage.Destroy;
  end;
end
相关文章
相关标签/搜索