Skip to main content

WebService to release Quarantined messages in Exchange 2007

This post follows on from the quarantine digest message post last month and looks at the challeges around resubmitting a Quarantined message. Outlook 2007 and OWA 2007 have the ability to resend a message from the Quarantine mailbox using the Resend facilities that are documented in http://msdn.microsoft.com/en-us/library/ms527630(EXCHG.10).aspx. Unfortunately you can't do this using Exchange Web Services because EWS doesn't give you full access to to the recipient collection so this means its not possible to set the PR_RECIPIENT_TYPE for each recipient you want the message delivered to and to also mark those message recipients you dont want the message delivered to. One interesting piece of functionality the quarantine mailbox has is the ability to sendas any email address be it Internal or External eg all you need to do is set the following Mapi property on the message your sending and this will become the sender of the message when you send it. Note this will only work for the quarantine mailbox

ExtendedPropertyType osOrginalSender = new ExtendedPropertyType();
PathToExtendedFieldType epExPath = new PathToExtendedFieldType();
epExPath.DistinguishedPropertySetIdSpecified = true;
epExPath.DistinguishedPropertySetId = DistinguishedPropertySetType.PublicStrings;
epExPath.PropertyName = "quarantine-original-sender";
epExPath.PropertyType = MapiPropertyTypeType.String;
osOrginalSender.ExtendedFieldURI = epExPath;
osOrginalSender.Item = "";
Message.ExtendedProperty = new ExtendedPropertyType[1];
Message.ExtendedProperty[0] = osOrginalSender;

Back on track if you want to release a quarantined message you have the option of using Mapi or using the Replay directory on a Hub Role Server. There is a good description of the Replay directory here what makes the replay directory usefull for the task of resubmitting a quarantined message is that it allows specifying X-Sender and X-Reciever to allow you to specify who this message is delievered to ignoring the existing To and From Mail headers. With Quarantined messages the actual message that is quarantined is an attachment of the NDR message that is delivered to the quarantined mailbox. To to be able to submit the message to the replay directory I've come up with a WebService that uses EWS and takes one parameter which is the ItemID of the quarantine report message. It then Grabs this message using EWS looks at the attachments on the message and grabs the original message and then writes X-headers to represent the Sender and Recipients the message is to be delivered to. The messsage is then saved to the replay directory on the Hub Server.

This is still pretty rough and needs more work but this is the skeleton I've put a download here the code looks like.

using System;
using System.Collections.Generic;
using System.Web;
using System.Net;
using System.Web.Services;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
using WebService1.ews;
using System.IO;

namespace WebService1
{
///
/// Summary description for Service1
///

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{

[WebMethod]
public string ResubmitMessage(String MessageID)
{
ServicePointManager.ServerCertificateValidationCallback =
delegate(Object obj, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{
// Replace this line with code to validate server certificate.
return true;
};
string res = "";
ExchangeServiceBinding esb = new ExchangeServiceBinding();
esb.RequestServerVersionValue = new RequestServerVersion();
esb.RequestServerVersionValue.Version = ExchangeVersionType.Exchange2007_SP1;
esb.Credentials = new NetworkCredential("user", "password", "domain");
esb.Url = @"https://casserver/EWS/Exchange.asmx";
MessageType[] oMessages = GetQuantined(esb, MessageID);
if (oMessages[0] != null & oMessages[1] != null)
{
byte[] MimeContent = Convert.FromBase64String(oMessages[1].MimeContent.Value);
System.Text.ASCIIEncoding asEncoding = new System.Text.ASCIIEncoding();
String xsXSender = "X-Sender: <" + oMessages[0].ExtendedProperty[0].Item.ToString() + ">";
String xrXreciever = "";
foreach (EmailAddressType recp in oMessages[0].ToRecipients)
{
xrXreciever = xrXreciever + "X-Receiver: <" + recp.EmailAddress + ">" + "\r\n";
}
String emlMessage = xsXSender + "\r\n" + xrXreciever + asEncoding.GetString(MimeContent);
String MessageGuid = Guid.NewGuid().ToString();
MessageGuid = MessageGuid + DateTime.Now.Ticks.ToString();
StreamWriter emlwriter = new StreamWriter((@"C:\Program Files\Microsoft\Exchange Server\TransportRoles\replay\" + MessageGuid + "eml"), true);
emlwriter.WriteLine(emlMessage);
emlwriter.Close();
res = "Email Written to Replay Directory";

}
else {
res = "Error geting message";
}
return res;
}
private MessageType[] GetQuantined(ExchangeServiceBinding esb,String MessageID) {
MessageType[] msMessages = new MessageType[2];
ItemIdType iiItemId = new ItemIdType();
iiItemId.Id = MessageID;
ItemIdType[] giGetItemArray = new ItemIdType[1];
giGetItemArray[0] = iiItemId;
BasePathToElementType[] beAdditionproperteis = new BasePathToElementType[1];
PathToExtendedFieldType osOriginalSenderEmail = new PathToExtendedFieldType();
osOriginalSenderEmail.PropertyTag = "0x0067";
osOriginalSenderEmail.PropertyType = MapiPropertyTypeType.String;
beAdditionproperteis[0] = osOriginalSenderEmail;
ItemResponseShapeType isItemResponseShape = new ItemResponseShapeType();
isItemResponseShape.BaseShape = DefaultShapeNamesType.AllProperties;
GetItemType giGetItem = new GetItemType();
isItemResponseShape.AdditionalProperties = beAdditionproperteis;
giGetItem.ItemShape = isItemResponseShape;
giGetItem.ItemIds = giGetItemArray;
GetItemResponseType giGetItemResponse = esb.GetItem(giGetItem);
if (giGetItemResponse.ResponseMessages.Items[0].ResponseClass == ResponseClassType.Success)
{
ItemType miNDRItem = ((ItemInfoResponseMessageType)giGetItemResponse.ResponseMessages.Items[0]).Items.Items[0];
msMessages[0] = (MessageType)miNDRItem;
foreach (AttachmentType atAttachment in miNDRItem.Attachments)
{
if (atAttachment.ContentType == "message/rfc822")
{
msMessages[1] = GetOrigMsg(esb, atAttachment.AttachmentId);
}

}

}
else {
// Handle Error
}
return msMessages;
}
private MessageType GetOrigMsg(ExchangeServiceBinding esb, AttachmentIdType atAttachID)
{
MessageType omOrigMessage = null;
GetAttachmentType gaGetAttachment = new GetAttachmentType();
AttachmentIdType[] atArray = new AttachmentIdType[1];
atArray[0] = atAttachID;
gaGetAttachment.AttachmentShape = new AttachmentResponseShapeType();
gaGetAttachment.AttachmentShape.IncludeMimeContent = true;
gaGetAttachment.AttachmentShape.IncludeMimeContentSpecified = true;
gaGetAttachment.AttachmentIds = atArray;
GetAttachmentResponseType gaGetAttachmentResponse = esb.GetAttachment(gaGetAttachment);
foreach (AttachmentInfoResponseMessageType atAttachmentResponse in gaGetAttachmentResponse.ResponseMessages.Items)
{
if (atAttachmentResponse.ResponseClass == ResponseClassType.Success)
{
ItemAttachmentType itIemAttachmentType = atAttachmentResponse.Attachments[0] as ItemAttachmentType;
omOrigMessage = (MessageType)itIemAttachmentType.Item;
}
else
{
//Deal with error
}
}
return omOrigMessage;
}
}
}

Popular posts from this blog

The MailboxConcurrency limit and using Batching in the Microsoft Graph API

If your getting an error such as Application is over its MailboxConcurrency limit while using the Microsoft Graph API this post may help you understand why. Background   The Mailbox  concurrency limit when your using the Graph API is 4 as per https://docs.microsoft.com/en-us/graph/throttling#outlook-service-limits . This is evaluated for each app ID and mailbox combination so this means you can have different apps running under the same credentials and the poor behavior of one won't cause the other to be throttled. If you compared that to EWS you could have up to 27 concurrent connections but they are shared across all apps on a first come first served basis. Batching Batching in the Graph API is a way of combining multiple requests into a single HTTP request. Batching in the Exchange Mail API's EWS and MAPI has been around for a long time and its common, for email Apps to process large numbers of smaller items for a variety of reasons.  Batching in the Gr...

Exporting and Uploading Mailbox Items using Exchange Web Services using the new ExportItems and UploadItems operations in Exchange 2010 SP1

Two new EWS Operations ExportItems and UploadItems where introduced in Exchange 2010 SP1 that allowed you to do a number of useful things that where previously not possible using Exchange Web Services. Any object that Exchange stores is basically a collection of properties for example a message object is a collection of Message properties, Recipient properties and Attachment properties with a few meta properties that describe the underlying storage thrown in. Normally when using EWS you can access these properties in a number of a ways eg one example is using the strongly type objects such as emailmessage that presents the underlying properties in an intuitive way that's easy to use. Another way is using Extended Properties to access the underlying properties directly. However previously in EWS there was no method to access every property of a message hence there is no way to export or import an item and maintain full fidelity of every property on that item (you could export the...

Sending a Message in Exchange Online via REST from an Arduino MKR1000

This is part 2 of my MKR1000 article, in this previous post  I looked at sending a Message via EWS using Basic Authentication.  In this Post I'll look at using the new Outlook REST API  which requires using OAuth authentication to get an Access Token. The prerequisites for this sketch are the same as in the other post with the addition of the ArduinoJson library  https://github.com/bblanchon/ArduinoJson  which is used to parse the Authentication Results to extract the Access Token. Also the SSL certificates for the login.windows.net  and outlook.office365.com need to be uploaded to the devices using the wifi101 Firmware updater. To use Token Authentication you need to register an Application in Azure https://msdn.microsoft.com/en-us/office/office365/howto/add-common-consent-manually  with the Mail.Send permission. The application should be a Native Client app that use the Out of Band Callback urn:ietf:wg:oauth:2.0:oob. You ...
All sample scripts and source code is provided by for illustrative purposes only. All examples are untested in different environments and therefore, I cannot guarantee or imply reliability, serviceability, or function of these programs.

All code contained herein is provided to you "AS IS" without any warranties of any kind. The implied warranties of non-infringement, merchantability and fitness for a particular purpose are expressly disclaimed.