0

I've written an Event Receiver (SPEmailEventReceiver) which is used to populate metadata based on the email message contents. The Event Receiver saves the Attachments in the same Document Library as the Email message (which works as intended), However, there are still copies of these attachments in the actual email message.

What I would like to do is delete the Attachments from the Email message once they are saved to the Document Library as separate files. How can this be achieved programmatically?

  • I'm not understanding. Where do you want to save the Attachments after they are added to the Doc library? – Michael Colbs Jan 26 '15 at 17:13
  • Once the attachments are saved to the doc library, I want to remove/delete them from the email message (.eml file), to prevent duplication. – Stuart C Jan 27 '15 at 13:16
1

the best solution to solve this is to create a new file based on the stream, headers and parameters of the e-mail. Delete the original file SPEmail file.

It is not possible to remove attachments of the SPEmail nor adding files to it. The only possible way is write to original message to a new file. Microsoft does it the same way within the timer service.

protected static void SaveOriginalMessageAsAttachment(SPEmailMessage message, SPListItem item)
{
    if (!item.ParentList.EnableAttachments)
    {
        return;
    }
    Stream messageStream = message.MessageStream;
    long position = messageStream.Position;
    messageStream.Seek(0L, SeekOrigin.Begin);
    byte[] array = new byte[messageStream.Length];
    messageStream.Read(array, 0, (int)messageStream.Length);
    item.Attachments.Add("OriginalMessage.eml", array);
    messageStream.Seek(position, SeekOrigin.Begin);
}
  • Thanks for your answer. It's along the lines of what I want to do but instead of saving the email as an attachment, I want it to be an item in the document library. When you open the email, you should see the message body etc but any attachments that were there, should be stripped out and added to the library as individual items. This is what happens when using the default incoming mail settings. – Stuart C Jan 29 '15 at 11:33
1

Getting the message stream using SPEMailMessage.GetMessageStream() still contains the attachments however I was able to use String.SubString() to remove everything after closing <\html>, leaving the message body and headers (minus the attachments).

SPFolder folder = list.RootFolder;
string folderUrl = folder.Url;
string emailUrl = string.Format(@"{0}/{1}.eml", folderUrl, GenerateFileName());

byte[] emailBinaryContent = null;
string emailContent = string.Empty;

using (Stream emailStream = emailMessage.GetMessageStream())
{
    using (StreamReader sr = new StreamReader(emailStream))
    {
        emailContent = sr.ReadToEnd();
        int index = emailContent.IndexOf("</html>");
        if (index != -1)
        {
            emailContent = emailContent.Substring(0, index);
        }
    }
}

emailBinaryContent = Encoding.UTF8.GetBytes(emailContent);

SPFile emailFile = folder.Files.Add(emailUrl, emailBinaryContent, true);
SPItem emailItem = emailFile.Item;

emailItem.Update();
list.Update();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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