Search code examples
c#sharepointsharepoint-2010event-receiver

Getting the email message (.eml) as an attachment to SharePoint ListItem


I have an SPEmailEventReceiver that receives an email message, adds a listitem to the list, loops through the email message attachments collection and adds them to the listitem attachment collection. I also need to get the email message itself (.eml file) and add it to the ListItem attachments collection. I am able to get the email body, subject, sender...etc, but i cannot get the message itself. Any suggestions on what i can use? Here's what I have so far.

   public class EInvoiceEmailEventReceiver : SPEmailEventReceiver
    {
    /// <summary>
    /// The list received an e-mail message.
    /// </summary>
    public override void EmailReceived(SPList list, SPEmailMessage emailMessage, String receiverData)
    {
        if (list == null)
            throw new ArgumentNullException("list", "null list parameter in EmailReceived");

        if (emailMessage == null)
            throw new ArgumentNullException("emailMessage", "null emailMessage parameter in EmailReceived");
        try
        {
            AddItemFromEmail(list, emailMessage, receiverData);
        }
        catch (Exception ex)
        {
            DiagnosticsService.WriteToLocalLog("ERROR: Error while adding eInvoice item from email: " +     
        }
    }

    private void AddItemFromEmail(SPList list, SPEmailMessage emailMessage, String receiverData)
     {
        string subject;
        try
        {
           subject = emailMessage.Headers["Subject"] ?? string.Empty;
        }
        catch
        {
            subject = string.Empty;
        }

        SPListItem item = list.Items.Add();

        SetItemFieldValue(item, "Title", "Crreated from email on " + DateTime.Now.ToString("yyyy-MM-dd 
                          HH:mm:ss.FFFFFFF"));
        SetItemFieldValue(item, "EmailFromAddress", emailMessage.Sender);

        SPAttachmentCollection itemAttachments = item.Attachments;
        base.EmailReceived(list, emailMessage, receiverData);
        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);
                    itemAttachments.Add(emailAttachment.FileName, emailAttachmentBytes);
                }
                catch (Exception ex)
                {
                    DiagnosticsService.WriteToLocalLog("Error while moving attachment from email to eInvoice  
                    item: " + ex.Message, LogLogLevel.Error, CategoryId.CustomAction);
                }
            }
           }

            item.Update();
        }
    private static void SetItemFieldValue(SPListItem item, string fieldName, string value)
     {
        try
        {
            item[fieldName] = value ?? string.Empty;
        }
        catch
        {
            DiagnosticsService.WriteToLocalLog(string.Format("Error while setting field {0} in eInvoice list.",   fieldName), LogLogLevel.Error, CategoryId.CustomAction);
        }
       }
     }
   }

Solution

  • You can use GetMessageStream method of SPEmailMessage object to do that.

    var emailStream = emailMessage.GetMessageStream();
    var emailAsBytes = new byte[emailStream.Length];
    emailStream.Read(emailAsBytes, 0, emailStream.Length);
    itemAttachments.Add('Message.eml', emailAsBytes);