现在的位置: 首页 > 综合 > 正文

用indy收邮件的问题,怎么样把邮件存放到本地,最好是存到数据库

2012年01月19日 ⁄ 综合 ⁄ 共 6119字 ⁄ 字号 评论关闭
用indy收邮件的问题,怎么样把邮件存放到本地,最好是存到数据库 Delphi / Windows SDK/API
http://www.delphi2007.net/DelphiNetwork/html/delphi_20061215094636123.html
现在用indy收邮件,邮件是可以收了,但是不知道怎么样最好的保存方法,也不知道foxmail怎么保存的,也   不知道怎么样把他保存到数据库中,感觉他的数据库类型比较麻烦,附件的话是不是一般都不存到数据库中的?  
  请高手指教

可以实现的     等等写个列子给你看~~

//****************************收取指定的邮件************************************  
  function   Trec_mail.Get_Receive(lett_index:   OleVariant):   OleVariant;  
  var  
      nmm_strUidl,nmm_strAttachFileName:string;  
      nmm_strDirName,nmm_strUrlPath:string;  
      nmm_strAttachFileUrl,nmm_strAttachFilePath:widestring;  
      nmm_index:integer;  
      nmm_strbody:widestring;  
      nmm_strdecode:string;  
  begin  
  try  
          nmg_intLetterIndex   :=strtoint(lett_index);   //全局变量邮件id  
          nmm_strUidl   :=   trim(get_uid);  
          nmg_msgBody.clear;  
          nmg_msgBody.NoDecode   :=   false;  
          nmg_msgBody.IsEncoded   :=   true;  
          try  
                nmg_pop.Retrieve(nmg_intLetterindex,nmg_msgBody);  
          except  
                result:='';  
                exit;  
          end;  
          nmg_msgHead.clear;  
          nmg_msgHead.NoDecode   :=   false;  
          nmg_msgHead.IsEncoded   :=   true;  
          try  
              nmg_pop.RetrieveHeader(nmg_intLetterIndex,nmg_msgHead);  
          except  
              result:='';  
              exit;  
          end;  
          if   nmg_strMailUserDir=''   then  
                begin  
                    nmm_strDirName   :=   nmg_strRootDir+nmm_strUidl;  
                    nmm_strUrlPath   :=   nmg_strRootUrlPath+nmm_strUidl;  
                end  
          else  
                begin  
                    nmm_strDirName   :=   nmg_strRootDir+nmg_strMailUserDir+'\'+nmm_strUidl;  
                    nmm_strUrlPath   :=   nmg_strRootUrlPath+nmg_strMailUserDir+'/'+nmm_strUidl;  
                end;  
          ForceDirectories(nmm_strDirName);  
          FormAttachHtm;  
          nmg_blnHasAttOut   :=   false;  
          //*************************正文的下载***************************************  
          try  
              if   nmg_msgBody.messageparts.Count=0   then   nmg_stsContent   :=nmg_msgBody.Body  
              else   begin  
                    for   nmm_index:=0   to   pred(nmg_msgBody.messageparts.Count)   do  
                    if   nmg_msgBody.MessageParts.Items[nmm_index]   is   TIdText   then   nmg_stsContent   :=   TIdText(nmg_msgBody.MessageParts.Items[nmm_index]).Body;  
              end;  
          except  
                result:='';  
                exit;  
          end;  
          if   (pos('HTML>',nmg_stsContent.text))or(pos('html>',nmg_stsContent.text))<1   then     nmg_stsContent.text:='<pre>'+nmg_stsContent.text+'</pre>';  
          //*************************附件的下载***************************************  
          for   nmm_index:=0   to   pred(nmg_msgBody.messageparts.Count)   do  
                  begin  
                          if   (nmg_msgBody.MessageParts.Items[nmm_index]   is   TIdAttachment)   then  
                                  begin  
                                          nmm_strAttachFileName   :=DecodeHeader(TIdAttachment(nmg_msgBody.MessageParts.Items[nmm_index]).Filename);  
                                          nmm_strAttachFileUrl   :=   nmm_strUrlPath;  
                                          nmm_strdecode:=(tidattachment(nmg_msgBody.MessageParts.Items[nmm_index]).ContentDisposition);  
                                          if   trim(nmm_strdecode)<>''   then  
                                                begin  
                                                      nmm_strAttachFilePath   :=   nmm_strDirName   +   '\attachment\'+inttostr(nmm_index);  
                                                      ForceDirectories(nmm_strAttachFilePath);  
                                                      nmm_strAttachFileUrl   :=   nmm_strAttachFileUrl+'/attachment/'+inttostr(nmm_index)+'/'+nmm_strAttachFileName;  
                                                      nmm_strAttachFilePath   :=   nmm_strAttachFilePath+'\'+nmm_strAttachFileName;  
                                                      try  
                                                      if   not   fileexists(nmm_strAttachFilePath)   then  
                                                      tidattachment(nmg_msgBody.MessageParts.Items[nmm_index]).SaveToFile(nmm_strAttachFilePath);  
                                                      except  
                                                      continue;  
                                                      end;  
                                                      nmg_stsAttach.add('<a   href="'+   nmm_strAttachFileUrl+'"   target=_blank>   '+nmm_strAttachFileName+'</a><BR><br>');  
                                                      nmg_blnHasAttOut   :=   true   ;  
                                                end  
                                          else  
                                                begin  
                                                      nmm_strAttachFilePath   :=   nmm_strDirName+'\Image\'+inttostr(nmm_index);  
                                                      nmm_strAttachFileUrl   :=   nmm_strAttachFileUrl+'\Image\'+inttostr(nmm_index)+'\'+nmm_strAttachFileName;  
                                                      ForceDirectories(nmm_strAttachFilePath);  
                                                      nmm_strAttachFilePath   :=   nmm_strAttachFilePath+'\'+nmm_strAttachFileName;  
                                                      try  
                                                      if   not   fileexists(nmm_strAttachFilePath)   then  
                                                      tidattachment(nmg_msgBody.MessageParts.Items[nmm_index]).SaveToFile(nmm_strAttachFilePath);  
                                                      except  
                                                      continue;  
                                                      end;  
                                                      //*************************替换图片src****************  
                                                      nmg_Reg.Expression   :='src\s*=\s*"?[^\s]*@[^\s]*"?';  
                                                      nmm_strbody   :=nmg_stsContent.text;  
                                                      if   nmg_reg.Exec(nmm_strbody)   then  
                                                            begin  
                                                                nmm_strbody:=StringReplace(nmm_strbody,nmg_reg.Match[0],  
                                                                                        'src='+nmm_strAttachFileUrl,[rfReplaceAll,   rfIgnoreCase]);  
                                                                nmg_stsContent.Text   :=   nmm_strbody;  
                                                            end;  
                                                      //****************************************************  
                                                end;  
                                  end;  
                  end;  
          if   nmg_blnHasAttOut   =true   then   FinishAttachHtm(nmm_strDirName+'\mailatt.htm');  
          FinishContentHtm(nmm_strDirName+'\mailcont.htm');  
          //******************************收取完成************************************  
          nmg_reRunTimeErr:=reSuccess;  
          nmg_arrAllErrString[Ord(reGetReceiveErr)]   :=   '收取指定邮件成功.';  
  except  
          ON   E:Exception   do  
          begin  
                Result   :=   '';  
                nmg_reRunTimeErr:=reGetReceiveErr;  
                nmg_arrAllErrString[Ord(nmg_reRunTimeErr)]   :=   E.Message;  
          end;  
  end;  
      Result   :=   nmg_reRunTimeErr;  
  end;  
 

多谢高手,我先研究一下,稍后给分

to:zuoansuifeng,你代码中的以下几个函数的代码能再给一下啊。  
  FormAttachHtm,DecodeHeader,FinishAttachHtm,FinishContentHtm,nmg_stsAttach  
  先谢了

zuoansuifeng好人

我自己已经搞定了    
  不过还是谢谢     zuoansuifeng(左岸)  
   
  散分结贴了

抱歉!评论已关闭.