Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I'm trying to link an SPEmailEventReceiver to my document library..

First of all I created the EventReceiver using the Visual Studio 2010 Add -> Event Receiver -> List Email Events -> Document Library -> The list received an e-mail message

Then I wrote down the code in the generated class:

namespace ProvaEmailEvent.EventReceiver1
{
/// <summary>
/// List Email Events
/// </summary>
public class EventReceiver1 : SPEmailEventReceiver
{
   /// <summary>
   /// The list received an e-mail message.
   /// </summary>
   public override void EmailReceived(SPList list, SPEmailMessage emailMessage, String receiverData)
   {
       SPUser currentUser = list.ParentWeb.CurrentUser;
       Guid listGUID = list.ID;

       SPSecurity.RunWithElevatedPrivileges(() =>
       {
           using (SPSite sito = new SPSite(list.ParentWebUrl))
           {
               using (SPWeb web = sito.OpenWeb())
               {

                   foreach (SPEmailAttachment attach in emailMessage.Attachments)
                   {
                       SPList elevatedList = web.Lists[listGUID];
                       SPFile file = elevatedList.RootFolder.Files.Add(attach.FileName, attach.ContentStream);
                       SPListItem item = file.Item;
                       item.BreakRoleInheritance(false);
                       item.SystemUpdate();
                       AssignPermissionsToItem(item, currentUser, SPRoleType.Contributor);
                       item.SystemUpdate();
                   }
               }
           }
       });
   }

   public static void AssignPermissionsToItem(SPListItem item, SPPrincipal obj, SPRoleType roleType)
   {
       SPRoleAssignment roleAssignment = new SPRoleAssignment(obj);
       SPRoleDefinition roleDefinition = item.Web.RoleDefinitions.GetByType(roleType);

       roleAssignment.RoleDefinitionBindings.Add(roleDefinition);

       item.RoleAssignments.Add(roleAssignment);
   }

The elements.xml file contains:

 <?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
 <Receivers ListTemplateId="101">
  <Receiver>
    <Name>EventReceiver1EmailReceived</Name>
    <Type>EmailReceived</Type>
    <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
    <Class>ProvaEmailEvent.EventReceiver1.EventReceiver1</Class>
    <SequenceNumber>10000</SequenceNumber>
  </Receiver>

</Receivers>
</Elements>

In sharepoint log I found messages like this (when sending an email):

 Documents _ Microsoft.SharePoint.Utilities.SPEmailMessage __   18fd777b-039b-402b-ad8a-1e0a5e534d88
 The user does not exist or is not unique
Warning     An error occurred while processing the incoming e-mail file C:\inetpub\mailroot\Drop\8d8bea9e01cd963600000007.eml. The error was: Object reference not set to an instance of an object..    18fd777b-039b-402b-ad8a-1e0a5e534d88
    The Incoming E-Mail service has completed a batch.  The elapsed time was 00:00:00.0156250.  The service processed 1 message(s) in total.  Errors occurred processing 1 message(s): Message ID: <SP2010DEVNsErKkSoii00000007@SP2010DEV.virtualsp.dev> 

I tried also with blank code or with a single instruction that wrote down something in the logs...

Any suggestion?

share|improve this question
add comment

1 Answer 1

up vote 3 down vote accepted

It might be that you have a nullpointer exception.
I would try debugging the receiver, like this:

  • Deploy your solution.
  • Restart the SharePoint Timer Service (From services).
  • In VS click Tools -> Attach to Process.
  • Click Refresh
  • Find the one called OWSTIMER.EXE and attach to this.
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.