ufrmViewEmail.pas
上传用户:yjb1804
上传日期:2021-01-30
资源大小:3105k
文件大小:11k
源码类别:

Email服务器

开发平台:

Delphi

  1. unit ufrmViewEmail;
  2. interface
  3. uses
  4.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  5.   Dialogs, uBaseEditorForm, ActnList, uHtmlEdit, dxBar, ImgList,
  6.   dxBarExtItems, cxClasses, OleCtrls, SHDocVw, ExtCtrls, cxGraphics,
  7.   cxControls, dxStatusBar, cxContainer, cxEdit, cxLabel, cxStyles,
  8.   cxCustomData, cxFilter, cxData, cxDataStorage, cxGridCustomView,
  9.   cxGridCustomTableView, cxGridTableView, cxGridLevel, cxGrid, dxSkinsCore,
  10.   dxSkinsdxBarPainter, dxSkinBlack, dxSkinBlue, dxSkinCaramel,
  11.   dxSkinCoffee, {dxSkinDarkRoom,} dxSkinDarkSide, {dxSkinFoggy,}
  12.   dxSkinGlassOceans, dxSkiniMaginary, dxSkinLilian, dxSkinLiquidSky,
  13.   dxSkinLondonLiquidSky, dxSkinMcSkin, dxSkinMoneyTwins,
  14.   dxSkinOffice2007Black, dxSkinOffice2007Blue, dxSkinOffice2007Green,
  15.   dxSkinOffice2007Pink, dxSkinOffice2007Silver, dxSkinPumpkin, {dxSkinSeven,}
  16.   {dxSkinSharp,} dxSkinSilver, {dxSkinSpringTime,} dxSkinStardust,
  17.   dxSkinSummer2008, dxSkinsDefaultPainters, dxSkinValentine,
  18.   dxSkinXmas2008Blue, dxSkinsdxStatusBarPainter;
  19. type
  20.   TfrmViewEmail = class(TBaseEditorForm)
  21.     pnl1: TPanel;
  22.     dxStatusBar1: TdxStatusBar;
  23.     dxbrsbtm4: TdxBarSubItem;
  24.     dxbrsbtm5: TdxBarSubItem;
  25.     dxbrsbtm6: TdxBarSubItem;
  26.     btn1: TdxBarButton;
  27.     btn2: TdxBarButton;
  28.     btn3: TdxBarButton;
  29.     btn4: TdxBarButton;
  30.     btn5: TdxBarButton;
  31.     btn6: TdxBarButton;
  32.     btn7: TdxBarButton;
  33.     btn8: TdxBarButton;
  34.     btn9: TdxBarButton;
  35.     btnNextUnread: TdxBarButton;
  36.     btn11: TdxBarButton;
  37.     btn12: TdxBarButton;
  38.     btn13: TdxBarButton;
  39.     dxbrsbtm7: TdxBarSubItem;
  40.     btn14: TdxBarButton;
  41.     btn15: TdxBarButton;
  42.     btn16: TdxBarButton;
  43.     btn17: TdxBarButton;
  44.     btn18: TdxBarButton;
  45.     btn19: TdxBarButton;
  46.     btn20: TdxBarButton;
  47.     btn21: TdxBarButton;
  48.     btn22: TdxBarButton;
  49.     btn23: TdxBarButton;
  50.     btn24: TdxBarButton;
  51.     lbl2: TcxLabel;
  52.     lbl3: TcxLabel;
  53.     lbl4: TcxLabel;
  54.     lbl5: TcxLabel;
  55.     lblSender: TcxLabel;
  56.     lblRecver: TcxLabel;
  57.     lblRecvDate: TcxLabel;
  58.     lblSubject: TcxLabel;
  59.     btnRecp: TdxBarLargeButton;
  60.     btnPrior: TdxBarLargeButton;
  61.     btnTurn: TdxBarLargeButton;
  62.     btnSucc: TdxBarLargeButton;
  63.     btnDeleteEmail: TdxBarLargeButton;
  64.     btnPreview: TdxBarLargeButton;
  65.     btnPrint: TdxBarLargeButton;
  66.     btnExit: TdxBarLargeButton;
  67.     procedure FormCreate(Sender: TObject);
  68.     procedure FormDestroy(Sender: TObject);
  69.     procedure btnRecpClick(Sender: TObject);
  70.     procedure btnTurnClick(Sender: TObject);
  71.     procedure btnDeleteEmailClick(Sender: TObject);
  72.     procedure btnPriorClick(Sender: TObject);
  73.     procedure btnSuccClick(Sender: TObject);
  74.     procedure btnPreviewClick(Sender: TObject);
  75.     procedure btnPrintClick(Sender: TObject);
  76.     procedure btnExitClick(Sender: TObject);
  77.     procedure btn1Click(Sender: TObject);
  78.     procedure FormClose(Sender: TObject; var Action: TCloseAction);
  79.     procedure btnNextUnreadClick(Sender: TObject);
  80.     procedure btn6Click(Sender: TObject);
  81.     procedure btn9Click(Sender: TObject);
  82.     procedure btn11Click(Sender: TObject);
  83.   private
  84.     { Private declarations }
  85.     FAttchs:TStrings;
  86.     FOneEmail:Boolean;//只显示一封邮件
  87.   public
  88.     { Public declarations }
  89.     FTableView:TcxGridTableView; //删除邮件时,在创建窗体之后,必须设置这5个值
  90.     FCurRecordIndex,
  91.     FContentFileItemIndex:Integer;
  92.     FRecverEmail,    //接收者
  93.     FSenderEmail:string;     //发送者
  94.     FEmailAddr,
  95.     FContentFilePath:string;
  96.     procedure ShowEmail;
  97.     procedure ShowOneEmail;
  98.     {调用方法:
  99.     1,创建窗体  Create.
  100.     2,设置以下变量的值
  101.         FTableView:TcxGridTableView;  //从那个Grid中读取数据
  102.         FCurRecordIndex,              //记录集当前记录位置
  103.         FContentFileItemIndex,        //    email保存位置在记录集的第几个字段.
  104.         FRecverEMailAddrItemIndex:Integer;  //   email地址在记录集的第几个字段.
  105.         FSenderEmail:string;
  106.     3,调用 ShowEmail
  107.     }
  108.     { 显示一封邮件时的调用方法:
  109.       1,设置FEmailAddr,
  110.       2,设置FContentFilePath:string;
  111.       3,调用ShowOneEmail;
  112.     }
  113.   end;
  114. var
  115.   frmViewEmail: TfrmViewEmail;
  116. implementation
  117. uses UEmailFile, ufrmWriteEmail, uMyXml, uCommon, cxCheckListBox;
  118. {$R *.dfm}
  119. { TfrmViewEmail }
  120. procedure TfrmViewEmail.ShowEmail;
  121. var
  122.   EMail:TEMailFile;
  123. begin
  124.   if (FCurRecordIndex<0) then
  125.     FCurRecordIndex:=FTableView.DataController.RecordCount-1 ;
  126.   if FCurRecordIndex>FTableView.DataController.RecordCount-1 then
  127.      FCurRecordIndex:=0;
  128.   btnNextUnread.Enabled:=FCurRecordIndex<FTableView.DataController.RecordCount-1;
  129.   btnPrior.Enabled:=FCurRecordIndex>0;
  130.   btnSucc.Enabled:=FCurRecordIndex<FTableView.DataController.RecordCount-1;
  131.   btnNextUnread.Enabled:=FCurRecordIndex<FTableView.DataController.RecordCount-1;
  132.   
  133.   FContentFilePath:=VarToStr(FTableView.DataController.Values[FCurRecordIndex,FContentFileItemIndex]);
  134.   EMail:=TEMailFile.Create(FContentFilePath);
  135.   try
  136.     EMail.GetEmail;
  137.     lblSender.Caption:=Email.Sender;
  138.     lblRecver.Caption:=FRecverEmail;
  139.     lblRecvDate.Caption:=Email.Date;
  140.     lblSubject.Caption:=Email.Subject;
  141.     HtmlEdit.Clear;
  142.     HtmlEdit.Insert(Email.Content);
  143.     FAttchs.Assign(EMail.Attchs);
  144.   finally
  145.     EMail.Free;
  146.   end;
  147. end;
  148. procedure TfrmViewEmail.FormCreate(Sender: TObject);
  149. begin
  150.   inherited;
  151.   FOneEmail:=false;
  152.   FAttchs:=TStringList.Create;
  153. end;
  154. procedure TfrmViewEmail.FormDestroy(Sender: TObject);
  155. begin
  156.   FAttchs.Free;
  157.   inherited;
  158. end;
  159. procedure TfrmViewEmail.btnRecpClick(Sender: TObject);
  160. var
  161.   Email:TEmailFile;
  162.   idx:Integer;
  163. begin
  164.   if FOneEmail then
  165.   begin
  166.     with TfrmWriteEMail.Create(Application) do
  167.     begin
  168.       try
  169.         Caption:='回复邮件';
  170.         Email:=TEmailFile.Create(FContentFilePath);
  171.         try
  172.           Email.GetEmail;
  173.         finally
  174.           idx:=cbSender.Properties.Items.IndexOf(Email.Recver);
  175.           if idx>=0 then
  176.             cbSender.ItemIndex:=idx  //cbSender.Properties.Items.IndexOf(FEmailAddr)
  177.           else
  178.             cbSender.Properties.Items.Insert(0,Email.Recver);
  179.           idx:=cbRecver.Properties.Items.IndexOf(Email.Sender) ;
  180.           if idx>=0 then
  181.              cbRecver.ItemIndex:=idx
  182.             //cbRecver.Properties.Items.Delete(cbRecver.Properties.Items.IndexOf(Email.Sender));
  183.           else
  184.             cbRecver.Properties.Items.Insert(0,Email.Sender);
  185.           //cbRecver.ItemIndex:=0;
  186.         end;
  187.         ShowModal;
  188.       finally
  189.         Free;
  190.       end;
  191.     end;
  192.     Exit;
  193.   end;
  194.   with TfrmWriteEMail.Create(Application) do
  195.   try
  196.     Caption:='回复邮件';
  197.     with FTableView.DataController do
  198.     begin
  199.       cbRecver.Properties.Items.Insert(0,FSenderEmail) ;
  200.       if cbSender.Properties.Items.IndexOf(FRecverEmail)<>-1 then
  201.         cbSender.ItemIndex:=cbSender.Properties.Items.IndexOf(FRecverEmail);
  202.     end;
  203.     cbRecver.ItemIndex:=0;
  204.     ShowModal;
  205.   finally
  206.     Free;
  207.   end;
  208. end;
  209. procedure TfrmViewEmail.btnTurnClick(Sender: TObject);
  210. var
  211.   EmailFile:TEmailFile;
  212.   Path:string;
  213.   I:Integer;
  214. begin
  215.   if FOneEmail then
  216.     Path:=FContentFilePath
  217.   else
  218.   begin
  219.     if not FileExists(VarToStr(FTableView.DataController.Values[FCurRecordIndex,FContentFileItemIndex])) then exit;
  220.     Path:=VarToStr(FTableView.DataController.Values[FCurRecordIndex,FContentFileItemIndex]);
  221.   end;
  222.   with TfrmWriteEMail.Create(Application) do
  223.   try
  224.     Caption:='转发邮件';
  225.     FSetDoc:=True;
  226.     pnl1.Height:=234;
  227.     
  228.     EmailFile:=TEmailFile.Create(Path);
  229.     try
  230.       EmailFile.GetEmail;
  231.       for I:=0 to EmailFile.Attchs.Count-1 do
  232.       begin
  233.         with lstAttch.Items.Add do
  234.         begin
  235.           Text:=EmailFile.Attchs[I];
  236.           Checked:=True;
  237.         end;
  238.       end;
  239.       //cbAttch.Properties.Items.Assign(EmailFile.Attchs);
  240.       if FOneEmail then
  241.       begin
  242.         if  cbSender.Properties.Items.IndexOf(FEmailAddr)<>-1 then
  243.           cbSender.ItemIndex:=cbSender.Properties.Items.IndexOf(FEmailAddr);
  244.         cbSender.ItemIndex:=0;
  245.       end;
  246.       FStr:=EmailFile.Content;
  247.     finally
  248.       EmailFile.Free;
  249.     end;
  250.     ShowModal;
  251.   finally
  252.     Free;
  253.   end;
  254. end;
  255. procedure TfrmViewEmail.btnDeleteEmailClick(Sender: TObject);
  256. var
  257.   xml:TAppXml;
  258. begin
  259.   if MessageDlg('你确定要删除当前邮件吗?' + #13#10#13#10#13#10#13#10 +
  260.     #13#10,  mtConfirmation, mbOKCancel, 0)=mrCancel then Exit;
  261.   xml:=TAppXml.Create;
  262.   try
  263.     if FOneEmail then
  264.     begin
  265.       xml.DeleteEmailAwake(FEmailAddr,FContentFilePath);
  266.       MsgBoxWarn('删除成功!');
  267.     end
  268.     else
  269.     begin
  270.       if xml.DeleteAEmail(FRecverEmail,VarToStr(FTableView.DataController.Values[FCurRecordIndex,FContentFileItemIndex])) then
  271.         MsgBoxWarn('删除成功!')
  272.       else
  273.         MsgBoxError('无法删除邮件!');
  274.     end;
  275.   finally
  276.     xml.Free;
  277.   end;
  278.   lblSender.Caption:='';
  279.   lblRecver.Caption:='';
  280.   lblRecvDate.Caption:='';
  281.   lblSubject.Caption:='';
  282.   HtmlEdit.Clear;
  283.   if not FOneEmail then
  284.   begin
  285.     FTableView.DataController.DeleteRecord(FCurRecordIndex);
  286.     btnSuccClick(nil);
  287.   end;
  288. end;
  289. procedure TfrmViewEmail.btnPriorClick(Sender: TObject);
  290. begin
  291.   Dec(FCurRecordIndex);
  292.   ShowEmail;
  293. end;
  294. procedure TfrmViewEmail.btnSuccClick(Sender: TObject);
  295. begin
  296.   Inc(FCurRecordIndex);
  297.   ShowEmail;
  298. end;
  299. procedure TfrmViewEmail.btnPreviewClick(Sender: TObject);
  300. begin
  301.   HtmlEdit.PrintPageSetup;
  302.   HtmlEdit.PrintPreview;
  303. end;
  304. procedure TfrmViewEmail.btnPrintClick(Sender: TObject);
  305. begin
  306.   HtmlEdit.PrintPageSetup;
  307.   HtmlEdit.Print;
  308. end;
  309. procedure TfrmViewEmail.btnExitClick(Sender: TObject);
  310. begin
  311.   close;
  312. end;
  313. procedure TfrmViewEmail.btn1Click(Sender: TObject);
  314. begin
  315.   if dlgSave.Execute then
  316.     HtmlEdit.SaveToFile(dlgSave.FileName);
  317. end;
  318. procedure TfrmViewEmail.FormClose(Sender: TObject;
  319.   var Action: TCloseAction);
  320. begin
  321.   Action:=caFree;
  322. end;
  323. procedure TfrmViewEmail.btnNextUnreadClick(Sender: TObject);
  324. begin
  325.   repeat
  326.     Inc(FCurRecordIndex);
  327.     if  FCurRecordIndex>=FTableView.DataController.RecordCount then
  328.     begin
  329.       MsgBoxWarn('没有未读邮件');
  330.       exit;
  331.     end;
  332.   until (FTableView.DataController.Values[FCurRecordIndex,0]=false);
  333.   ShowEmail;
  334. end;
  335. procedure TfrmViewEmail.btn6Click(Sender: TObject);
  336. begin
  337.   dxbrmngr1Bar1.visible:=btn6.Down;
  338. end;
  339. procedure TfrmViewEmail.btn9Click(Sender: TObject);
  340. begin
  341.   dxStatusBar1.visible:=btn9.Down;
  342. end;
  343. procedure TfrmViewEmail.btn11Click(Sender: TObject);
  344. begin
  345.   pnl1.visible:=btn11.Down;
  346. end;
  347. procedure TfrmViewEmail.ShowOneEmail;
  348. var
  349.   EMail:TEMailFile;
  350. begin
  351.   FOneEmail:=True;
  352.   btn12.Visible:=ivNever;
  353.   btn13.Visible:=ivNever;
  354.   btnNextUnread.Visible:=ivNever;
  355.   btnPrior.Visible:=ivNever;
  356.   btnSucc.Visible:=ivNever;
  357.   EMail:=TEMailFile.Create(VarToStr(FTableView.DataController.Values[FCurRecordIndex,FContentFileItemIndex]));
  358.   try
  359.     EMail.GetEmail;
  360.     lblSender.Caption:=Email.Sender;
  361.     lblRecver.Caption:=FEmailAddr;
  362.     lblRecvDate.Caption:=Email.Date;
  363.     lblSubject.Caption:=Email.Subject;
  364.     HtmlEdit.Clear;
  365.     HtmlEdit.Insert(Email.Content);
  366.     FAttchs.Assign(EMail.Attchs);
  367.   finally
  368.     EMail.Free;
  369.   end;
  370. end;
  371. end.