设为首页 收藏本站
查看: 1114|回复: 0

[经验分享] 如何实现用Delphi访问Outlook Express的收发邮件箱中的邮件?

[复制链接]

尚未签到

发表于 2015-9-14 09:54:27 | 显示全部楼层 |阅读模式
  如何实现用delphi访问outlook express的收发邮件箱中的邮件?
   unit   MapiControl;     interface     uses     Windows,   Messages,   SysUtils,   Classes,   Graphics,   Controls,   Forms,   Dialogs;     type     {   Introducing   a   new   Type   of   Event   to   get   the   Errorcode   }     TMapiErrEvent   =   procedure(Sender:   TObject;   ErrCode:   Integer)   of   object;     TMapiControl   =   class(TComponent)     constructor   Create(AOwner:   TComponent);   override;     destructor   Destroy;   override;     private     {   Private-Deklarationen   }     FSubject:   string;     FMailtext:   string;     FFromName:   string;     FFromAdress:   string;     FTOAdr:   TStrings;     FCCAdr:   TStrings;     FBCCAdr:   TStrings;     FAttachedFileName:   TStrings;     FDisplayFileName:   TStrings;     FShowDialog:   Boolean;     FUseAppHandle:   Boolean;     {   Error   Events:   }     FOnUserAbort:   TNotifyEvent;     FOnMapiError:   TMapiErrEvent;     FOnSuccess:   TNotifyEvent;     {   +>   Changes   by   Eugene   Mayevski   [mailto:Mayevski@eldos.org]}     procedure   SetToAddr(newValue   :   TStrings);     procedure   SetCCAddr(newValue   :   TStrings);     procedure   SetBCCAddr(newValue   :   TStrings);     procedure   SetAttachedFileName(newValue   :   TStrings);     {   +<   Changes   }     protected     {   Protected-Deklarationen   }     public     {   Public-Deklarationen   }     ApplicationHandle:   THandle;     procedure   Sendmail();     procedure   Reset();     published     {   Published-Deklarationen   }     property   Subject:   string   read   FSubject   write   FSubject;     property   Body:   string   read   FMailText   write   FMailText;     property   FromName:   string   read   FFromName   write   FFromName;     property   FromAdress:   string   read   FFromAdress   write   FFromAdress;     property   Recipients:   TStrings   read   FTOAdr   write   SetTOAddr;     property   CopyTo:   TStrings   read   FCCAdr   write   SetCCAddr;     property   BlindCopyTo:   TStrings   read   FBCCAdr   write   SetBCCAddr;     property   AttachedFiles:   TStrings   read   FAttachedFileName   write   SetAttachedFileName;     property   DisplayFileName:   TStrings   read   FDisplayFileName;     property   ShowDialog:   Boolean   read   FShowDialog   write   FShowDialog;     property   UseAppHandle:   Boolean   read   FUseAppHandle   write   FUseAppHandle;     {   Events:   }     property   OnUserAbort:   TNotifyEvent   read   FOnUserAbort   write   FOnUserAbort;     property   OnMapiError:   TMapiErrEvent   read   FOnMapiError   write   FOnMapiError;     property   OnSuccess:   TNotifyEvent   read   FOnSuccess   write   FOnSuccess;     end;     procedure   Register;     implementation     uses   Mapi;     {   Register   the   component:   }     procedure   Register;     begin     RegisterComponents('expectIT',   [TMapiControl]);     end;     {   TMapiControl   }     constructor   TMapiControl.Create(AOwner:   TComponent);     begin     inherited   Create(AOwner);     FOnUserAbort   :=   nil;     FOnMapiError   :=   nil;     FOnSuccess   :=   nil;     FSubject   :=   '';     FMailtext   :=   '';     FFromName   :=   '';     FFromAdress   :=   '';     FTOAdr   :=   TStringList.Create;     FCCAdr   :=   TStringList.Create;     FBCCAdr   :=   TStringList.Create;     FAttachedFileName   :=   TStringList.Create;     FDisplayFileName   :=   TStringList.Create;     FShowDialog   :=   False;     ApplicationHandle   :=   Application.Handle;     end;     procedure   TMapiControl.SetToAddr(newValue   :   TStrings);     begin     FToAdr.Assign(newValue);     end;     procedure   TMapiControl.SetCCAddr(newValue   :   TStrings);     begin     FCCAdr.Assign(newValue);     end;     procedure   TMapiControl.SetBCCAddr(newValue   :   TStrings);     begin     FBCCAdr.Assign(newValue);     end;     procedure   TMapiControl.SetAttachedFileName(newValue   :   TStrings);     begin     FAttachedFileName.Assign(newValue);     end;     {   +<   Changes   }     destructor   TMapiControl.Destroy;     begin     FTOAdr.Free;     FCCAdr.Free;     FBCCAdr.Free;     FAttachedFileName.Free;     FDisplayFileName.Free;     inherited   destroy;     end;     {   Reset   the   fields   for   re-use}     procedure   TMapiControl.Reset;     begin     FSubject   :=   '';     FMailtext   :=   '';     FFromName   :=   '';     FFromAdress   :=   '';     FTOAdr.Clear;     FCCAdr.Clear;     FBCCAdr.Clear;     FAttachedFileName.Clear;     FDisplayFileName.Clear;     end;{   Send   the   Mail   via   the   API,   this   procedure   composes   and   sends     the   Email   }     procedure   TMapiControl.Sendmail;     var     MapiMessage:   TMapiMessage;     MError:   Cardinal;     Sender:   TMapiRecipDesc;     PRecip,   Recipients:   PMapiRecipDesc;     PFiles,   Attachments:   PMapiFileDesc;     i:   Integer;     AppHandle:   THandle;     begin     {   First   we   store   the   Application   Handle,   if   not     the   Component   might   fail   to   send   the   Email   or     your   calling   Program   gets   locked   up.   }     AppHandle   :=   Application.Handle;     {   We   need   all   recipients   to   alloc   the   memory   }     MapiMessage.nRecipCount   :=   FTOAdr.Count   +   FCCAdr.Count   +   FBCCAdr.Count;     GetMem(Recipients,   MapiMessage.nRecipCount   *   sizeof(TMapiRecipDesc));     try     with   MapiMessage   do     begin     ulReserved   :=   0;     {   Setting   the   Subject:   }     lpszSubject   :=   PChar(Self.FSubject);     {   ...   the   Body:   }     lpszNoteText   :=   PChar(FMailText);     lpszMessageType   :=   nil;     lpszDateReceived   :=   nil;     lpszConversationID   :=   nil;     flFlags   :=   0;     {   and   the   sender:   (MAPI_ORIG)   }     Sender.ulReserved   :=   0;     Sender.ulRecipClass   :=   MAPI_ORIG;     Sender.lpszName   :=   PChar(FromName);     Sender.lpszAddress   :=   PChar(FromAdress);     Sender.ulEIDSize   :=   0;     Sender.lpEntryID   :=   nil;     lpOriginator   :=   @Sender;     PRecip   :=   Recipients;     {   We   have   multiple   recipients:   (MAPI_TO)     and   setting   up   each:   }     if   nRecipCount   >   0   then     begin     for   i   :=   1   to   FTOAdr.Count   do     begin     PRecip^.ulReserved   :=   0;     PRecip^.ulRecipClass   :=   MAPI_TO;     {   lpszName   should   carry   the   Name   like   in   the     contacts   or   the   adress   book,   I   will   take   the     email   adress   to   keep   it   short:   }     PRecip^.lpszName   :=   PChar(FTOAdr.Strings[i   -   1]);     {   If   you   use   this   component   with   Outlook97   or   2000     and   not   some   of   Express   versions   you   will   have   to   set     'SMTP:'   in   front   of   each   (email-)   adress.   Otherwise     Outlook/Mapi   will   try   to   handle   the   Email   on   itself.     Sounds   strange,   just   erease   the   'SMTP:',   compile,   compose     a   mail   and   take   a   look   at   the   resulting   email   adresses     (right   click).     }     PRecip^.lpszAddress   :=   PChar('SMTP:'   +   FTOAdr.Strings[i   -   1]);     PRecip^.ulEIDSize   :=   0;     PRecip^.lpEntryID   :=   nil;     Inc(PRecip);     end;     {   Same   with   the   carbon   copy   recipients:   (CC,   MAPI_CC)   }     for   i   :=   1   to   FCCAdr.Count   do     begin     PRecip^.ulReserved   :=   0;     PRecip^.ulRecipClass   :=   MAPI_CC;     PRecip^.lpszName   :=   PChar(FCCAdr.Strings[i   -   1]);     PRecip^.lpszAddress   :=   PChar('SMTP:'   +   FCCAdr.Strings[i   -   1]);     PRecip^.ulEIDSize   :=   0;     PRecip^.lpEntryID   :=   nil;     Inc(PRecip);     end;     {   ...   and   the   blind   copy   recipients:   (BCC,   MAPI_BCC)   }     for   i   :=   1   to   FBCCAdr.Count   do     begin     PRecip^.ulReserved   :=   0;     PRecip^.ulRecipClass   :=   MAPI_BCC;     PRecip^.lpszName   :=   PChar(FBCCAdr.Strings[i   -   1]);     PRecip^.lpszAddress   :=   PChar('SMTP:'   +   FBCCAdr.Strings[i   -   1]);     PRecip^.ulEIDSize   :=   0;     PRecip^.lpEntryID   :=   nil;     Inc(PRecip);     end;     end;     lpRecips   :=   Recipients;     {   Now   we   process   the   attachments:   }     if   FAttachedFileName.Count   >   0   then     begin     nFileCount   :=   FAttachedFileName.Count;     GetMem(Attachments,   MapiMessage.nFileCount   *   sizeof(TMapiFileDesc));     PFiles   :=   Attachments;     {   Fist   setting   up   the   display   names   (without   path):   }     FDisplayFileName.Clear;     for   i   :=   0   to   FAttachedFileName.Count   -   1   do     FDisplayFileName.Add(ExtractFileName(FAttachedFileName));     if   nFileCount   >   0   then     begin     {   Now   we   pass   the   attached   file   (their   paths)   to   the     structure:   }     for   i   :=   1   to   FAttachedFileName.Count   do     begin     {   Setting   the   complete   Path   }     Attachments^.lpszPathName   :=   PChar(FAttachedFileName.Strings[i   -   1]);     {   ...   and   the   displayname:   }     Attachments^.lpszFileName   :=   PChar(FDisplayFileName.Strings[i   -   1]);     Attachments^.ulReserved   :=   0;     Attachments^.flFlags   :=   0;     {   Position   has   to   be   -1,   please   see   the   WinApi   Help     for   details.   }     Attachments^.nPosition   :=   Cardinal(-1);     Attachments^.lpFileType   :=   nil;     Inc(Attachments);     end;     end;     lpFiles   :=   PFiles;     end     else     begin     nFileCount   :=   0;     lpFiles   :=   nil;     end;     end;     {   Send   the   Mail,   silent   or   verbose:     Verbose   means   in   Express   a   Mail   is   composed   and   shown   as   setup.     In   non-Express   versions   we   show   the   Login-Dialog   for   a   new     session   and   after   we   have   choosen   the   profile   to   use,   the     composed   email   is   shown   before   sending     Silent   does   currently   not   work   for   non-Express   version.   We   have     no   Session,   no   Login   Dialog   so   the   system   refuses   to   compose   a     new   email.   In   Express   Versions   the   email   is   sent   in   the     background.     }     if   FShowDialog   then     MError   :=   MapiSendMail(0,   AppHandle,   MapiMessage,   MAPI_DIALOG   or   MAPI_LOGON_UI   or   MAPI_NEW_SESSION,   0)     else     MError   :=   MapiSendMail(0,   AppHandle,   MapiMessage,   0,   0);     {   Now   we   have   to   process   the   error   messages.   There   are   some     defined   in   the   MAPI   unit   please   take   a   look   at   the   unit   to   get     familiar   with   it.     I   decided   to   handle   USER_ABORT   and   SUCCESS   as   special   and   leave     the   rest   to   fire   the   "new"   error   event   defined   at   the   top   (as     generic   error)     Not   treated   as   special:     MAPI_E_AMBIGUOUS_RECIPIENT,     MAPI_E_ATTACHMENT_NOT_FOUND,     MAPI_E_ATTACHMENT_OPEN_FAILURE,     MAPI_E_BAD_RECIPTYPE,     MAPI_E_FAILURE,     MAPI_E_INSUFFICIENT_MEMORY,     MAPI_E_LOGIN_FAILURE,     MAPI_E_TEXT_TOO_LARGE,     MAPI_E_TOO_MANY_FILES,     MAPI_E_TOO_MANY_RECIPIENTS,     MAPI_E_UNKNOWN_RECIPIENT:     }     case   MError   of     MAPI_E_USER_ABORT:     begin     if   Assigned(FOnUserAbort)   then     FOnUserAbort(Self);     end;     SUCCESS_SUCCESS:     begin     if   Assigned(FOnSuccess)   then     FOnSuccess(Self);     end     else   begin     if   Assigned(FOnMapiError)   then     FOnMapiError(Self,   MError);     end;     end;     finally     {   Finally   we   do   the   cleanups,   the   message   should   be   on   its   way   }     FreeMem(Recipients,   MapiMessage.nRecipCount   *   sizeof(TMapiRecipDesc));     end;     end;     {     Please   treat   this   as   free.   If   you   improve   the   component     I   would   be   glad   to   get   a   copy.     }     end.

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-113394-1-1.html 上篇帖子: Outlook Express 收发邮件出现"0x800CCC0F"错误代码解决方法 下篇帖子: VSTO 得到Office文档的选中内容(Word、Excel、PPT、Outlook)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表