Saving a Specific Sent Mail Message on Exchange Server

Hi,


I have a particular scenario where a batch process performs the following steps:

  1. create and save a .msg file
  2. load and send the message i.e. MailMessage.Load(msgfile) >> ExchangeClient.Send(msg)
  3. 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.

http://www.aspose.com/docs/display/emailnet/Save+Messages+from+Exchange+Server+Mailbox+in+EML+and+MSG+Format
http://www.aspose.com/docs/display/emailnet/Filter+Messages+from+Exchange+Mailbox+Based+on+Sender,+Recipient+or+Date

------- 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;
}
-------

Hi Richard,


Thank you for contacting Aspose support team.

We can use MailMessage header option to keep track of sent messages. When you create a message you may add a header as given below:

MailMessage msg = new MailMessage("sender@domain.com", "receiver@domain.com", “Subject”, “Body”);
String guid = Guid.NewGuid().ToString();
msg.Headers.Add_(“MyId”, guid);
msg.Save(@“Output.msg”,SaveOptions.DefaultMsgUnicode);
Here you may save this guid in database or wherever you want. Later you send the message as given below:

MailMessage msg = MailMessage.Load("Output.msg", MailMessageLoadOptions.DefaultMsg);
IEWSClient client = GetAsposeEWSClient();
client.Send(msg);

Once the message is sent you can retrieve this message from the sent folder as given below:

IEWSClient client = GetAsposeEWSClient();
ExchangeFolderInfo info = client.GetFolderInfo("Sent Items");
ExchangeMessageInfoCollection mic = client.ListMessages(info.Uri);
foreach(ExchangeMessageInfo emi in mic)
{
MailMessage msg = client.FetchMessage(emi.UniqueUri);
if(msg.Headers["MyId"] == "saved guid string here")
{
Console.WriteLine(msg.Headers["MyId"].ToString() + " is the target message");
}
}

This is how you can keep track of sent items however, there is no method to build query based upon the specific header value. If you want to make it more efficient, you may please add this Guid in the subject as well if possible. It will help to find the specific message only.

Please feel free to write us back if you have any query in this regard.

The issues you have found earlier (filed as EMAILNET-34675) have been fixed in this update.


This message was posted using Notification2Forum from Downloads module by Aspose Notifier.

This code I utilize with Exchange 2010, the header is there in the message but after fetching the message the custom header is gone. Need help fixing this, need to find that custom header somewhere in the fetched MailMessage


Cordially,

Luis

Hi Luis,

All the custom headers are retained till the message is residing oon the disc. As soon as it is sent, Exchange server re-writes all the headers and custom headers are removed. That is why these cannot be fetched on the receving end.

You may use some attachment or guid in the subject/body as these are not truncated by the Exchange. Please feel free to write us back if you have any other query in this regard.