获取电子邮件消息(.eml)作为SharePoint ListItem的附件

时间:2014-06-23 16:23:50

标签: c# sharepoint sharepoint-2010 event-receiver

我有一个SPEmailEventReceiver接收电子邮件消息,将listitem添加到列表中,循环遍历电子邮件附件集合并将它们添加到listitem附件集合中。 我还需要获取电子邮件本身(.eml文件)并将其添加到ListItem附件集合中。我能够获得电子邮件正文,主题,发件人等等,但我无法得到消息本身。关于我可以使用什么的任何建议?这是我到目前为止所拥有的。

   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);
        }
       }
     }
   }

1 个答案:

答案 0 :(得分:1)

您可以使用SPEmailMessage对象的GetMessageStream方法来执行此操作。

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