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 have a Document Library with an email associated with the OOTB feature. When I send to the library an email with some attachments, every attachment is stored with one of the OOTB methods (divided in folder by sender).

I need to do some operation whenever a new file is added so I tryed to add an event handler. I tried every method but:

  • When I upload a file from the SharePoint UI the event handler is triggered
  • When I upload a file by e-mail it is not

Is there any way to do that?

Thank you very much!

share|improve this question

1 Answer 1

up vote 3 down vote accepted

There are custom events for incoming emails, have you looked for "Email events"? If you add such Event Receiver you might have to handle the item correctly as well, or else it will not be inserted properly.
Like this:

public class OnEmail : SPEmailEventReceiver
{

   public override void EmailReceived(SPList list, SPEmailMessage emailMessage, String receiverData)
   {
       base.EmailReceived(list, emailMessage, receiverData);

       var item = list.AddItem();
       item["Title"] = emailMessage.Headers["Subject"];
       item["EmailSubject"] = emailMessage.Headers["Subject"];
       item["EmailFrom"] = emailMessage.Headers["From"];
       item["EmailHeaders"] = emailMessage.Headers;
       item["EmailSender"] = emailMessage.Headers["x-sender"];
       item["EmailTo"] = emailMessage.Headers["To"];
       item["Body"] = emailMessage.HtmlBody;
       emailMessage.Attachments.Cast<SPEmailAttachment>().ToList().ForEach(attachment =>
       {
           var array = new byte[attachment.ContentStream.Length];
           attachment.ContentStream.Read(array, 0, (int)attachment.ContentStream.Length);
           item.Attachments.Add(attachment.FileName, array);
       });
       item.Update();
   }
}
share|improve this answer

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.