-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmail.java
More file actions
932 lines (784 loc) · 35.9 KB
/
Email.java
File metadata and controls
932 lines (784 loc) · 35.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
package javaxt.exchange;
//******************************************************************************
//** Email Message
//******************************************************************************
/**
* Used to represent a mail message found in a Mail Folder (e.g. "Inbox").
* http://msdn.microsoft.com/en-us/library/aa494306%28v=exchg.140%29.aspx
*
******************************************************************************/
public class Email extends FolderItem {
private Mailbox from;
private java.util.HashSet<Mailbox> ToRecipients = new java.util.HashSet<Mailbox>();
private java.util.HashSet<Mailbox> CcRecipients = new java.util.HashSet<Mailbox>();
private java.util.HashSet<Mailbox> BccRecipients = new java.util.HashSet<Mailbox>();
private String importance = "Normal";
private String sensitivity = "Normal";
private Integer size;
private boolean isRead = false;
private javaxt.utils.Date sent;
private javaxt.utils.Date received;
private String response;
private javaxt.utils.Date responseDate;
//The following parameters are for internal use only!
private String referenceId;
private String messageType = "Message";
private java.util.HashMap<String, String> headers = new java.util.HashMap<String, String>();
//**************************************************************************
//** Constructor
//**************************************************************************
/** Used to create a new email message. The message will be saved in the
* "Drafts" folder.
*/
public Email(){}
//**************************************************************************
//** Constructor
//**************************************************************************
/** Creates a new instance of this class using an existing Email,
* effectively creating a clone.
*/
public Email(javaxt.exchange.Email message){
this.init(message);
}
private void init(javaxt.exchange.Email message){
//General information
this.id = message.id;
this.parentFolderID = message.parentFolderID;
this.subject = message.subject;
this.body = message.body;
this.bodyType = message.bodyType;
this.categories = message.categories;
this.hasAttachments = message.hasAttachments;
this.attachments = message.attachments;
this.updates = message.updates;
this.lastModified = message.lastModified;
this.additionalProperties = message.additionalProperties;
this.extendedProperties = message.extendedProperties;
//Email specific information
this.from = message.from;
this.ToRecipients = message.ToRecipients;
this.CcRecipients = message.CcRecipients;
this.BccRecipients = message.BccRecipients;
this.importance = message.importance;
this.sensitivity = message.sensitivity;
this.size = message.size;
this.isRead = message.isRead;
this.sent = message.sent;
this.received = message.received;
this.response = message.response;
this.responseDate = message.responseDate;
this.referenceId = message.referenceId;
this.messageType = message.messageType;
}
//**************************************************************************
//** Constructor
//**************************************************************************
/** Creates a new instance of Contact using a node from a
* FindItemResponseMessage.
*/
protected Email(org.w3c.dom.Node messageNode) {
super(messageNode);
parseMessage();
}
//**************************************************************************
//** Constructor
//**************************************************************************
/** Creates a new instance of this class
*/
public Email(String exchangeID, Connection conn, ExtendedFieldURI[] additionalProperties) throws ExchangeException{
super(exchangeID, conn, additionalProperties);
parseMessage();
}
//**************************************************************************
//** Constructor
//**************************************************************************
/** Creates a new instance of this class
*/
public Email(String exchangeID, Connection conn) throws ExchangeException{
this(exchangeID, conn, null);
}
//**************************************************************************
//** parseMessage
//**************************************************************************
/** Used to parse an xml node with email information.
*/
private void parseMessage(){
boolean isDraft = false;
org.w3c.dom.NodeList outerNodes = this.getChildNodes();
for (int i=0; i<outerNodes.getLength(); i++){
org.w3c.dom.Node outerNode = outerNodes.item(i);
if (outerNode.getNodeType()==1){
String nodeName = outerNode.getNodeName();
if (nodeName.contains(":")) nodeName = nodeName.substring(nodeName.indexOf(":")+1);
if (nodeName.equalsIgnoreCase("Sensitivity")){
sensitivity = javaxt.xml.DOM.getNodeValue(outerNode);
}
else if(nodeName.equalsIgnoreCase("Importance")){
importance = javaxt.xml.DOM.getNodeValue(outerNode);
}
else if(nodeName.equalsIgnoreCase("Size")){
try{
size = Integer.parseInt(javaxt.xml.DOM.getNodeValue(outerNode));
}
catch(Exception e){
}
}
else if(nodeName.equalsIgnoreCase("From")){
org.w3c.dom.Node[] nodes = javaxt.xml.DOM.getElementsByTagName("Mailbox", outerNode);
if (nodes.length>0){
try{
from = new Mailbox(nodes[0]);
}
catch(ExchangeException e){
}
}
}
else if(nodeName.equalsIgnoreCase("ToRecipients")){
for (org.w3c.dom.Node node : javaxt.xml.DOM.getElementsByTagName("Mailbox", outerNode)){
try{
ToRecipients.add(new Mailbox(node));
}
catch(ExchangeException e){
}
}
}
else if(nodeName.equalsIgnoreCase("CcRecipients")){
for (org.w3c.dom.Node node : javaxt.xml.DOM.getElementsByTagName("Mailbox", outerNode)){
try{
CcRecipients.add(new Mailbox(node));
}
catch(ExchangeException e){
}
}
}
else if(nodeName.equalsIgnoreCase("BccRecipients")){
for (org.w3c.dom.Node node : javaxt.xml.DOM.getElementsByTagName("Mailbox", outerNode)){
try{
BccRecipients.add(new Mailbox(node));
}
catch(ExchangeException e){
}
}
}
else if(nodeName.equalsIgnoreCase("IsRead")){
isRead = javaxt.xml.DOM.getNodeValue(outerNode).equalsIgnoreCase("true");
}
else if(nodeName.equalsIgnoreCase("IsDraft")){
isDraft = javaxt.xml.DOM.getNodeValue(outerNode).equalsIgnoreCase("true");
}
else if(nodeName.equalsIgnoreCase("DateTimeSent")){
try{
sent = new javaxt.utils.Date(javaxt.xml.DOM.getNodeValue(outerNode));
}
catch(java.text.ParseException e){}
}
else if(nodeName.equalsIgnoreCase("DateTimeReceived")){
try{
received = new javaxt.utils.Date(javaxt.xml.DOM.getNodeValue(outerNode));
}
catch(java.text.ParseException e){}
}
}
}
//Update the sent and received timestamps as needed
//if (isDraft) sent = received = null;
//Find the PR_LAST_VERB_EXECUTED (0x10810003) extended MAPI property
java.util.Iterator<ExtendedFieldURI> it = extendedProperties.keySet().iterator();
while(it.hasNext()){
ExtendedFieldURI property = it.next();
if (property.getName().equalsIgnoreCase("0x1081")){
Integer value = extendedProperties.get(property).toInteger();
if (value==102) this.response = "Reply";
else if(value == 103) this.response = "ReplyAll";
else if(value == 104) this.response = "Forward";
extendedProperties.remove(property);
break;
}
}
//Find the PR_LAST_VERB_EXECUTION_TIME (0x10820040) extended MAPI property
it = extendedProperties.keySet().iterator();
while(it.hasNext()){
ExtendedFieldURI property = it.next();
if (property.getName().equalsIgnoreCase("0x1082")){
responseDate = extendedProperties.get(property).toDate();
extendedProperties.remove(property);
break;
}
}
//Find the PR_SENDER_EMAIL_ADDRESS (0x0c1f001e) extended MAPI property
it = extendedProperties.keySet().iterator();
while(it.hasNext()){
ExtendedFieldURI property = it.next();
if (property.getName().equalsIgnoreCase("0xC1F")){
String email = extendedProperties.get(property).toString();
if (from==null){ //Example: Sharing Request
EmailAddress _null = null;
from = new Mailbox(null, _null);
}
if (from.getEmailAddress()==null){
if (!email.contains("@")){
from.setDomainAddress(email); //<--For performance reasons, don't resolve the domain address!
}
else{
try{
from.setEmailAddress(email);
}
catch(ExchangeException e){}
}
}
extendedProperties.remove(property);
break;
}
}
//Find the PR_HASATTACH (0x0e1b000b) extended MAPI property
it = extendedProperties.keySet().iterator();
while(it.hasNext()){
ExtendedFieldURI property = it.next();
if (property.getName().equalsIgnoreCase("0xE1B")){
hasAttachments = extendedProperties.get(property).toBoolean();
extendedProperties.remove(property);
break;
}
}
}
//**************************************************************************
//** getSubject
//**************************************************************************
/** Returns the subject associated with this message.
*/
public String getSubject(){
return super.getSubject();
}
//**************************************************************************
//** setSubject
//**************************************************************************
/** Used to set/update the subject.
*/
public void setSubject(String subject){
super.setSubject(subject);
}
//**************************************************************************
//** getBody
//**************************************************************************
/** Used to get the content of the message.
*/
public String getBody(){
return super.getBody();
}
//**************************************************************************
//** setBody
//**************************************************************************
/** Used to set the content of the message.
* @param format Text format ("Best", "HTML", or "Text").
*/
public void setBody(String description, String format){
super.setBody(description, format);
}
//**************************************************************************
//** getBodyType
//**************************************************************************
/** Returns the text encoding used in the body of this item. Possible values
* include "Best", "HTML", or "Text".
*/
public String getBodyType(){
return super.getBodyType();
}
//**************************************************************************
//** getImportance
//**************************************************************************
/** Returns the importance assigned to this message. Possible values include
* "Low", "Normal", and "High".
*/
public String getImportance(){
return importance;
}
//**************************************************************************
//** setImportance
//**************************************************************************
/** Used to assign an importance to this message.
* @param importance Possible values include "Low", "Normal", and "High"
*/
public void setImportance(String importance){
if (importance!=null) importance = importance.trim();
if (importance==null || importance.length()==0) importance = "Normal";
if (importance.equalsIgnoreCase("High")) importance = "High";
else if (importance.equalsIgnoreCase("Low")) importance = "Low";
else importance = "Normal";
if (!this.importance.equals(importance)){
this.importance = importance;
updates.put("Importance", importance);
}
}
//**************************************************************************
//** getSensitivity
//**************************************************************************
/** Returns the sensitivity level associated with this message. Possible
* values include "Normal", "Personal", "Private", and "Confidential".
*/
public String getSensitivity(){
return sensitivity;
}
//**************************************************************************
//** setSensitivity
//**************************************************************************
/** Used to set the sensitivity level associated with this message.
* @param sensitivity Possible values include "Normal", "Personal",
* "Private", and "Confidential".
*/
public void setSensitivity(String sensitivity){
if (sensitivity!=null) sensitivity = sensitivity.trim();
if (sensitivity==null || sensitivity.length()==0) sensitivity = "Normal";
if (sensitivity.equalsIgnoreCase("Personal")) sensitivity = "Personal";
else if (sensitivity.equalsIgnoreCase("Private")) sensitivity = "Private";
else if (sensitivity.equalsIgnoreCase("Confidential")) sensitivity = "Confidential";
else sensitivity = "Normal";
if (!this.sensitivity.equals(sensitivity)){
this.sensitivity = sensitivity;
updates.put("Sensitivity", sensitivity);
}
}
public String getMessageClass(){
return itemClass;
}
public void setMessageClass(String itemClass){
this.itemClass = itemClass;
}
//**************************************************************************
//** getFrom
//**************************************************************************
/** Returns the Mailbox associated with the sender. Returns null if there
* is no sender associated with this message (e.g. draft message). <p/>
* Note that when a Mailbox is returned, the Mailbox may not always include
* an email address. This is especially true for emails originating from
* another Exchange account. In this case, you can try to retrieve the
* domain address associated with the Mailbox and resolve it to an email
* address via the Mailbox.getDomainAddress() and Mailbox.resolveName()
* methods.
*/
public Mailbox getFrom(){
return from;
}
//**************************************************************************
//** getDateTimeReceived
//**************************************************************************
/** Returns the date/time when the message was received. Returns a null if
* the message is a draft.
*/
public javaxt.utils.Date getDateTimeReceived(){
if (received==null) return null;
return received.clone();
}
//**************************************************************************
//** getDateTimeSent
//**************************************************************************
/** Returns the date/time when the message was sent. Returns a null if
* the message has not been sent (e.g. Draft message).
*/
public javaxt.utils.Date getDateTimeSent(){
if (sent==null) return null;
return sent.clone();
}
//**************************************************************************
//** getSize
//**************************************************************************
/** Returns the size of the message.
*/
public Integer getSize(){
return size;
}
//**************************************************************************
//** getResponse
//**************************************************************************
/** Returns the last action performed on this message. Possible values
* include "Forward", "Reply", "ReplyAll", or null.
*/
public String getResponse(){
return response;
}
//**************************************************************************
//** getResponseDate
//**************************************************************************
/** Returns the date/time associated with the last action performed on this
* message. This, in conjunction with the getResponse() method can be used
* to generate messages like "You replied on 12/13/2012 5:38 PM." or
* "You forwarded this message on 12/13/2012 6:01 PM."
*/
public javaxt.utils.Date getResponseDate(){
if (responseDate==null) return null;
return responseDate.clone();
}
//**************************************************************************
//** isRead
//**************************************************************************
/** Returns a boolean used to indicate whether the message has been read.
*/
public boolean isRead(){
return isRead;
}
//**************************************************************************
//** setIsRead
//**************************************************************************
/** Returns a boolean used to indicate whether the message has been read.
*/
public void setIsRead(boolean isRead){
if (id!=null) {
if (this.isRead!=isRead){
this.isRead = isRead;
updates.put("IsRead", isRead);
}
}
}
//**************************************************************************
//** addRecipient
//**************************************************************************
/** Used to add a recipient to this message.
* @param list Possible values include "To", "Cc", or "Bcc".
*/
public void addRecipient(String list, Mailbox recipient){
updateRecipients("add", recipient, list);
}
//**************************************************************************
//** removeRecipient
//**************************************************************************
/** Used to remove a recipient from this message.
* @param list Possible values include "To", "Cc", or "Bcc".
*/
public void removeRecipient(String list, Mailbox recipient){
updateRecipients("remove", recipient, list);
}
//**************************************************************************
//** updateRecipients
//**************************************************************************
/** Private method used to add/remove a recipient from this message.
*/
private void updateRecipients(String action, Mailbox recipient, String type){
String updateNode = "";
java.util.HashSet<Mailbox> recipients = null;
if (type.equalsIgnoreCase("To")){
recipients = ToRecipients;
updateNode = "ToRecipients";
}
else if(type.equalsIgnoreCase("Cc")){
recipients = CcRecipients;
updateNode = "CcRecipients";
}
else if(type.equalsIgnoreCase("Bcc")){
recipients = BccRecipients;
updateNode = "BccRecipients";
}
else return; //Throw Exception?
if (action.equals("add")){
if (recipients.contains(recipient)) return;
else recipients.add(recipient);
}
else if (action.equals("remove")){
if (!recipients.contains(recipient)) return;
else recipients.remove(recipient);
}
else{
return;
}
if (id!=null) {
StringBuffer xml = new StringBuffer();
for (Mailbox r : recipients){
xml.append(r.toXML("t"));
}
updates.put(updateNode, xml.toString());
}
}
//**************************************************************************
//** getToRecipients
//**************************************************************************
/** Returns an array of all the recipients on the "To" list.
*/
public Mailbox[] getToRecipients(){
if (ToRecipients.isEmpty()) return null;
else return ToRecipients.toArray(new Mailbox[ToRecipients.size()]);
}
//**************************************************************************
//** getCcRecipients
//**************************************************************************
/** Returns an array of all the recipients on the "CC" list.
*/
public Mailbox[] getCcRecipients(){
if (CcRecipients.isEmpty()) return null;
else return CcRecipients.toArray(new Mailbox[CcRecipients.size()]);
}
//**************************************************************************
//** getBccRecipients
//**************************************************************************
/** Returns an array of all the recipients on the "BCC" list.
*/
public Mailbox[] getBccRecipients(){
if (BccRecipients.isEmpty()) return null;
else return BccRecipients.toArray(new Mailbox[BccRecipients.size()]);
}
//**************************************************************************
//** isSharingRequest
//**************************************************************************
/** Returns true if the message is an invitation to share a calendar
* or contact folder.
*/
public boolean isSharingRequest(){
return itemClass.equalsIgnoreCase("IPM.Sharing");
}
//**************************************************************************
//** acceptSharingRequest
//**************************************************************************
/** Used to accept a sharing invitation, allowing another user access to
* view the calendar or contacts data.
*/
public void acceptSharingRequest(Connection conn) throws ExchangeException {
if (!isSharingRequest()) return;
/*NOTE: This method fails on my 2010 server. It complains that that the
request is invalid:
The request failed schema validation: The element 'Items' in namespace
'http://schemas.microsoft.com/exchange/services/2006/messages' has
invalid child element 'AcceptSharingInvitation' in namespace
'http://schemas.microsoft.com/exchange/services/2006/types'.
This message doesn't make sense! The documentation clearly indicates
that AcceptSharingInvitation is a child of Items:
http://msdn.microsoft.com/en-us/library/aa565652%28v=exchg.140%29.aspx
Also, check out the example from Microsoft for how to accept a sharing
request:
http://msdn.microsoft.com/en-us/library/exchange/ee693280%28v=exchg.140%29.aspx
*/
StringBuffer msg = new StringBuffer();
msg.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
msg.append("<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:t=\"http://schemas.microsoft.com/exchange/services/2006/types\" xmlns:m=\"http://schemas.microsoft.com/exchange/services/2006/messages\">");
msg.append("<soap:Body>");
msg.append("<m:CreateItem MessageDisposition=\"" + "SendOnly" + "\">");
msg.append("<m:Items>");
msg.append("<t:AcceptSharingInvitation>");
msg.append("<t:ReferenceItemId Id=\"" + this.getID() + "\" ChangeKey=\"" + this.getChangeKey(conn) + "\" />");
msg.append("</t:AcceptSharingInvitation>");
msg.append("</m:Items>");
msg.append("</m:CreateItem>");
msg.append("</soap:Body>");
msg.append("</soap:Envelope>");
conn.execute(msg.toString());
}
//**************************************************************************
//** delete
//**************************************************************************
/** Used to delete a message.
* @param MoveToDeletedItems If true, moves the item to the deleted items
* folder. If false, permanently deletes the message.
*/
public void delete(boolean MoveToDeletedItems, Connection conn) throws ExchangeException {
java.util.HashMap<String, String> options = new java.util.HashMap<String, String>();
if (MoveToDeletedItems) options.put("DeleteType", "MoveToDeletedItems");
else options.put("DeleteType", "HardDelete");
super.delete(options, conn);
}
//**************************************************************************
//** forward
//**************************************************************************
/** Creates a forwarded message and saves it to the drafts folder. The
* message won't be sent until the send() method is called.
*/
public Email forward(Connection conn) throws ExchangeException {
if (getID()==null) throw new ExchangeException("Can't forward message.");
Email email = new Email();
email.messageType = "ForwardItem";
email.referenceId = this.getID();
email.save(conn);
return new Email(email.getID(), conn);
}
//**************************************************************************
//** reply
//**************************************************************************
/** Creates a reply message and saves it to the drafts folder. The message
* won't be sent until the send() method is called.
*/
public Email reply(Connection conn) throws ExchangeException {
if (getID()==null) throw new ExchangeException("Can't reply to message.");
Email email = new Email();
email.messageType = "ReplyToItem";
email.referenceId = this.getID();
email.save(conn);
return new Email(email.getID(), conn);
}
//**************************************************************************
//** replyAll
//**************************************************************************
/** Creates a replyAll message and saves it to the drafts folder. The
* message won't be sent until the send() method is called.
*/
public Email replyAll(Connection conn) throws ExchangeException {
if (getID()==null) throw new ExchangeException("Can't reply to message.");
Email email = new Email();
email.messageType = "ReplyAllToItem";
email.referenceId = this.getID();
email.save(conn);
return new Email(email.getID(), conn);
}
//**************************************************************************
//** save
//**************************************************************************
/** Used to save this message. */
public void save(Connection conn) throws ExchangeException {
//Save the email message
if (this.getID()==null) this.create(conn);
else{
java.util.HashMap<String, String> options = new java.util.HashMap<String, String>();
options.put("ConflictResolution", "AutoResolve");
options.put("MessageDisposition", "SaveOnly");
super.update("Message", options, conn);
}
//Save attachments
Attachment[] attachments = this.getAttachments();
if (attachments!=null){
for (Attachment attachment : attachments){
if (attachment.getID()==null) attachment.save(conn);
}
}
//Reset all the attributes of this item to reflect what's in Exchange
init(new Email(id, conn, additionalProperties));
}
public void setHeaders(java.util.HashMap<String,String> headers){
this.headers = headers;
}
//**************************************************************************
//** send
//**************************************************************************
/** Used to send this message. A copy of this message will be saves in the
* "Sent" folder.
*/
public void send(Connection conn) throws ExchangeException {
//Make sure there's at least one recipient before sending the email
if (this.getToRecipients()==null && this.getCcRecipients()==null &&
this.getBccRecipients()==null){
throw new ExchangeException("At least one recipient is required.");
}
//Save the message
save(conn);
//Send the message and create a copy in the "sentitems" folder
StringBuffer msg = new StringBuffer();
msg.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
msg.append("<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:t=\"http://schemas.microsoft.com/exchange/services/2006/types\" xmlns:m=\"http://schemas.microsoft.com/exchange/services/2006/messages\">");
msg.append("<soap:Body>");
msg.append("<m:SendItem SaveItemToFolder=\"" + true + "\">");
msg.append("<m:ItemIds>");
msg.append("<t:ItemId Id=\"" + this.getID() + "\" ChangeKey=\"" + this.getChangeKey(conn) + "\" />"); //
msg.append("</m:ItemIds>");
msg.append("<m:SavedItemFolderId>");
msg.append("<t:DistinguishedFolderId Id=\"" + "sentitems" + "\" />");
msg.append("</m:SavedItemFolderId>");
msg.append("</m:SendItem>");
msg.append("</soap:Body>");
msg.append("</soap:Envelope>");
conn.execute(msg.toString(), headers);
}
//**************************************************************************
//** create
//**************************************************************************
/** Used to create a new email message.
*/
private void create(Connection conn) throws ExchangeException {
String action = "SaveOnly";
StringBuffer msg = new StringBuffer();
msg.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
msg.append("<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:t=\"http://schemas.microsoft.com/exchange/services/2006/types\" xmlns:m=\"http://schemas.microsoft.com/exchange/services/2006/messages\">");
msg.append("<soap:Body>");
msg.append("<m:CreateItem MessageDisposition=\"" + action + "\">");
msg.append("<m:SavedItemFolderId>");
if (this.getParentFolderID()!=null){
msg.append("<t:FolderId Id=\"" + this.getParentFolderID() + "\"/>");
}
else{
String folderName = "drafts";
if (action.equals("SendOnly") || action.equals("SendAndSaveCopy")) folderName = "sentitems";
msg.append("<t:DistinguishedFolderId Id=\"" + folderName + "\" />");
}
msg.append("</m:SavedItemFolderId>");
msg.append("<m:Items>");
/*
Here's is an ordered list of all email properties.
WARNING -- ORDER IS VERY IMPORTANT!!! If you mess up the order of the
properties, the operation will fail - at least it did on my Exchange
Server 2007 SP3 (8.3)
MimeContent, ItemId, ParentFolderId, ItemClass, Subject, Sensitivity,
Body, Attachments, DateTimeReceived, Size, Categories, Importance,
InReplyTo, IsSubmitted, IsDraft, IsFromMe, IsResend, IsUnmodified,
InternetMessageHeaders, DateTimeSent, DateTimeCreated, ResponseObjects,
ReminderDueBy, ReminderIsSet, ReminderMinutesBeforeStart, DisplayCc,
DisplayTo, HasAttachments, ExtendedProperty, Culture, Sender,
ToRecipients, CcRecipients, BccRecipients, IsReadReceiptRequested,
IsDeliveryReceiptRequested, ConversationIndex, ConversationTopic, From,
InternetMessageId, IsRead, IsResponseRequested, References, ReplyTo,
EffectiveRights, ReceivedBy, ReceivedRepresenting, LastModifiedName,
LastModifiedTime, IsAssociated, WebClientReadFormQueryString,
WebClientEditFormQueryString, ConversationId, UniqueBody
*/
msg.append("<t:" + messageType + ">");
if (this.getMessageClass()!=null) msg.append("<t:ItemClass>" + this.getMessageClass() + "</t:ItemClass>"); //<--New for sharing requests
if (this.getSubject()!=null) msg.append("<t:Subject>" + this.getSubject() + "</t:Subject>");
if (referenceId==null) msg.append("<t:Sensitivity>" + this.getSensitivity() + "</t:Sensitivity>");
//Set body
if (getBody()!=null){
msg.append("<t:Body BodyType=\"" + getBodyType() + "\">");
msg.append(wrap(body));
msg.append("</t:Body>");
};
//Set properties for new email messages
if (referenceId==null){
msg.append("<t:Importance>" + getImportance() + "</t:Importance>");
if (from!=null) msg.append("<t:Sender>" + from.toXML("t") + "</t:Sender>"); //<--Doesn't seem to work!
}
Mailbox[] ToRecipients = this.getToRecipients();
if (ToRecipients!=null){
msg.append("<t:ToRecipients>");
for (Mailbox recipient : ToRecipients){
msg.append(recipient.toXML("t"));
}
msg.append("</t:ToRecipients>");
}
Mailbox[] CcRecipients = this.getCcRecipients();
if (CcRecipients!=null){
msg.append("<t:CcRecipients>");
for (Mailbox recipient : CcRecipients){
msg.append(recipient.toXML("t"));
}
msg.append("</t:CcRecipients>");
}
Mailbox[] BccRecipients = this.getBccRecipients();
if (BccRecipients!=null){
msg.append("<t:BccRecipients>");
for (Mailbox recipient : BccRecipients){
msg.append(recipient.toXML("t"));
}
msg.append("</t:BccRecipients>");
}
if (referenceId!=null){
/*
<IsReadReceiptRequested/>
<IsDeliveryReceiptRequested/>
<From/>
*/
msg.append("<t:ReferenceItemId Id=\"" + referenceId + "\" ChangeKey=\"" + new Email(referenceId, conn).getChangeKey() + "\" />");
/*
<NewBodyContent/>
<ReceivedBy/>
<ReceivedRepresenting/>
*/
}
if (referenceId==null){
if (from!=null) msg.append("<t:From>" + from.toXML("t") + "</t:From>"); //<--Doesn't seem to work!
}
msg.append("</t:" + messageType + ">");
msg.append("</m:Items>");
msg.append("</m:CreateItem>");
msg.append("</soap:Body>");
msg.append("</soap:Envelope>");
org.w3c.dom.Document xml = conn.execute(msg.toString());
//Parse the response. Note that send events don't return an Item ID
org.w3c.dom.NodeList nodes = xml.getElementsByTagName("t:ItemId");
if (nodes!=null && nodes.getLength()>0){
id = javaxt.xml.DOM.getAttributeValue(nodes.item(0), "Id");
}
}
public String toString(){
return this.getSubject();
}
}