2

Im trying to implement custom SPEmailEventReceiver. I created class simular to this: https://stackoverflow.com/questions/24370749/getting-the-email-message-eml-as-an-attachment-to-sharepoint-listitem Reciever is succesfuly called and list item is created. But the result is not the same as without receiver.

witout receiver: enter image description here

with receiver: enter image description here

without receiver there is referece to email message from which was item created. I would like to get this result from SPEmailEventReceiver. Is there any option how to do this?

0

I finnaly found the solution.

Problem was that no all fields was filled. What brings me to soluion was customization of DispForm for ListItem in my Discussion List. I found that there is some hidden field and one of them is EmailHeaders. I fill this field with all headers form email in format "{Name}:{Value}\n". And based on this field the relation to Email message was created.

This is my code:

public class Inbox_ITHD_EmailReciever : SPEmailEventReceiver
{
    public override void EmailReceived(SPList list, SPEmailMessage emailMessage, string receiverData)
    {
        try
        {
            SPListItem item = list.AddItem();

            Stream emailStream = emailMessage.GetMessageStream();
            byte[] emailAsBytes = new byte[emailStream.Length];
            emailStream.Read(emailAsBytes, 0, (int)emailStream.Length);
            item.Attachments.Add("Message.eml", emailAsBytes);

            SPEmailAttachmentCollection emailAttachments = emailMessage.Attachments;

            if (emailAttachments != null)
            {
                foreach (SPEmailAttachment emailAttachment in emailAttachments)
                {
                    try
                    {
                        byte[] emailAttachmentBytes = new byte[emailAttachment.ContentStream.Length];
                        emailAttachment.ContentStream.Read(emailAttachmentBytes, 0, emailAttachmentBytes.Length);
                        item.Attachments.Add(emailAttachment.FileName, emailAttachmentBytes);
                    }
                    catch (Exception ex)
                    {
                        //LOG
                    }
                }
            }

            m.FindEmailContent();

            item["EmailFrom"] = emailMessage.Sender;
            item["EmailSender"] = emailMessage.Sender;
            item["EmailTo"] = emailMessage.Headers["To"];
            item["EmailSubject"] = emailMessage.Headers["Subject"];
            item["Body"] = emailMessage.HtmlBody;

            item["Subject"] = emailMessage.Headers["Subject"];
            StringBuilder headers = new StringBuilder();
            foreach (SPEmailHeader h in emailMessage.Headers)
            {
                headers.AppendFormat("{0}:{1}\n", h.Name, h.Value);
            }
            item["EmailHeaders"] = headers.ToString();
            item.Update();
            list.Update();
        }
        catch (Exception ex)
        {
            //LOG
        }
    }

    public static string StreamToString(Stream stream)
    {
        stream.Position = 0;
        using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
        {
            return reader.ReadToEnd();
        }
    }
}

I think that with this approach also events will be created correctly in sharepoint(i dont test it yet).

Next problem which I faced was creation of mail body. If mail is only text or html there is no problem (SPMailMessage contains fiendy methods for this puprpose). Problem is embedded images, which are included in mail as a MIME part. SPMailMessage restricts access to Mail structure.

In debbufer i found that there is structure well created with Microsoft.Exchange.Data.Mime namespace, but im not possible o use private varialbe m_mimemessage in SPMailMessage. As i found there is no possiblity to include Microsoft.Exchange.Data.Mime namespace to sharepoint project (if you know how please let me know).

What i have done is usage of OpenPop. This library can split raw mail message to multiple parts and im possible to get attachements, embeded images and mail text. I add some functionality to lookup for storing embeded images in discussion attachements and change ContentID links in email message to http links to images. This approach is not described in code.

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.