EWS CreateItemResponse for any Email Address
Is it possible in Exchange Web Services 2010 to create a message in a mailbox from any email address?
So the scenario is; User receives email, Service scoops email and stores in SQL (stores the message as XML) Email then deleted from mailbox Time passes... Service takes SQL version of email and creates back into the original mailbox
We previously ran into an issue of exchange security which didn't allow us to create the email using the sender object of anyone outside our domain. For example Mike@MyDomain.com worked fine but Bob@Microsoft.com didn't work.
I can obviously see security risks associated with lifting this restriction but it's a requirement for a system I need to build.
Actual code for creating the message is here (most of the code was taken from MSDN);
public void CreateEmail(string FromAddress, MessageType message)
{
ExchangeServiceBinding esb = getExchangeServiceBinding(FromAddress);
// Create the CreateItem request.
CreateItemType createItemRequest = new CreateItemType();
// Specifiy how the created items are handled
createItemRequest.MessageDisposition = MessageDispositionType.SendAndSaveCopy;
createItemRequest.MessageDispositionSpecified = true;
// Specify the location of sent items.
createItemRequest.SavedItemFolderId = new TargetFolderIdType();
DistinguishedFolderIdType sentitems = new DistinguishedFolderIdType();
sentitems.Id = DistinguishedFolderIdNameType.sentitems;
createItemRequest.SavedItemFolderId.Item = sentitems;
// Create the array of items.
createItemRequest.Items = new NonEmptyArrayOfAllItemsType();
// Create a single e-mail message.
MessageType newmessage = new MessageType();
开发者_开发问答 //Can't seem to restore the original message.
//If part of the email is being lost when restored then it's happening here.
//import original message into newmessage
newmessage.Subject = message.Subject;
newmessage.Body = message.Body;
newmessage.ItemClass = message.ItemClass;
newmessage.Sender = message.Sender;
newmessage.ToRecipients = message.ToRecipients;
newmessage.Sensitivity = message.Sensitivity;
newmessage.ExtendedProperty = message.ExtendedProperty;
newmessage.DateTimeSent = message.DateTimeSent;
newmessage.IsRead = true;
// Add the message to the array of items to be created.
createItemRequest.Items.Items = new ItemType[1];
createItemRequest.Items.Items[0] = newmessage;
try
{
// Send the request to create and send the e-mail item, and get the response.
CreateItemResponseType createItemResponse = esb.CreateItem(createItemRequest);
// Determine whether the request was a success.
if (createItemResponse.ResponseMessages.Items[0].ResponseClass == ResponseClassType.Error)
{
throw new Exception(createItemResponse.ResponseMessages.Items[0].MessageText);
}
else
{
Console.WriteLine("Item was created");
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
I have now sorted this issue. There was no security restriction. Using Exchange WebServices this code worked fine. The restriction was with using the Managed web services API. There is no way to alter the Sender/From object when creating a message.
精彩评论