Hi,
I have a particular scenario where a batch process performs the following steps:
- create and save a .msg file
- load and send the message i.e. MailMessage.Load(msgfile) >> ExchangeClient.Send(msg)
- download and save the sent item (I’ve included code sample below)
n.b. The steps may each be performed independently of each other i.e. step 1, then x minutes later step 2 is performed and so on.
MY QUESTION: How would I go about identifying the email in the sent items? I was thinking of using the message id, but it seems to be empty before it’s sent.
I’m really looking for the most appropriate way to send an email and then later download and save the sent copy of that email from an exchange server.
n.b. I can’t use something as simple as sender, recipient, or date information because this batch process could send hundreds if not thousands of messages all based on a small number of templates.
Thanks in advance for any help you can offer!!
I’ve found the links below, but they don’t help in this situation.
------- CODE SAMPLE -------
protected override void PerformExecute()
{
ExchangeClient client = EmailHelper.CreateClient(_server, _domain, _user, _password);
var query = BuildQuery(_messageId);
var messageInfo = GetMessageInfo(client, query);
var message = client.FetchMessage(messageInfo.UniqueUri);
message.Save(_messageFile, SaveOptions.DefaultMsg);
}
#region Private Methods
private ExchangeMessageInfo GetMessageInfo(ExchangeClient client, MailQuery query)
{
var match = client.ListMessages(client.MailboxInfo.SentItemsUri, query, false);
if (match.Count == 0)
throw new MessageNotFoundException(query.ToString());
var messageInfo = match[0];
return messageInfo;
}
private MailQuery BuildQuery(string messageId)
{
var queryBuilder = new ExchangeQueryBuilder();
// what should I search for?
var query = queryBuilder.GetQuery();
return query;
}
-------