MOSS Forum

 
Home » Forum » MOSS       Ask a questionRSS Feeds

EmailRecived EvenHandler for document Library

  Asked By: Azra Azra         Date: Aug 11, 2010      Category: MOSS      Views: 798
 

hi
I have created the following code to handle document library email received event handling



public override void EmailReceived(SPList list, SPEmailMessage emailMessage, string receiverData)
{

SPSecurity.RunWithElevatedPrivileges(delegate()
{
try
{


//Declaration Of Variables.
string strTo = ""; string strSubject = ""; string strBody = ""; string strCC = ""; string strFrom = ""; string strCreatedBy = ""; string strImportance = "";
base.EmailReceived(list, emailMessage, receiverData);
SPView view = list.DefaultView;
//Getting MailMessage Values In String Variables.
strSubject = emailMessage.Headers["Subject"].ToString();
strBody = emailMessage.HtmlBody.ToString();
strTo = emailMessage.Headers["To"].ToString();
strFrom = emailMessage.EnvelopeSender.ToString();
strCreatedBy = emailMessage.Headers["From"].ToString();
if (emailMessage.Headers["CC"] != null)
{ strCC = emailMessage.Headers["CC"].ToString(); }
if (emailMessage.Headers["Importance"] != null)
{ strImportance = emailMessage.Headers["Importance"].ToString(); }

SPListItem oListItem = list.Items.Add();
if (!list.Fields.ContainsField("Priority"))
{
list.Fields.Add("Priority", SPFieldType.Text, true);
view.ViewFields.Add("Priority");
view.Update();
}

oListItem["Title"] = strSubject;
oListItem["E-Mail To"] = strTo;
oListItem["E-Mail From"] = strFrom;
oListItem["Body"] = strBody;
oListItem["Priority"] = strImportance;
oListItem.Update();

//Get the Message Stream to save it as .eml.
byte[] content = null;
string strPath = "c:\\SharePointMailDrop\\";

using (Stream objMessageStream = emailMessage.GetMessageStream())
{
content = new byte[objMessageStream.Length];
objMessageStream.Read(content, 0, (int)objMessageStream.Length);
objMessageStream.Close();

if (!Directory.Exists(strPath))
{
Directory.CreateDirectory(strPath);
}

using (FileStream messageFileStream = new FileStream(strPath + strSubject +".eml", FileMode.Create))
{
messageFileStream.Write(content, 0, content.Length);
list.RootFolder.Files.Add(strSubject +".eml", messageFileStream);
messageFileStream.Close();

}


}




}
catch (System.Exception ex)
{
SPUtility.TransferToErrorPage(ex.Message.ToString());
}
});




in this code oListItem.Update(); is giving me error use SPFileCollection.Add to add a doc to doc library.
What could be the problem

}


 

1 Answer Found

 
Answer #1       Answered By: Sirisha Reddimasi          Answered On: Aug 13, 2010       

Hi,
Please try to upload doc first by using the below code
SPFile uploadedFile = _DocumentLibrary.Files.Add();
and then get this item as below
SPListItem listItem = uploadedFile.Item;
and then by using this listItem try to update all other columns for this doc.

 
Didn't find what you were looking for? Find more on EmailRecived EvenHandler for document Library Or get search suggestion and latest updates.


Your Answer
  • Answer should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].