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

Email服务器

开发平台:

Delphi

  1. unit ufrmWriteEmail;
  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.   cxMRUEdit, cxLabel, cxTextEdit, cxControls, cxContainer, cxEdit,
  8.   cxMaskEdit, cxDropDownEdit,uSendMail, dxStatusBar,idsmtp,uSingnalMgr,
  9.   cxCheckListBox, cxButtonEdit, Menus, cxLookAndFeelPainters, StdCtrls,
  10.   cxButtons, dxSkinsCore, dxSkinsdxBarPainter, dxSkinBlack, dxSkinBlue,
  11.   dxSkinCaramel, 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.   TfrmWriteEmail = class(TBaseEditorForm)
  21.     btnSend: TdxBarLargeButton;
  22.     btnSave: TdxBarLargeButton;
  23.     btn3: TdxBarLargeButton;
  24.     btn4: TdxBarLargeButton;
  25.     btnSendSave: TdxBarLargeButton;
  26.     btn7: TdxBarLargeButton;
  27.     btn8: TdxBarLargeButton;
  28.     pnl1: TPanel;
  29.     cbRecver: TcxComboBox;
  30.     cbSender: TcxComboBox;
  31.     edtSubject: TcxTextEdit;
  32.     lbl2: TcxLabel;
  33.     lbl3: TcxLabel;
  34.     lbl4: TcxLabel;
  35.     lbl5: TcxLabel;
  36.     dxStatusBar1: TdxStatusBar;
  37.     btn6: TdxBarLargeButton;
  38.     btn1: TdxBarLargeButton;
  39.     pm3: TdxBarPopupMenu;
  40.     btn2: TdxBarButton;
  41.     btn5: TdxBarButton;
  42.     dxbrgrp1: TdxBarGroup;
  43.     dxbrgrp2: TdxBarGroup;
  44.     dxbrgrp3: TdxBarGroup;
  45.     dxbrgrp4: TdxBarGroup;
  46.     dxbrgrp5: TdxBarGroup;
  47.     dxbrgrp6: TdxBarGroup;
  48.     dxbrgrp7: TdxBarGroup;
  49.     lstAttch: TcxCheckListBox;
  50.     btnAddAttvh: TcxButton;
  51.     procedure FormCreate(Sender: TObject);
  52.     procedure FormDestroy(Sender: TObject);
  53.     procedure btnSaveClick(Sender: TObject);
  54.     procedure btnSendClick(Sender: TObject);
  55.     procedure btn8Click(Sender: TObject);
  56.     procedure FormClose(Sender: TObject; var Action: TCloseAction);
  57.     procedure btnSendSaveClick(Sender: TObject);
  58.     procedure btn3Click(Sender: TObject);
  59.     procedure btn4Click(Sender: TObject);
  60.     procedure cbAttchPropertiesButtonClick(Sender: TObject);
  61.     procedure btn6Click(Sender: TObject);
  62.     procedure btn7Click(Sender: TObject);
  63.     procedure FormShow(Sender: TObject);
  64.     procedure btn1Click(Sender: TObject);
  65.     procedure cbAttchKeyPress(Sender: TObject; var Key: Char);
  66.     procedure btnAddAttvhClick(Sender: TObject);
  67.   private
  68.     { Private declarations }
  69.     FPath:String;
  70.     FSingnalMgr:TAppSingnalMgr;
  71.     ThreadMailMessage:TThreadMailMessage;
  72.     FCheckPass:Boolean;
  73.     function CheckInput:Boolean;
  74.     procedure MyBtnClick(Sender:Tobject);
  75.   protected
  76.     procedure SendError(Sender:TObject;ErrMsg:string);
  77.     procedure SendComplete(Sender:TObject);
  78.     function GetEmailAsHtml:string;
  79.     function GetEmailAsText:string;
  80.     procedure LoadSingnalMenu;
  81.   public
  82.     { Public declarations }
  83.     FStr:string;
  84.     FSetDoc:Boolean;
  85.     procedure AddAttch(List:TStrings);
  86.   end;
  87. {
  88. 调用时,不要删除cbSender combox中的项!!! 因为这没有必要。
  89. }
  90. var
  91.   frmWriteEmail: TfrmWriteEmail;
  92. implementation
  93. {$R *.dfm}
  94. uses uMyxml, uCommon, NativeXml, UEmailFile, uAttemper,dateutils, ufrmMain;
  95. procedure TfrmWriteEmail.FormCreate(Sender: TObject);
  96. var
  97.   xml:TMyXml;
  98.   Lst:TList;
  99.   I:Integer;
  100.   info:PPopInfo;
  101.   cont:PContactAddress;
  102. begin
  103.   pnl1.Height:=130;
  104.   FStr:='';
  105.   FSetDoc:=False;
  106.   xml:=TAPPXml.Create;
  107.   try
  108.     if  (xml.Pop3sNode=nil)  then Exit;
  109.     lst:=TList.Create;
  110.     try
  111.       xml.Pop3Nodes(lst);
  112.       for I:=0 to Lst.Count-1 do
  113.       begin
  114.         New(info);
  115.         info.EMailAddr:=TXmlNode(Lst[I]).ReadString('emailaddr');
  116.         info.pwd:=TXmlNode(Lst[I]).ReadString('pwd');
  117.         info.saveto:= xml.Pop3sNode.AttributeByName['emailsaveto'];
  118.         info.pop3Server:=  TXmlNode(Lst[I]).ReadString('pop3server');
  119.         cbSender.Properties.Items.AddObject(TXmlNode(Lst[I]).ReadString('emailaddr'),TObject(info));
  120.       end;
  121.       cbSender.ItemIndex:=0;
  122.       
  123.       if  xml.Root.FindNode('emails')=nil then Exit; 
  124.       Lst.Clear;
  125.       xml.Root.FindNode('emails').NodesByName('email',lst); //从地址薄中读数据
  126.       for I:=0 to Lst.Count-1 do
  127.       begin
  128.         New(cont);
  129.         cont.Email:=TXmlNode(Lst[I]).ReadString('addr');
  130.         cont.displayname:=TXmlNode(Lst[I]).ReadString('displayname');
  131.         cbRecver.Properties.Items.AddObject(TXmlNode(Lst[I]).ReadString('addr'),TObject(cont));
  132.       end;
  133.       cbRecver.ItemIndex:=0;
  134.     finally
  135.       Lst.Free;
  136.     end;
  137.   finally
  138.     xml.Free;
  139.   end;
  140.   FSingnalMgr:=TAppSingnalMgr.Create;
  141.   FSingnalMgr.LoadFromFile;
  142.   LoadSingnalMenu;
  143. end;
  144. procedure TfrmWriteEmail.cbAttchPropertiesButtonClick(Sender: TObject);
  145. {var
  146.   I:Integer;    }
  147. begin
  148.   {dlgOpen.Filter:='所有文件|*.*';
  149.   if dlgOpen.Execute then
  150.     for I:=0 to dlgOpen.Files.Count-1 do
  151.       cbAttch.Properties.Items.Add(dlgOpen.Files.Strings[I]);
  152.   cbAttch.ItemIndex:=0; }
  153. end;
  154. procedure TfrmWriteEmail.FormDestroy(Sender: TObject);
  155. var
  156.   I:Integer;
  157. begin
  158.   for I:=0 to cbRecver.Properties.Items.Count-1 do
  159.     Dispose(PContactAddress(cbRecver.Properties.Items.Objects[I]));
  160.   for I:=0 to cbSender.Properties.Items.Count-1 do
  161.     Dispose(PPopInfo(cbSender.Properties.Items.Objects[I]));
  162.   FSingnalMgr.Free;
  163.   inherited ;
  164. end;
  165. procedure TfrmWriteEmail.btnSaveClick(Sender: TObject);
  166. var
  167.   EmailFile:TEmailFile;
  168.   MyXml:TMyXml;
  169.   L:TStrings;
  170.   I:integer;
  171. begin
  172.   FCheckPass:=CheckInput;
  173.   if not FCheckPass then Exit;
  174.   FPath:=frmMain.FEmailSaveTo+GenalFileName+'.ema';
  175.   EmailFile:=TEmailFile.Create(FPath);
  176.   try
  177.     EmailFile.Sender:=trim(cbSender.Text);
  178.     EmailFile.Recver:=trim(cbRecver.Text) ;
  179.     EmailFile.Subject:=Trim(edtSubject.Text);
  180.     EmailFile.Date:=DateTimeToStr(Now) ;
  181.     EmailFile.Content:=trim(getEmailAsHtml);
  182.     EmailFile.Size:=IntToStr(Length(EmailFile.Content));
  183.     L:=TStringList.Create;
  184.     try
  185.       for I:=0 to lstAttch.Items.Count-1 do
  186.         if lstAttch.Items[I].Checked then L.Add(lstAttch.Items[I].Text);
  187.       EmailFile.Attchs:=L; //cbAttch.Properties.Items;
  188.     finally
  189.       L.Free;
  190.     end;
  191.     EmailFile.SaveEmail;
  192.   finally
  193.     EmailFile.Free;
  194.   end;
  195.   MyXml:=TAppXml.Create;
  196.   try
  197.     MyXml.SaveNewEmail(trim(cbSender.Text),FPath);
  198.   finally
  199.     MyXml.Free;
  200.   end;
  201.   close;
  202. end;
  203. function TfrmWriteEmail.CheckInput: Boolean;
  204. var
  205.   I:Integer;
  206. begin
  207.   Result:=False;
  208.   if not TEMailAddress.IsEmail(trim(cbRecver.Text)) then
  209.   begin
  210.     ShowMessage('收件人地址不正确');
  211.     Exit;
  212.   end;
  213.   if not TEMailAddress.IsEmail(cbSender.Text) then
  214.   begin
  215.       ShowMessage('发件人地址不正确');
  216.       Exit;
  217.   end;
  218.   if Trim(edtSubject.Text)='' then
  219.   begin
  220.       ShowMessage('邮件主题不能为空');
  221.       Exit;
  222.   end;
  223.   for I:=0  to lstAttch.Items.Count-1 do
  224.   begin
  225.     if not FileExists(lstAttch.Items[I].Text) then
  226.     begin
  227.       ShowMessage(Format('附件%S不存在',[lstAttch.Items[I].Text]));
  228.       Exit;
  229.     end;
  230.   end;
  231.   try
  232.     if Trim(GetEmailAsHtml)='' then
  233.     begin
  234.       ShowMessage('邮件正文不能为空');
  235.       Exit;
  236.     end;
  237.   except
  238.     Exit;
  239.   end;
  240.   Result:=True;
  241. end;
  242. procedure TfrmWriteEmail.SendComplete(Sender: TObject);
  243. begin
  244.   dxStatusBar1.Panels[0].Text:='邮件发送完毕';
  245.   btnSend.Enabled:=True;
  246.   btnSendSave.Enabled:=True;
  247. end;
  248. procedure TfrmWriteEmail.SendError(Sender: TObject; ErrMsg: string);
  249. begin
  250.   ShowMessage('错误:'+ErrMsg);
  251.   btnSend.Enabled:=True;
  252.   btnSendSave.Enabled:=True;
  253. end;
  254. procedure TfrmWriteEmail.btnSendClick(Sender: TObject);
  255. var
  256.   dest:TDestinationPart;
  257.   Orig:TOriginPart;
  258.   f:TStringList;
  259.   I,idx:Integer;
  260.   info:PPopInfo;
  261.   Email:TEmailInfo;
  262.   S:String;
  263.   xml:TAppXml;
  264. begin
  265.   btnSaveClick(nil);
  266.   if not FCheckPass then Exit;
  267.   
  268.   f:=TStringList.Create;
  269.   for I:=0 to lstAttch.Items.Count-1 do
  270.     if lstAttch.Items[I].Checked then f.Add(lstAttch.Items[I].Text);
  271.   S:=trim(GetEmailAsHtml);
  272.   dest:=TDestinationPart.Create(Trim(cbRecver.Text),Trim(cbRecver.Text),Trim(cbRecver.Text),'',edtSubject.Text,S,f);
  273.   idx:=cbSender.ItemIndex;
  274.   info:=PPopinfo(cbSender.Properties.Items.Objects[idx]);
  275.   Orig:=TOriginPart.Create(atDefault,Trim(info.EMailAddr),Trim(info.EMailAddr),Trim(info.pwd),TEMailAddress.SMTPEmailSever(Trim(info.EMailAddr)),25);
  276.   Email:=TEmailInfo.create;
  277.   try
  278.     Email.Recv:=Dest;
  279.     Email.Send:=Orig;
  280.     Email.id:=FPath;
  281.     frmMain.EmailSenderMgr.Push(Email);
  282.     xml:=TAppXml.Create;
  283.     try
  284.       xml.AddSendingEmail(info.EMailAddr,Fpath);
  285.     finally
  286.       xml.Free;
  287.     end;
  288.     frmMain.EmailSenderMgr.Send;
  289.   finally
  290.     Email.Free;
  291.     Orig.Free;
  292.     dest.Free;
  293.     f.Free;
  294.   end;
  295.   close;
  296. end;
  297. procedure TfrmWriteEmail.btn8Click(Sender: TObject);
  298. begin
  299.   if ThreadMailMessage<>nil then  TerminateThread(ThreadMailMessage.Handle,0);
  300.   Self.Close;
  301. end;
  302. procedure TfrmWriteEmail.FormClose(Sender: TObject;
  303.   var Action: TCloseAction);
  304. begin
  305.   Action:=caFree;
  306. end;
  307. procedure TfrmWriteEmail.btnSendSaveClick(Sender: TObject);
  308. var
  309.   MyXml:TAppXml;
  310. begin
  311.   if not CheckInput then exit;
  312.   try
  313.     btnSendClick(nil);
  314.   finally
  315.     MyXml:=TAppXml.Create;
  316.     try
  317.       MyXml.AddSentEmail(PPopinfo(cbSender.Properties.Items.Objects[cbSender.ItemIndex]).EMailAddr,FPath);
  318.     finally
  319.       MyXml.Free;
  320.     end;
  321.   end;
  322.   close;
  323. end;
  324. procedure TfrmWriteEmail.btn3Click(Sender: TObject);
  325. var
  326.   EmailFile:TEmailFile;
  327.   MyXml:TMyXml;
  328.   L:TStrings;
  329.   I:integer;
  330. begin
  331.   if not CheckInput then Exit;
  332.   FPath:=FrmMain.FEmailSaveTo;
  333.   EmailFile:=TEmailFile.Create(FPath);
  334.   try
  335.     EmailFile.Sender:=trim(cbSender.Text);
  336.     EmailFile.Recver:=trim(cbRecver.Text) ;
  337.     EmailFile.Subject:=Trim(edtSubject.Text);
  338.     EmailFile.Content:=trim(GetEmailAsHtml);
  339.     EmailFile.Size:=IntToStr(Length(EmailFile.Content));
  340.     EmailFile.Date:=DateTimeToStr(Now);
  341.     L:=TStringList.Create;
  342.     try
  343.       for I:=0 to lstAttch.Count-1 do
  344.         if lstAttch.Items[I].Checked then L.Add(lstAttch.Items[I].Text);
  345.       EmailFile.Attchs:=L; //cbAttch.Properties.Items;
  346.     finally
  347.       L.Free;
  348.     end;
  349.     EmailFile.SaveEmail;
  350.   finally
  351.     EmailFile.Free;
  352.   end;
  353.   MyXml:=TMyXml.create(AppPath+'EmailServers.xml');
  354.   try
  355.     MyXml.SaveAsDraft(trim(cbSender.Text),FPath);
  356.   finally
  357.     MyXml.Free;
  358.   end;
  359.   close;
  360. end;
  361. procedure TfrmWriteEmail.btn4Click(Sender: TObject);
  362. var
  363.   EmailFile:TEmailFile;
  364.   MyXml:TMyXml;
  365.   L:TStrings;
  366.   I:Integer;
  367. begin
  368.   if not CheckInput then Exit;
  369.   with TfrmAttemper.Create(nil) do
  370.   try
  371.     if ShowModal=mrOk then
  372.     begin
  373.       FPath:=frmMain.FEmailSaveTo;
  374.       EmailFile:=TEmailFile.Create(FPath);
  375.       try
  376.         EmailFile.Sender:=trim(cbSender.Text);
  377.         EmailFile.Recver:=trim(cbRecver.Text) ;
  378.         EmailFile.Subject:=Trim(edtSubject.Text);
  379.         EmailFile.Date:=FormatDateTime('YYYY-MM-DD hh:nn:ss', Trunc(Date) + Frac(Time));
  380.         EmailFile.Content:=trim(GetEmailAsHtml);
  381.         EmailFile.Size:=IntToStr(Length(EmailFile.Content) );
  382.         L:=TStringList.Create;
  383.         try
  384.           for I:=0 to lstAttch.Items.Count-1 do
  385.             if lstAttch.Items[I].Checked then L.Add(lstAttch.Items[I].Text);
  386.           EmailFile.Attchs:=L ;//cbAttch.Properties.Items;
  387.         finally
  388.           L.Free;
  389.         end;
  390.         EmailFile.SaveEmail;
  391.       finally
  392.         EmailFile.Free;
  393.       end;
  394.       MyXml:=TMyXml.create(AppPath+'EmailServers.xml');
  395.       try
  396.         MyXml.SaveAsAttampter(trim(cbSender.Text),FPath,DateOf(edDate.Date)+TimeOf(edTime.Time));
  397.       finally
  398.         MyXml.Free;
  399.       end;
  400.     end;
  401.   finally
  402.     Free;
  403.   end;
  404.   close;
  405. end;
  406. function TfrmWriteEmail.GetEmailAsHtml: string;
  407. begin
  408.   result:=HtmlEdit.HTML;
  409. end;
  410. function TfrmWriteEmail.GetEmailAsText: string;
  411. begin
  412.   result:=HtmlEdit.Text;
  413. end;
  414. procedure TfrmWriteEmail.btn6Click(Sender: TObject);
  415. begin
  416.   HtmlEdit.PrintPageSetup;
  417.   HtmlEdit.Print;
  418. end;
  419. procedure TfrmWriteEmail.btn7Click(Sender: TObject);
  420. begin
  421.   HtmlEdit.PrintPageSetup;
  422.   HtmlEdit.PrintPreview;
  423. end;
  424. procedure TfrmWriteEmail.FormShow(Sender: TObject);
  425. begin
  426.   inherited;
  427.   if (Fstr<>'')  then
  428.   begin
  429.     HtmlEdit.Insert(FStr);
  430.   end;
  431. end;
  432. procedure TfrmWriteEmail.LoadSingnalMenu;
  433. var
  434.   btn:TdxBarButton;
  435.   I:integer;
  436.   pData:PSingnalData;
  437. begin
  438.   I:=0;
  439.   FSingnalMgr.Iterator.First;
  440.   while FSingnalMgr.Iterator.HasNext do
  441.   begin
  442.     btn:= pm3.BarManager.AddButton;
  443.     btn.ButtonStyle:=bsChecked;
  444.     pData:=PSingnalData(FSingnalMgr.Iterator.Next);
  445.     btn.Caption:=pData.Caption;
  446.     btn.Tag:=I;
  447.     btn.Down:=pData.Default;
  448.     btn.AllowAllUp:=False;
  449.     btn.Category:=5;
  450.     dxbrmngr1.Groups[5].Add(btn);
  451.     btn.GroupIndex:=5;
  452.     pm3.ItemLinks.Add.Item:=btn;
  453.     btn.OnClick:=MyBtnClick;
  454.     btn.ImageIndex:=I;
  455.     inc(I);
  456.   end;   
  457. end;
  458. procedure TfrmWriteEmail.MyBtnClick(Sender: Tobject);
  459. begin
  460.   HtmlEdit.Insert(StrPas(FSingnalMgr.Get(TdxBarButton(Sender).Tag).Text));
  461. end;
  462. procedure TfrmWriteEmail.btn1Click(Sender: TObject);
  463. var
  464.   I:integer;
  465. begin
  466.   for I:=0 to dxbrmngr1.Groups[5].Count-1 do
  467.     if TdxBarButton(dxbrmngr1.Groups[5].Items[I]).Down then
  468.       HtmlEdit.Insert(StrPas(FSingnalMgr.Get(TdxBarButton(dxbrmngr1.Groups[5].Items[I]).Tag).Text));
  469. end;
  470. procedure TfrmWriteEmail.cbAttchKeyPress(Sender: TObject; var Key: Char);
  471. {var
  472.   idx:Integer;}
  473. begin
  474. //  if cbAttch.Text='' then Exit;
  475. //  if byte(Key) in [8,12,46] then {推格键,vbClear,vk_Delete}
  476. //  begin
  477. //    idx:=cbAttch.Properties.Items.IndexOf(cbAttch.Text);
  478. //    cbAttch.Properties.Items.Delete(idx);
  479. //    cbAttch.Text:='';
  480. //  end;
  481. end;
  482. procedure TfrmWriteEmail.btnAddAttvhClick(Sender: TObject);
  483. var
  484.   I:integer;
  485. begin
  486.   dlgOpen.Filter:='所有文件|*.*';
  487.   if dlgOpen.Execute then
  488.   begin
  489.     for I:=0 to dlgOpen.Files.Count-1 do
  490.     begin
  491.       with lstAttch.Items.Add  do
  492.       begin
  493.         Text:=dlgOpen.Files[I];
  494.         Checked:=True;
  495.       end;
  496.     end;
  497.     pnl1.Height:=234;
  498.   end;
  499. end;
  500. procedure TfrmWriteEmail.AddAttch(List: TStrings);
  501. var
  502.   I:Integer;
  503. begin
  504.   for I:=0 to List.Count-1 do
  505.     with lstAttch.Items.Add  do
  506.     begin
  507.       Text:=List[I];
  508.       Checked:=True;
  509.     end;
  510. end;
  511. end.