为incoming mail绑定事件,SPEmailEventReceiver

 1) 新建 class 项目, 添加Microsoft.Sharepoint.dll引用。新建class并继承SPEmailEventReceiver

 

 

ExpandedBlockStart.gif 代码
using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Text;
using  Microsoft.SharePoint;
using  Microsoft.SharePoint.Utilities;

namespace  Sample{
    
public   class  Class1 : SPEmailEventReceiver
    {
        
public   override   void  EmailReceived(SPList oList, SPEmailMessage oMessage,   string  strReceiverData)   
        {  
            SPListItem oListItem 
=  oList.Items.Add();  
            oListItem[
" Title " =  oMessage.Headers[ " Subject " ];  
            oListItem[
" Body " =  oMessage.HtmlBody;  
            oListItem.Update();

            
foreach  (SPEmailAttachment attachment  in  oMessage.Attachments)
            {
                
byte [] attachmentArray  =   new   byte [attachment.ContentStream.Length];
                attachment.ContentStream.Read(attachmentArray, 
0 , ( int )attachment.ContentStream.Length);
                oList.RootFolder.Files.Add(attachment.FileName, attachmentArray);
            }
        }
    }
}


 

 

 

 2) 新建console项目,为列表绑定事件。

 

 

ExpandedBlockStart.gif 代码
using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Text;
using  Microsoft.SharePoint;

namespace  RegisterEvent
{
    
class  Program
    {
        
static   void  Main( string [] args)
        {
            
string  url  =   @" http://mossdev1/fa/corpsystem " ;
            SPSite curSite 
=   new  SPSite(url);
            SPWeb curWeb 
=  curSite.OpenWeb();

            
int  t  =   0 ;
            
foreach  (SPList list  in  curWeb.Lists)
            {
                Console.WriteLine(t
++   +   " "   +  list.Title);
            }
            SPList commentsList 
=  curWeb.Lists[ " title value " ];

            
string  asmName  =   " IISZ.SP.MailHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=a3efae80b726e354 " ;
            
string  className  =   " IISZ.SP.MailHandler.Class1 " ;

            
for  (  int  i  =   0  ; i  <   commentsList.EventReceivers.Count ; i  ++  )
            {
                Console.Write(commentsList.EventReceivers[i].Class.ToString());
                 commentsList.EventReceivers[i].Delete();
                 Console.WriteLine(
"  -- deleted! " );
            }
            commentsList.EventReceivers.Add(SPEventReceiverType.EmailReceived, asmName, className);
            
foreach  (SPEventReceiverDefinition srd  in  commentsList.EventReceivers)
            {
                Console.WriteLine(srd.Class.ToString() 
+   "  -- added! "  );
            }

            Console.ReadLine();
        }
    }
}

 

 

3) 调试绑定的进程为owstimer.exe

 

4) 发送附件为txt的文本文件,报错。查看事件管理器为:

Error loading and running event receiver Sample.Class1 in Sample, Version=1.0.0.0, Culture=neutral, PublicKeyToken=a3efae80b726e354. Additional information is below.

: Value does not fall within the expected range.

 

5) 单步调试查到读取message.Attachments 报错,怀疑是文档库的问题,绑定事件到默认的文档库,同样错误。绑定到Announcements,OK.

 

6) 结论:document library 可以绑定邮件接收处理事件,但得不到附件信息。Announcements 一切可以。为什么?不知道。

 

浪费一整天,我是差不多先生。。。。

 

参考文档:

http://blogs.msdn.com/malag/archive/2009/05/13/attachments-disappear-with-custom-email-event-handler.aspx

转载于:https://www.cnblogs.com/lfwolf/archive/2010/01/19/1651476.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值