For some time now developers have been able to access Exchange Server 2007 via Exchange Web Services(EWS). To get this working you basically added a webreference in Visual Studio and constructed a request pending upon the type of function you wanted to execute. EWS was a great step away from the various message api(CDO, MAPI, WebDaV) which had their own strength and weakness. But still EWS can be laborious and you often end up building your own stub upon the EWS webreference. Through my teaching of Developing for Microsoft Unified Communications R2 i got to know a new API called Exchange Web Services Managed API. In short this is the way – this is the api for programming for Exchange Server 2007 with sp1. In the next few blogs I will run through some of the basics for this new API. As a first sending a email would be a place to start.
Sending email with EWS(c#)
1: // Setup the request
2: MessageType emailMessage = new MessageType();
3:
4: // Set the To email address
5: emailMessage.ToRecipients = new EmailAddressType[1];
6: emailMessage.ToRecipients[0] = new EmailAddressType();
7: emailMessage.ToRecipients[0].EmailAddress = "email@receiver.somewhere";
8:
9: // Set the From email address
10: emailMessage.From = new SingleRecipientType();
11: emailMessage.From.Item = new EmailAddressType();
12: emailMessage.From.Item.EmailAddress = "email@sender.somewhere";
13:
14: // Set the Subject
15: emailMessage.Subject = this.textBoxSubject.Text;
16:
17: // Set the Body
18: emailMessage.Body = new BodyType();
19: emailMessage.Body.BodyType1 = BodyTypeType.Text;
20: emailMessage.Body.Value = "Something to write about";
21:
22:
23: // Create request object and set properties
24:
25: // Create the CreateItemType in order to create and send the email
26: CreateItemType request = new CreateItemType();
27: request.Items = new NonEmptyArrayOfAllItemsType();
28: request.Items.Items = new ItemType[1];
29: request.Items.Items[0] = emailMessage;
30: request.MessageDisposition = MessageDispositionType.SendAndSaveCopy;
31: request.MessageDispositionSpecified = true;
32:
33:
34: // Call CreateItem and process response
35:
36: // Finally, call the CreateItem method to create and send the email
37: CreateItemResponseType response = _service.CreateItem(request);
38:
39: // Analyze the response message for success/failure
40: ItemInfoResponseMessageType responseMessage = response.ResponseMessages.Items[0] as ItemInfoResponseMessageType;
41:
42: if (responseMessage.ResponseClass == ResponseClassType.Success)
43: success = true;
44:
45: MessageBox.Show("Email send succeeded: " + success);
Sending email with EWS Managed API(c#)
1: //Creating service binding for a Exchange Server 2007
2: ExchangeService es = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
3:
4: //Assigning credentials for accessing the sender account
5: es.Credentials = new System.Net.NetworkCredential("sender@email.somewhere", "SuperSecret#1");
6:
7: //using autodiscover to find righ Exchange From End
8: es.AutodiscoverUrl("pli@inceptio.dk");
9:
10: //Creating a email message
11: EmailMessage em = new EmailMessage(es);
12: em.Subject = "Hello World!";
13: em.Body = "Using EWS Managed API";
14: em.ToRecipients.Add("receiver@emai.somewhere");
15: em.SendAndSaveCopy();
1 comment:
Thanks a lot!! I'm new to mail server technology. This helped me in learning more about the same.
Post a Comment