Pergunta

I've managed to connect to my exchange server via EWS and send an email. I need an example of how to create a calendar item.

Coldfusion 9 Exchange 2007

<cfobject type="Java" class="microsoft.exchange.webservices.data.ExchangeService" name="service">
<cfobject type="Java" class="microsoft.exchange.webservices.data.ExchangeVersion" name="version">
<cfset service.init(version.Exchange2007_SP1)>
<cfobject type="Java" class="microsoft.exchange.webservices.data.WebCredentials" name="credentials">
<cfset credentials.init("z@x.com","password")>
<cfset service.setCredentials(credentials) />
<cfobject type="Java" class="java.net.URI" name="uri">
<cfset uri.init("server URL")>
<cfset service.setUrl(uri) />
<cfobject type="Java" action="create" class="microsoft.exchange.webservices.data.EmailMessage" name="message">
<cfset message = message.init(service) />
<cfset message.SetSubject("EWSTest")>
<cfset messageBody = CreateObject("java", "microsoft.exchange.webservices.data.MessageBody")>
<cfset messageBody.init("My EWS test message again")>
<cfset message.SetBody( messageBody )>
<cfset message.ToRecipients.Add("email@gmail.com") >
<cfset message.SendAndSaveCopy() >

I tried the following code but only got an error that only says "Subject";

<cfobject type="Java" class="microsoft.exchange.webservices.data.ExchangeService" name="appointment">
<cfset appointment.Subject = "Dentist Appointment">
<cfset appointment.Body = "The appointment is with Dr. Smith.">
<cfset appointment.Start = createDateTime(Year('2014/03/22'), Month('2014/03/22'), 
 Day('2014/03/22'), Hour('09:00'), Minute('09:00'), 0)>
<cfset appointment.End = createDateTime(Year('2014/03/22'), Month('2014/03/22'), 
 Day('2014/03/22'), Hour('09:00'), Minute('09:00'), 0)>
<cfset appointment.Save(SendInvitationsMode.SendToNone)>

(edit) - current code:

    <cfset StartDateTime = createDateTime(Year('2014/03/22'), Month('2014/03/22'), Day('2014/03/22'), Hour('09:00'), Minute('09:00'), 0)>
<cfset EndDateTime = createDateTime(Year('2014/03/22'), Month('2014/03/22'), Day('2014/03/22'), Hour('09:30'), Minute('09:30'), 0)>
<cfscript>
    exchangeService = createObject("java", "microsoft.exchange.webservices.data.ExchangeService");
    exchangeVersion = createObject("java", "microsoft.exchange.webservices.data.ExchangeVersion");
    service = exchangeService.init(exchangeVersion.Exchange2007_SP1);
    webCredentials = createObject("java", "microsoft.exchange.webservices.data.WebCredentials");
    service.setCredentials(webCredentials.init("z@x.com","password"));
    serviceURI = createObject("java", "java.net.URI").init("https://owa016.msoutlookonline.net/EWS/Exchange.asmx?wsdl"));
    service.setUrl(serviceURI);
    appointment = createObject("java", "microsoft.exchange.webservices.data.Appointment").init(service);
    appointment.Start = "#StartDateTime#";
appointment.End = "#EndDateTime#";
    appointment.Subject = "Dentist Appointment";
    appointment.Body = "The appointment is with Dr. Smith.";
    appointment.Save(SendInvitationsMode.SendToNone);
</cfscript> 

(edit 2) - partial solution;

<cfset StartDateTime = createDateTime(Year('2014/03/22'), Month('2014/03/22'), Day('2014/03/22'), Hour('09:00'), Minute('09:00'), 0)>
<cfset EndDateTime = createDateTime(Year('2014/03/22'), Month('2014/03/22'), Day('2014/03/22'), Hour('09:30'), Minute('09:30'), 0)>
<!--- <cfoutput>Start=#StartDateTime#<br>End=#EndDateTime#</cfoutput><cfabort> --->
<cfscript>
    exchangeService = createObject("java", "microsoft.exchange.webservices.data.ExchangeService");
    exchangeVersion = createObject("java", "microsoft.exchange.webservices.data.ExchangeVersion");
    service = exchangeService.init(exchangeVersion.Exchange2007_SP1);
    webCredentials = createObject("java", "microsoft.exchange.webservices.data.WebCredentials");
    service.setCredentials(webCredentials.init("z@x.com","password"));
    serviceURI = createObject("java", "java.net.URI").init("https://owa016.msoutlookonline.net/EWS/Exchange.asmx?wsdl");
    service.setUrl(serviceURI);
    appointment = createObject("java", "microsoft.exchange.webservices.data.Appointment").init(service);
    appointment.setStartTimeZone(microsoft.exchange.webservices.data.TimeZoneDefinition)
    appointment.setStart("#StartDateTime#");
    appointment.setEnd("#EndDateTime#");
    appointment.setSubject("Dentist Appointment");
    MessageBody = createObject("java", "microsoft.exchange.webservices.data.MessageBody");
    appointment.setBody( MessageBody.init("The appointment is with Dr. Smith.") );
    SendInvitationsMode = createObject("java", "microsoft.exchange.webservices.data.SendInvitationsMode");
    appointment.Save(SendInvitationsMode.SendToNone);
</cfscript> 

This is the error I got; StartTimeZone required when setting the Start, End, IsAllDayEvent, or Recurrence properties. You must load or assign this property before attempting to update the appointment.

That sounds like Exchange 2010 and I'm on Exchange 2007.

Foi útil?

Solução

(Too long for comments ...)

Though not a complete answer, a few observations about the code:

  • When working with java objects, the true error is often contained in the stack trace, rather than the error header. Always check the stack trace too. In this case it reports

    java.lang.NoSuchFieldException: SUBJECT at coldfusion.runtime.StructBean.bindName(StructBean.java:243)


  • The most likely reason for that error is that you are using the wrong type of object. The properties you are attempting to set do not exist in the ExchangeService class. I believe you need to create an instance of an Appointment object instead, which does have the subject, startdate, etcetera properties. Similar to what is shown in this C# example.

    (Side note, personally I prefer cfscript for java code as the syntax is very similar. Using your original example as a base, something along these lines:)

    exchangeService = createObject("java", "microsoft.exchange.webservices.data.ExchangeService");
    exchangeVersion = createObject("java", "microsoft.exchange.webservices.data.ExchangeVersion");
    service = exchangeService.init(exchangeVersion.Exchange2007_SP1);
    
    webCredentials = createObject("java", "microsoft.exchange.webservices.data.WebCredentials");
    service.setCredentials(webCredentials.init("z@x.com","password") );
    
    serviceURI = createObject("java", "java.net.URI").init("server URL");
    service.setUrl(serviceURI);
    
    appointment = createObject("java", "microsoft.exchange.webservices.data.Appointment").init(service);
    // ... set subject, date, etcetera properties 
    



  • If you are using C# examples as a base, keep in mind the syntax does not always translate exactly. In theory, CF supports direct assignment of properties using the syntax below, but only if "[the] class conforms to the JavaBeans pattern.":

    <cfset appointment.Subject = "Dentist Appointment">

    That may not be case with the EWS classes. You may need to invoke the methods explicitly:

    <cfset appointment.setSubject("Dentist Appointment")>

  • The "Body" property is not a simple string. You need to create an instance of microsoft.exchange.webservices.data.MessageBody instead.

  • SendInvitationsMode is also a class. You need to create an instance of it before you can use its properties here:

    appointment.Save(SendInvitationsMode.SendToNone);

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top