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

Email服务器

开发平台:

Delphi

  1. unit ufrmNewEmail;
  2. interface
  3. uses
  4.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  5.   Dialogs, StdCtrls, OleCtrls, OALib_TLB, dxBar, dxBarExtItems, cxClasses,
  6.   cxGraphics, cxLabel, cxTextEdit, cxControls, cxContainer, cxEdit,
  7.   cxMaskEdit, cxDropDownEdit, ExtCtrls, cxStyles, cxMRUEdit, dxStatusBar,
  8.   ComCtrls, cxLookAndFeels,IdSMTP,uSendMail, DdeMan,uHtmlEdit, 
  9.   SHDocVw, ActnList, uBaseEditorForm;
  10. type
  11.   TfrmNewEMail = class(TBaseEditorForm)
  12.     dxbrmngr1: TdxBarManager;
  13.     btnSend: TdxBarLargeButton;
  14.     btnSave: TdxBarLargeButton;
  15.     dxbrlrgbtn3: TdxBarLargeButton;
  16.     dxbrlrgbtn4: TdxBarLargeButton;
  17.     Panel1: TPanel;
  18.     cbRecver: TcxComboBox;
  19.     cbSender: TcxComboBox;
  20.     edSubject: TcxTextEdit;
  21.     cxLabel1: TcxLabel;
  22.     cxLabel2: TcxLabel;
  23.     cxLabel3: TcxLabel;
  24.     cbAttch: TcxMRUEdit;
  25.     cxLabel4: TcxLabel;
  26.     dlgOpenFile: TOpenDialog;
  27.     dxStatusBar1: TdxStatusBar;
  28.     dxbrbtn1: TdxBarButton;
  29.     dxbrbtn2: TdxBarButton;
  30.     btnSendSave: TdxBarLargeButton;
  31.     dxbrmngr1Bar2: TdxBar;
  32.     dxbrlrgbtn1: TdxBarLargeButton;
  33.     dxbrbtn3: TdxBarButton;
  34.     btn1: TdxBarLargeButton;
  35.     btn2: TdxBarLargeButton;
  36.     ddeConv: TDdeClientConv;
  37.     procedure FormCreate(Sender: TObject);
  38.     procedure cbAttchPropertiesButtonClick(Sender: TObject);
  39.     procedure FormDestroy(Sender: TObject);
  40.     procedure btnSaveClick(Sender: TObject);
  41.     procedure btnSendClick(Sender: TObject);
  42.     procedure dxbrlrgbtn3Click(Sender: TObject);
  43.     procedure FormClose(Sender: TObject; var Action: TCloseAction);
  44.     procedure btnSendSaveClick(Sender: TObject);
  45.     procedure dxbrlrgbtn1Click(Sender: TObject);
  46.     procedure FormShow(Sender: TObject);
  47.     procedure btn1Click(Sender: TObject);
  48.     procedure btn2Click(Sender: TObject);
  49.     procedure ofcOfficeEnter(Sender: TObject);
  50.   private
  51.     { Private declarations }
  52.     FPath:String;
  53.     
  54.     ThreadMailMessage:TThreadMailMessage;
  55.     function CheckInput:Boolean;
  56.   protected
  57.    
  58.     function RTFtoTXT(rtf:string):string;
  59.     procedure SendError(Sender:TObject;ErrMsg:string);
  60.     procedure SendComplete(Sender:TObject);
  61.   public
  62.     { Public declarations }
  63.     FStr:string;
  64.     FSetDoc:Boolean;
  65.     
  66.     function GetRTF: string;
  67.     procedure  SetRtf(S:String);
  68.     procedure RunMacro(Macro:pChar);
  69.   end;
  70. var
  71.   frmNewEMail: TfrmNewEMail;
  72. implementation
  73. uses NativeXml, uCommon, ActiveX, ComObj,WordXP, OleServer, uMyXml,
  74.   UEmailFile, uAttemper,DateUtils,Math;
  75. {$R *.dfm}
  76. procedure TfrmNewEMail.FormCreate(Sender: TObject);
  77. var
  78.   xml:TMyXml;
  79.   Lst:TList;
  80.   I:Integer;
  81.   info:PPopInfo;
  82.   cont:PContactAddress;
  83. begin
  84.   FStr:='';
  85.   FSetDoc:=False;
  86.   xml:=TMyXml.Create(AppPath+'EmailServers.xml');
  87.   try
  88.     if  (xml.Root.FindNode('pop3s')=nil)  then Exit;
  89.     lst:=TList.Create;
  90.     try
  91.       xml.Root.FindNode('pop3s').NodesByName('pop3',lst);
  92.       for I:=0 to Lst.Count-1 do
  93.       begin
  94.         New(info);
  95.         info.EMailAddr:=TXmlNode(Lst[I]).ReadString('emailaddr');
  96.         info.pwd:=TXmlNode(Lst[I]).ReadString('pwd');
  97.         info.saveto:= TXmlNode(Lst[I]).ReadString('emailsaveto');
  98.         info.pop3Server:=  TXmlNode(Lst[I]).ReadString('pop3server');
  99.         cbSender.Properties.Items.AddObject(TXmlNode(Lst[I]).ReadString('emailaddr'),TObject(info));
  100.       end;
  101.       cbSender.ItemIndex:=0;
  102.       
  103.       if  xml.Root.FindNode('emails')=nil then Exit;
  104.       Lst.Clear;
  105.       xml.Root.FindNode('emails').NodesByName('email',lst);
  106.       for I:=0 to Lst.Count-1 do
  107.       begin
  108.         New(cont);
  109.         cont.Email:=TXmlNode(Lst[I]).ReadString('addr');
  110.         cont.displayname:=TXmlNode(Lst[I]).ReadString('displayname');
  111.         cbRecver.Properties.Items.AddObject(TXmlNode(Lst[I]).ReadString('addr'),TObject(cont));
  112.       end;
  113.       cbRecver.ItemIndex:=0;
  114.     finally
  115.       Lst.Free;
  116.     end;
  117.   finally
  118.     xml.Free;
  119.   end;
  120. end;
  121. procedure TfrmNewEMail.cbAttchPropertiesButtonClick(Sender: TObject);
  122. var
  123.   I:Integer;
  124. begin
  125.   if dlgOpenFile.Execute then
  126.     for I:=0 to dlgOpenFile.Files.Count-1 do
  127.       cbAttch.Properties.Items.Add(dlgOpenFile.Files.Strings[I]);
  128.   cbAttch.ItemIndex:=0;
  129. end;
  130. procedure TfrmNewEMail.FormDestroy(Sender: TObject);
  131. var
  132.   I:Integer;
  133. begin
  134.   for I:=0 to cbRecver.Properties.Items.Count-1 do
  135.     Dispose(PContactAddress(cbRecver.Properties.Items.Objects[I]));
  136.   for I:=0 to cbSender.Properties.Items.Count-1 do
  137.     Dispose(PPopInfo(cbSender.Properties.Items.Objects[I]));
  138. end;
  139. function GetRTFFormat(DataObject: IDataObject; var RTFFormat: TFormatEtc): Boolean; 
  140. var 
  141.   Formats: IEnumFORMATETC;
  142.   TempFormat: TFormatEtc; 
  143.   pFormatName: PChar; 
  144.   Found: Boolean; 
  145. begin 
  146.   try
  147.     OleCheck(DataObject.EnumFormatEtc(DATADIR_GET, Formats));
  148.     Found := False;
  149.     while (not Found) and (Formats.Next(1, TempFormat, nil) = S_OK) do
  150.     begin
  151.       pFormatName := AllocMem(255);
  152.       GetClipBoardFormatName(TempFormat.cfFormat, pFormatName, 254);
  153.       if (string(pFormatName) = 'Rich Text Format') then
  154.       begin
  155.         RTFFormat := TempFormat;
  156.         //WriteLog(string(pFormatName));
  157.         Found := True;
  158.       end;
  159.       FreeMem(pFormatName);
  160.     end;
  161.     Result := Found;
  162.   except
  163.     Result := False;
  164.   end;
  165. end;
  166. function TfrmNewEMail.GetRTF: string;
  167. var
  168.   DataObject: IDataObject;
  169.   RTFFormat: TFormatEtc;
  170.   ReturnData: TStgMedium;
  171.   Buffer: PChar;
  172.   WordDoc: _Document;
  173.   WordApp: _Application;
  174. begin
  175.   Result := '';
  176.   try
  177.     GetActiveOleObject('Word.Application').QueryInterface(_Application, WordApp);
  178.   except
  179.     ShowMessage('Error: MSWord is not running');
  180.     Exit;
  181.   end;
  182.   if (WordApp <> nil) then
  183.   try
  184.     WordDoc := WordApp.ActiveDocument;
  185.     WordDoc.QueryInterface(IDataObject, DataObject);
  186.     if GetRTFFormat(DataObject, RTFFormat) then
  187.     begin
  188.       OleCheck(DataObject.GetData(RTFFormat, ReturnData));
  189.       //RTF is passed through global memory
  190.       Buffer := GlobalLock(ReturnData.hglobal);
  191.       //Buffer is a pointer to the RTF text
  192.       Result := StrPas(Buffer);
  193.       GlobalUnlock(ReturnData.hglobal);
  194.       ReleaseStgMedium(ReturnData);
  195.     end;
  196.   except
  197.     // Error occured...
  198.   end;
  199. end;
  200. procedure TfrmNewEMail.SetRtf(S: String);
  201. var 
  202.   DataObject: IDataObject;
  203.   RTFFormat: TFormatEtc;
  204.   ReturnData: TStgMedium;
  205.   WordDoc: _Document;
  206.   WordApp: _Application;
  207. begin
  208.   try
  209.     GetActiveOleObject('Word.Application').QueryInterface(_Application, WordApp);
  210.   except
  211.     ShowMessage('Error: MSWord is not running');
  212.     Exit;
  213.   end;
  214.   if (WordApp <> nil) then
  215.   try
  216.     WordDoc := WordApp.ActiveDocument; 
  217.     WordDoc.QueryInterface(IDataObject, DataObject);
  218.     if GetRTFFormat(DataObject, RTFFormat) then
  219.     begin
  220.       ReturnData.tymed:=TYMED_HGLOBAL;
  221.       ReturnData.unkForRelease:=nil;
  222.       ReturnData.hGlobal:=GlobalAlloc(GMEM_MOVEABLE or GMEM_ZEROINIT or GMEM_DDESHARE,Length(S));
  223.       CopyMemory(Ptr(ReturnData.hGlobal),@S[1],Length(S));
  224.       OleCheck(DataObject.SetData(RTFFormat,ReturnData,LongBool(True)));
  225.       //ReleaseStgMedium(ReturnData);
  226.     end;
  227.   except
  228.     // Error occured...
  229.   end;
  230. end;
  231. function TfrmNewEMail.RTFtoTXT(rtf: string): string;
  232. var 
  233.   ss: TStringstream;
  234.   RichEdit: TRichEdit;
  235. begin
  236.   ss := TStringStream.Create(rtf);
  237.   RichEdit:=TRichEdit.Create(self);
  238.   RichEdit.Parent:=Self;
  239.   RichEdit.Visible:=false;
  240.   try
  241.     ss.Position := 0;
  242.     RichEdit.Lines.LoadFromStream(ss); // load rtf into richedit1
  243.     Result:= RichEdit.Lines.Text;
  244.   finally
  245.     RichEdit.Free;
  246.     ss.Free
  247.   end;
  248. end;
  249. procedure TfrmNewEMail.btnSaveClick(Sender: TObject);
  250. var
  251.   EmailFile:TEmailFile;
  252.   MyXml:TMyXml;
  253. begin
  254.   if not CheckInput then Exit;
  255.   
  256.   FPath:=AppPath+'NoSent'+GenalFileName+'.ema';
  257.   EmailFile:=TEmailFile.Create(FPath);
  258.   try
  259.     EmailFile.Sender:=trim(cbSender.Text);
  260.     EmailFile.Recver:=trim(cbRecver.Text) ;
  261.     EmailFile.Subject:=Trim(edSubject.Text);
  262.     EmailFile.Date:=DateTimeToStr(Now) ;
  263.     EmailFile.Content:=trim(RTFtoTXT(GetRTF));
  264.     EmailFile.Size:=IntToStr(Length(EmailFile.Content));
  265.     EmailFile.Attchs:=cbAttch.Properties.Items;
  266.     EmailFile.SaveEmail;
  267.   finally
  268.     EmailFile.Free;
  269.   end;
  270.   MyXml:=TMyXml.create(AppPath+'EmailServers.xml');
  271.   try
  272.     MyXml.SaveNewEmail(trim(cbSender.Text),FPath);
  273.   finally
  274.     MyXml.Free;
  275.   end;
  276. end;
  277. function TfrmNewEMail.CheckInput: Boolean;
  278. var
  279.   I:Integer;
  280. begin
  281.   Result:=False;
  282.   if not TEMailAddress.IsEmail(trim(cbRecver.Text)) then
  283.   begin
  284.     ShowMessage('收件人地址不正确');
  285.     Exit;
  286.   end;
  287.   if not TEMailAddress.IsEmail(cbSender.Text) then
  288.   begin
  289.       ShowMessage('发件人地址不正确');
  290.       Exit;
  291.   end;
  292.   if Trim(edSubject.Text)='' then
  293.   begin
  294.       ShowMessage('邮件主题不能为空');
  295.       Exit;
  296.   end;
  297.   for I:=0  to cbAttch.Properties.Items.Count-1 do
  298.   begin
  299.     if not FileExists(cbAttch.Properties.Items[I]) then
  300.     begin
  301.       ShowMessage(Format('附件%S不存在',[cbAttch.Properties.Items[I]]));
  302.       Exit;
  303.     end;
  304.   end;
  305.   try
  306.     if Trim(RTFtoTXT(GetRTF))='' then
  307.     begin
  308.       ShowMessage('邮件正文不能为空');
  309.       Exit;
  310.     end;
  311.   except
  312.     Exit;
  313.   end;
  314.   Result:=True;
  315. end;
  316. procedure TfrmNewEMail.btnSendClick(Sender: TObject);
  317. var
  318.   dest:TDestinationPart;
  319.   Orig:TOriginPart;
  320.   f:TStringList;
  321.   I,idx:Integer;
  322.   info:PPopInfo;
  323.  { Head:TEmailRec;  }
  324.   S:String;  
  325. begin
  326.   if not CheckInput then Exit;
  327.   btnSend.Enabled:=False;
  328.   btnSendSave.Enabled:=False;
  329.   dxStatusBar1.Panels[0].Text:='正在发送邮件,请稍候。。。';
  330.   f:=TStringList.Create;
  331.   
  332.   for I:=0 to cbAttch.Properties.Items.Count-1 do
  333.   begin
  334.     //Attch:=Attch+cbAttch.Properties.Items[I]+';';
  335.     f.Add(cbAttch.Properties.Items[I]);
  336.   end;
  337.   S:=trim(RTFtoTXT(GetRTF));
  338.   dest:=TDestinationPart.Create(Trim(cbRecver.Text),Trim(cbRecver.Text),Trim(cbRecver.Text),'',edSubject.Text,S,f);
  339.   idx:=cbSender.ItemIndex;
  340.   info:=PPopinfo(cbSender.Properties.Items.Objects[idx]);
  341.   Orig:=TOriginPart.Create(atDefault,Trim(info.EMailAddr),Trim(info.EMailAddr),Trim(info.pwd),TEMailAddress.SMTPEmailSever(Trim(info.EMailAddr)),25);
  342.   try
  343.     ThreadMailMessage:=TThreadMailMessage.Create(dest,Orig);
  344.     ThreadMailMessage.OnSendComplete:=Self.SendComplete;
  345.     ThreadMailMessage.OnSendError:=Self.SendError;
  346.     ThreadMailMessage.Resume;
  347.   finally
  348.     Orig.Free;
  349.     dest.Free;
  350.     f.Free;
  351.   end;
  352. end;
  353. procedure TfrmNewEMail.SendComplete(Sender: TObject);
  354. begin
  355.   dxStatusBar1.Panels[0].Text:='邮件发送完毕。';
  356.   btnSend.Enabled:=True;
  357.   btnSendSave.Enabled:=True;
  358. end;
  359. procedure TfrmNewEMail.SendError(Sender: TObject; ErrMsg: string);
  360. begin
  361.   ShowMessage('错误:'+ErrMsg);
  362.   btnSend.Enabled:=True;
  363.   btnSendSave.Enabled:=True;
  364. end;
  365. procedure TfrmNewEMail.dxbrlrgbtn3Click(Sender: TObject);
  366. begin
  367.   if ThreadMailMessage<>nil then  TerminateThread(ThreadMailMessage.Handle,0);
  368.   Self.Close;
  369. end;
  370. procedure TfrmNewEMail.FormClose(Sender: TObject;
  371.   var Action: TCloseAction);
  372. begin
  373.   Action:=caFree;
  374. end;
  375. procedure TfrmNewEMail.btnSendSaveClick(Sender: TObject);
  376. var
  377.   MyXml:TMyXml;
  378. begin
  379.   btnSaveClick(nil);
  380.   MyXml:=TMyXml.Create(AppPath+'EmailServers.xml');
  381.   try
  382.     MyXml.AddSentEmail(PPopinfo(cbSender.Properties.Items.Objects[cbSender.ItemIndex]).EMailAddr,FPath);
  383.   finally
  384.     MyXml.Free;
  385.   end;
  386.   btnSendClick(nil);
  387. end;
  388. procedure TfrmNewEMail.dxbrlrgbtn1Click(Sender: TObject);
  389. begin
  390.   ofcOffice.Print;
  391. end;
  392. procedure TfrmNewEMail.FormShow(Sender: TObject);
  393. begin
  394.   ofcOffice.CreateNew('Word.Document');
  395.   ofcOffice.ShowToolbars(True);
  396.   while not ofcOffice.IsOpen do Application.ProcessMessages;
  397.   //if FSetDoc then
  398.   //begin
  399.   //  ofcOffice.SetFocus;
  400.   //  RunMacro('[EditPaste]');
  401.   //end;
  402.    //SetRtf(FStr);
  403. end;
  404. procedure TfrmNewEMail.btn1Click(Sender: TObject);
  405. var
  406.   EmailFile:TEmailFile;
  407.   MyXml:TMyXml;
  408. begin
  409.   if not CheckInput then Exit;
  410.   FPath:=AppPath+'NoSent'+GenalFileName+'.ema';
  411.   EmailFile:=TEmailFile.Create(FPath);
  412.   try
  413.     EmailFile.Sender:=trim(cbSender.Text);
  414.     EmailFile.Recver:=trim(cbRecver.Text) ;
  415.     EmailFile.Subject:=Trim(edSubject.Text);
  416.     EmailFile.Content:=trim(RTFtoTXT(GetRTF));
  417.     EmailFile.Size:=IntToStr(Length(EmailFile.Content));
  418.     EmailFile.Date:=DateTimeToStr(Now);
  419.     EmailFile.Attchs:=cbAttch.Properties.Items;
  420.     EmailFile.SaveEmail;
  421.   finally
  422.     EmailFile.Free;
  423.   end;
  424.   MyXml:=TMyXml.create(AppPath+'EmailServers.xml');
  425.   try
  426.     MyXml.SaveAsDraft(trim(cbSender.Text),FPath);
  427.   finally
  428.     MyXml.Free;
  429.   end;
  430. end;
  431. procedure TfrmNewEMail.btn2Click(Sender: TObject);
  432. var
  433.   EmailFile:TEmailFile;
  434.   MyXml:TMyXml;
  435. begin
  436.   if not CheckInput then Exit;
  437.   with TfrmAttemper.Create(nil) do
  438.   try
  439.     if ShowModal=mrOk then
  440.     begin
  441.       FPath:=AppPath+'NoSent'+GenalFileName+'.ema';
  442.       EmailFile:=TEmailFile.Create(FPath);
  443.       try
  444.         EmailFile.Sender:=trim(cbSender.Text);
  445.         EmailFile.Recver:=trim(cbRecver.Text) ;
  446.         EmailFile.Subject:=Trim(edSubject.Text);
  447.         EmailFile.Date:=DateTimeToStr(DateOf(edDate.Date)+TimeOf(edTime.Time));
  448.         EmailFile.Content:=trim(RTFtoTXT(GetRTF));
  449.         EmailFile.Size:=IntToStr(Length(EmailFile.Content) );
  450.         EmailFile.Attchs:=cbAttch.Properties.Items;
  451.         EmailFile.SaveEmail;
  452.       finally
  453.         EmailFile.Free;
  454.       end;
  455.       MyXml:=TMyXml.create(AppPath+'EmailServers.xml');
  456.       try
  457.         MyXml.SaveAsAttampter(trim(cbSender.Text),FPath,DateOf(edDate.Date)+TimeOf(edTime.Time));
  458.       finally
  459.         MyXml.Free;
  460.       end;
  461.     end;
  462.   finally
  463.     Free;
  464.   end;
  465. end;
  466. procedure TfrmNewEMail.RunMacro(Macro: pChar);
  467. var
  468.   pMacro:array[0..80] of Char;
  469. begin
  470.   ddeConv.SetLink('Winword','System');{设置连接}
  471.   ddeConv.OpenLink;{按设置打开连接}
  472.   StrPCopy(pMacro,Macro);
  473.   try
  474.   if Not ddeConv.ExecuteMacro(pMacro,false) then{执行宏命令}
  475.     ShowMessage('Unable to Execute Macro');
  476.   except
  477.   end;
  478.   ddeConv.CloseLink;{断开连接}
  479.   {
  480.  [FileNew] …… 创建新文件
  481.   [FileClose] …… 关闭文件
  482.   [FileSave] …… 保存文件
  483.   [FilePrint] …… 打印文件
  484.   [FileExit] …… 退出Word
  485.   [File1] …… 打开最近打开的文件,相应还有[File2]、[File3]等等
  486.   [EditCut] …… 剪切操作 
  487.   [EditCopy] …… 复制操作
  488.   [EditPaste] …… 粘贴操作
  489.   [EditUndo] …… 恢复上一步
  490.   [EditRedo] …… 重做上一步
  491.   [EditClear] …… 清除操作
  492.   [EditSelectAll] …… 全选操作
  493.   [ViewNormal] …… 正常视图
  494.   [ViewPage] …… 页面视图
  495.   [ViewOutLine] …… 大纲视图
  496.   [InsertBreak] …… 插入分割符
  497.   [InsertIndex] …… 插入索引
  498.   [FormatNumber] …… 格式化项目符号和编号
  499.   [ToolsOptions] …… 工具的选项
  500.   [TableInsertTable] …… 插入表格
  501.   [TableInsertRow] …… 插入行
  502.   [TableDeleteRow] …… 删除行
  503.   [TableSplit] …… 拆分表格
  504.   [TableSelectRow] …… 选择行
  505.   [TableSelectColumn] …… 选择列
  506.   [TableSelectTable] …… 选择表格
  507.   [TableSort] …… 排序
  508.   [WindowNewWindow] …… 新建窗口
  509.   [Window1] …… 最近打开的窗口,响应还有[Window2]、[Window3]等等
  510.   [HelpIndex] …… 帮助的索引
  511.   [HelpAbout] …… 帮助的关于
  512.   }
  513. end;
  514. procedure TfrmNewEMail.ofcOfficeEnter(Sender: TObject);
  515. begin
  516.   if FSetDoc then
  517.   begin
  518.     FSetDoc:=False;
  519.     RunMacro('[ViewOutLine]');
  520.   end;
  521. end;
  522. end.