Exchange 2007 EWS: Sending Email

Sending Email using Exchange 2007 Web Services (EWS) is a little trickier than I first anticipated (read: counterintuitive), since it uses the service.CreateItem() method call instead of the service.SendItem().  The basic idea is to formulate the email message as a CreateItemType with a MessageType assigned to the Item property, with the MessageDisposition set to "MessageDispositionType.SendAndSaveCopy."  The following code shows an example of a library method that can send a basic email message with no attachments from a sender to a single recipient (it should be fairly easy to extrapolate multiple recipients, cc's & bcc's from there).

The first step with any EWS call is to set up your Exchange Service Binding:

ExchangeServiceBinding esb = new ExchangeServiceBinding();
esb.Credentials = new NetworkCredential(config.UserName, config.Password, config.Domain);
 
esb.Url = config.ServerURL;

Next, create the root MessageType that will serve as your email message:

MessageType emailMessage = new MessageType();

Now you can add the from/sender as a SingleRecipientType (there can't be multiple senders), and add the ToRecipients as an array of EmailAddressTypes.  I'll also show the body and subject code since it is relatively straightforward.

//Add the sender/recipient to the email message
emailMessage.ToRecipients = new EmailAddressType[1];
emailMessage.ToRecipients[0] = new EmailAddressType();
emailMessage.ToRecipients[0].EmailAddress = "recipient@email.com"; //Currently there is only one recipient
 
emailMessage.From = new SingleRecipientType();  //set up a single sender
emailMessage.From.Item = new EmailAddressType();
emailMessage.From.Item.EmailAddress = "sender@email.com";
 
emailMessage.Subject = "Subject Goes Here";
 
emailMessage.Body = new BodyType();
emailMessage.Body.BodyType1 = BodyTypeType.Text; //specify HTML or plain Text
emailMessage.Body.Value = "Body Goes Here";

The next step is to place the email message inside of a CreateItemType object.

CreateItemType emailToSave = new CreateItemType();
emailToSave.Items = new NonEmptyArrayOfAllItemsType();
emailToSave.Items.Items = new ItemType[1];
emailToSave.Items.Items[0] = emailMessage;
emailToSave.MessageDisposition = MessageDispositionType.SendAndSaveCopy;
emailToSave.MessageDispositionSpecified = true;

Finally, you call the CreateItem() method to create the item and send it off.  I'll usually place this part in a try/catch block, and then return the ResponseCode (inside of the returned ResponseMessageType) so that it can be inspected for success.

CreateItemResponseType response = esb.CreateItem(emailToSave);
 
// Get the response messages.
ResponseMessageType[] rmta =  response.ResponseMessages.Items;

Regardless of the complexities, it sure beats WebDAV!

12 Comments

  • I stumbled upon your article on google, and just wanted to let you know that if you first create the item as a SaveOnly to drafts, you can then send it using SendItem and move it to the Sent Items folder. It simplifies the process and also saves it in case it doesn't send.

  • Does CreateItem create EML file on exchange server? My reqt is to fetch the link/location to the file on the exchange server that points to email I create/sent using CreateItem.

  • Any idea how one might go about sending both an HTML and a plain Text body? In my case, I don't know if the recipient will be able to see HTML, so I really need to send both formats. RFC822 allows me to send both formats in a multipart email, but the EWS XML doesn't seem to allow for multiple BodyTypes.

  • How would I go about adding attachments to this email? Thanks for a great tutorial!

  • I have an issue in sending external emails but no problem using the asp code to send internal email.

    anyone encounter a similar issue?

    thanks

  • When I sent message from "sender@email.com" message saves in sent folder of configUser MailBox, but not "sender@email.com" mailbox.
    What should I do with that?

  • When i connect to exchange server, in the response it is giving Bad Gate Way Exception.How to handle this?

  • I am tring to sen email from contact@domain.com but I want name to appear like "Jhon Smith". What am i supposed to add above code?
    any idea please??

  • how can we fetch mail from an exchange server using EWS

  • To get answers for most questions about how to set the user display name, etc., download an evaluation copy of JEC. The class EWSJExamples has code for most common operations and it can be imported as-is into Eclipse.

  • You can also find a simple solution here

  • how do I address to multiple recipients??

Comments have been disabled for this content.