I have an application that is basically a mailing list manager. New messages are sent to an Exchange mailbox, from outside of our organization. Periodically the application runs, scans the mailbox for new messages, and forwards them to the appropriate users. For the most part this works fine, but messages that come from a specific outside user cause a "Data is Corrupt" message.
I have tried re-creating the messages that cause problems, both using Outlook and using a Gmail account. In both instances, the message went through the system with no problems. But this vendor is doing something that is causing me grief.
I've included some sample code below. What I would really like are tips for debugging these kinds of problems. It seems to me that "Data is Corrupt" is sort of a generic error.
private void ForwardMessage(EmailMessage msg, List<String> recipients, String replyTo)
ResponseMessage stepOne;
EmailMessage stepTwo;
Folder drafts;
ConversationId cid;
/*
* This is a little screwy.
* EmailMessage.CreateForward() is really useful, because it preserves
* attachments. But it mangles the message body and subject, so I have reset those.
* And it doesn't support setting the Reply To, so I have to do this
* in two steps: create the forward, save to Drafts, then get the
* message in the Drafts forward, and set the Reply To.
*/
cid = msg.ConversationId;
stepOne = msg.CreateForward();
stepOne.Body = msg.Body;
stepOne.Subject = msg.Subject;
foreach (String s in recipients)
{
stepOne.BccRecipients.Add(s);
}
stepOne.Save(WellKnownFolderName.Drafts);
/*
* There is a chance, though small, that another process
* could create a draft message in response to the message
* I'm in the process of forwarding. But it is unlikely,
* so I'm going to live with this. Extended Properties would
* be the safer way to do this, but I couldn't get them
* to make the trip.
*/
stepTwo = null;
drafts = Folder.Bind(msg.Service, WellKnownFolderName.Drafts);
foreach (EmailMessage em in drafts.FindItems(new ItemView(100)))
{
if (em.ConversationId.Equals(cid))
{
stepTwo = em;
}
}
if (stepTwo != null)
{
stepTwo.ReplyTo.Add(replyTo);
// This next line is where the "Data is Corrupt" error keeps happening
stepTwo.SendAndSaveCopy();
}
}