Monday, 15 August 2011

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



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

i have spemaileventreceiver receives email message, adds listitem list, loops through email message attachments collection , adds them listitem attachment collection. need email message (.eml file) , add together listitem attachments collection. able email body, subject, sender...etc, cannot message itself. suggestions on can use? here's have far.

public class einvoiceemaileventreceiver : spemaileventreceiver { /// <summary> /// list received 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"); seek { additemfromemail(list, emailmessage, receiverdata); } grab (exception ex) { diagnosticsservice.writetolocallog("error: error while adding einvoice item email: " + } } private void additemfromemail(splist list, spemailmessage emailmessage, string receiverdata) { string subject; seek { subject = emailmessage.headers["subject"] ?? string.empty; } grab { subject = string.empty; } splistitem item = list.items.add(); setitemfieldvalue(item, "title", "crreated 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) { seek { byte[] emailattachmentbytes = new byte[emailattachment.contentstream.length]; emailattachment.contentstream.read(emailattachmentbytes, 0, emailattachmentbytes.length); itemattachments.add(emailattachment.filename, emailattachmentbytes); } grab (exception ex) { diagnosticsservice.writetolocallog("error while moving attachment email einvoice item: " + ex.message, logloglevel.error, categoryid.customaction); } } } item.update(); } private static void setitemfieldvalue(splistitem item, string fieldname, string value) { seek { item[fieldname] = value ?? string.empty; } grab { diagnosticsservice.writetolocallog(string.format("error while setting field {0} in einvoice list.", fieldname), logloglevel.error, categoryid.customaction); } } } }

you can utilize getmessagestream method of spemailmessage object that.

var emailstream = emailmessage.getmessagestream(); var emailasbytes = new byte[emailstream.length]; emailstream.read(emailasbytes, 0, emailstream.length); itemattachments.add('message.eml', emailasbytes);

c# sharepoint sharepoint-2010 event-receiver

No comments:

Post a Comment