哪位大侠指点一下,将收到的邮件保存一个文件中,包括html格式的Body在内(300,还可加分)(300分)

  • 主题发起人 主题发起人 yue_shan
  • 开始时间 开始时间
Y

yue_shan

Unregistered / Unconfirmed
GUEST, unregistred user!
我想这样做,但不行,您给看看:
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComObj,AxCtrls, ActiveX;

type
TEMailItem = class(TCollectionItem)
private
FMsgID: integer;
FBody: TFileStream;
public
procedure Assign(Source: TPersistent);override;
published
property MsgID: integer read FMsgID write FMsgID;
property Body: TFileStream read FBody write FBody;
end;

TEMailItems = class(TCollection)
public
constructor Create;
function Add: TEMailItem;
function GetItem(Index: Integer): TEMailItem;
procedure SetItem(Index: Integer;Value: TEMailItem);
property Items[Index: Integer]: TEMailItem read GetItem write SetItem;
end;

TEMailDummy = class(TComponent)
private
FItems: TEMailItems;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy;override;
published
property Items: TEMailItems read FItems write FItems;
end;

TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

{ TEMailItem }

procedure TEMailItem.Assign(Source: TPersistent);
begin
if Source is TEMailItem then
begin
FMsgID := TEMailItem(Source).FMsgID;
FBody := TEMailItem(Source).FBody;
end else
Inherited Assign(Source);
end;

{ TEMailItems }

function TEMailItems.Add: TEMailItem;
begin
Result := inherited Add as TEMailItem;
end;

constructor TEMailItems.Create;
begin
inherited Create(TEMailItem);
end;

function TEMailItems.GetItem(Index: Integer): TEMailItem;
begin
Result := inherited GetItem(Index) as TEMailItem;
end;

procedure TEMailItems.SetItem(Index: Integer; Value: TEMailItem);
begin
inherited SetItem(Index,Value);
end;

{ TEMailDummy }

constructor TEMailDummy.Create(AOwner: TComponent);
begin
FItems := TEMailItems.Create;
end;

destructor TEMailDummy.Destroy;
begin
FItems.Free
end;


procedure TForm1.Button1Click(Sender: TObject);
var
FEmailItem: TEMailItem;
FEmailItems: TEmailItems;

F: TFileStream;
FName: WideString;
FRootStorage: IStorage;

stm: IStream;
OS: TOleStream;
Dummy: TEMailDummy;
buf: array[0..1024] of char;
begin
FEmailItems := TEMailItems.Create;
FName := ExtractFileDir(Application.ExeName)+'/aa.html';
F := TFileStream.Create(FName,fmOpenRead);
F.WriteBuffer(buf,sizeof(F));
FEMailItem := FEmailItems.Add;
FEMailItem.MsgID := 100;
FEMailItem.Body.ReadBuffer(buf,sizeof(f));

FName := ExtractFileDir(Application.ExeName)+'/aa.stm';
if not SUCCEEDED(StgOpenStorage(PWideChar(FName),nil,STGM_READWRITE or STGM_SHARE_eXCLUSIVE,nil,0,FRootStorage)) then
OleCheck(StgCreateDocFile(PWideChar(FName),STGM_CREATE or STGM_READWRITE or STGM_SHARE_EXCLUSIVE,0,FRootStorage));
if not SUCCEEDED(FRootStorage.OpenStream('Accounts',nil,STGM_CREATE or STGM_WRITE or STGM_SHARE_EXCLUSIVE,0,stm)) then
OleCheck(FRootStorage.CreateStream('Accounts',STGM_CREATE or STGM_WRITE or STGM_SHARE_EXCLUSIVE,0,0,stm));
Dummy := TEMailDummy.Create(nil);
try
Dummy.Items.Assign(FEmailItems);
OS := TOleStream.Create(stm);
try
OS.WriteComponent(Dummy);
finally
OS.Free;
end;
finally
Dummy.Free;
end;

FEmailItems.Free;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
FEmailItem: TEMailItem;
FEmailItems: TEmailItems;

F: TFileStream;
FName: WideString;
FRootStorage: IStorage;

stm: IStream;
OS: TOleStream;
Dummy: TEMailDummy;
i: integer;
buf: array[0..124] of char;
begin
FEmailItems := TEMailItems.Create;
FName := ExtractFileDir(Application.ExeName)+'/aa.html';

FName := ExtractFileDir(Application.ExeName)+'/aa.stm';
if SUCCEEDED(StgOpenStorage(PWideChar(FName),nil,STGM_READ or STGM_SHARE_eXCLUSIVE,nil,0,FRootStorage)) then
if SUCCEEDED(FRootStorage.OpenStream('Accounts',nil,STGM_READ or STGM_SHARE_EXCLUSIVE,0,stm)) then
begin
Dummy := TEMailDummy.Create(nil);
try
OS := TOleStream.Create(stm);
try
OS.ReadComponent(Dummy);
FEmailItems.Assign(Dummy.Items);
finally
OS.Free;
end;
finally
Dummy.Free;
end;
end;

FName := ExtractFileDir(Application.ExeName)+'/bb.html';
F := TFileStream.Create(FName,fmCreate);
for i:=0 to FEmailItems.Count-1 do
begin
FEmailItem := FEmailItems.Items;
FEMailItem.Body.Write(buf,sizeof(FEMailItem.Body));
F.Read(buf,sizeof(FEMailItem.Body))
// F. := FEMailItem.Body.Create(FName,fmCreate);
end;
F.Free;
FEmailItems.Free;
end;

end.
 
高手帮忙啦
 
来看了,帮提前一下,我自己都提到肩酸啦,谢了
 
F := TFileStream.Create(FName,fmOpenRead);只读方式打开
F.WriteBuffer(buf,sizeof(F));怎么能写入。sizeof(F)是什么?
sizeof(f)返回4,f对象是指针,所以返回4。感觉不对路子。
 
我对这不是很熟,只是胡乱试,我也用过fmOpenwrite,也不行,老大,给看看吧
 
问题很多
TEMailItem = class(TCollectionItem)
private
FMsgID: integer;
FBody: TFileStream;//感觉不应该是Tfilestream是Tmemorystream。
因为根本不知道文件名在哪。
public
procedure Assign(Source: TPersistent);override;
//没有构造,fbody为空,引用会出错。
published
property MsgID: integer read FMsgID write FMsgID;
property Body: TFileStream read FBody write FBody;//直接用write FBody会有问题
要用setBody过程设置FBody的值。
end;

F := TFileStream.Create(FName,fmOpenRead);
F.WriteBuffer(buf,sizeof(F));
FEMailItem := FEmailItems.Add;
FEMailItem.MsgID := 100;
FEMailItem.Body.ReadBuffer(buf,sizeof(f));//不需要如此繁琐 F.poistion := 0;
用FEmailitem.body.copyfrom(f,f.size)即可。

try
OS.WriteComponent(Dummy);//body的数据不会写入文件
finally
OS.Free;
end;

换个方式吧这样作是不行的。
可以把msgid和html按照一定格式保存在一个文件流中存成文件。
读出时按照格式读出msgid和html。
 
问题虽没有解决,但是还是很感谢您的指点,结账了,老板,埋单
 
后退
顶部