Here the users will send emails with subject “DiscussionTitle [Discussion ID]” format. If there is a discussion thread present with specified “[Discussion ID]” then it will add the incoming email as a reply in that thread. If ID is not supplied and if there is no discussion thread present with “DiscussionTitle” then it will create a new discussion but if there is any discussion already available then it will ignore that incoming mail.

 

   1:  public override void EmailReceived(SPList list, Microsoft.SharePoint.Utilities.SPEmailMessage emailMessage, string receiverData)
   2:              {
   3:              base.EmailReceived(list, emailMessage, receiverData);
   4:              bool flag = false;
   5:              int id = -1;
   6:              string subject = emailMessage.Headers["Subject"].ToString();
   7:              SPUser ous = list.ParentWeb.AllUsers.GetByEmail(emailMessage.Sender);
   8:              SPListItemCollection viewColl = list.GetItems(list.Views["Subject"]);
   9:              foreach (SPListItem item1 in viewColl)
  10:              {
  11:                  SPListItem item2 = list.GetItemById(item1.ID);
  12:                  if ((item2["Subject"].Equals(subject.Substring(subject.IndexOf(":") + 2))) || (item2["Subject"].Equals(subject)))
  13:                  {
  14:                      flag = true;
  15:                      break;
  16:                  }
  17:              }
  18:   
  19:              if (subject.IndexOf("-[") >= 0)
  20:              {
  21:                  //if ((item2["Subject"].Equals(subject.Substring(subject.IndexOf(":") + 2, subject.IndexOf("-["))))
  22:   //|| (item2["Subject"].Equals(subject.Substring(0, subject.IndexOf("-[")))))
  23:                  {
  24:                      id = Convert.ToInt32(subject.Substring(subject.IndexOf("[") + 1, (subject.IndexOf("]") - subject.IndexOf("[") - 1)));
  25:                      SPListItem item2 = list.GetItemById(id);
  26:   
  27:                      SPListItem disc = SPUtility.CreateNewDiscussionReply(item2);
  28:                      disc["Body"] = emailMessage.HtmlBody;
  29:                      disc["TrimmedBody"] = emailMessage.HtmlBody;
  30:                      disc["Editor"] = ous;
  31:                      disc["Author"] = ous;
  32:                      disc["E-Mail Subject"] = emailMessage.Headers["Subject"].ToString();
  33:                      disc.Update();
  34:                      flag = true;
  35:                  }
  36:              }
  37:   
  38:              if (!flag)
  39:              {
  40:                  SPListItem disc = SPUtility.CreateNewDiscussion(list.Items, emailMessage.Headers["Subject"].ToString());
  41:                  disc["Body"] = emailMessage.HtmlBody;
  42:                  disc["TrimmedBody"] = emailMessage.HtmlBody;
  43:                  disc["Editor"] = ous;
  44:                  disc["Author"] = ous;
  45:                  disc["E-Mail Subject"] = emailMessage.Headers["Subject"].ToString();
  46:                  disc.Update();
  47:              }
  48:          }
  49:   

:: Please note that I would have just uploaded my initial code and you might want to consider proper optimization of the code and disposal of objects properly. I might not have updated the latest code here.