-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExchangeSource.java
More file actions
176 lines (153 loc) · 5.86 KB
/
ExchangeSource.java
File metadata and controls
176 lines (153 loc) · 5.86 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
import java.net.*;
import java.io.*;
import java.util.LinkedList;
import microsoft.exchange.webservices.data.credential.*;
import microsoft.exchange.webservices.data.core.*;
import microsoft.exchange.webservices.data.core.enumeration.misc.*;
import microsoft.exchange.webservices.data.core.enumeration.property.*;
import microsoft.exchange.webservices.data.core.enumeration.search.*;
import microsoft.exchange.webservices.data.core.enumeration.service.*;
import microsoft.exchange.webservices.data.search.*;
import microsoft.exchange.webservices.data.core.service.item.*;
import microsoft.exchange.webservices.data.property.complex.*;
import microsoft.exchange.webservices.data.search.filter.*;
import microsoft.exchange.webservices.data.core.service.schema.*;
import mcpy.emailModel.Email;
class ExchangeSource {
private String userName;
private String password;
private String uri;
private ExchangeService service;
public ExchangeSource(String userName_, String password_, String Uri_) {
userName = userName_;
password = password_;
uri = Uri_;
}
public void connectExchange() {
try {
service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
ExchangeCredentials credentials = new WebCredentials(userName, password);
service.setCredentials(credentials);
service.setUrl(new URI(uri));
} catch (Exception e) {
System.out.println("connectExchange exception!");
}
}
public LinkedList < Email > listItems() {
try {
int offset = 0;
LinkedList < Email > messages = new LinkedList < Email > ();
while (true) {
int inOffset = offset;
ItemView view = new ItemView(100, offset);
SearchFilter unreadFilter = new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false);
FindItemsResults < Item > findResults = service.findItems(new FolderId(WellKnownFolderName.Inbox), view);
for (Item item: findResults.getItems()) {
Email cur = new Email();
// Do something with the item as shown
System.out.println("Message details:");
System.out.println("id==========" + item.getId());
System.out.println("sub==========" + item.getSubject());
System.out.println("Item class:: " + item.getItemClass());
//Copy details into Email object..
cur.subject = item.getSubject();
if (item.getItemClass().compareTo("IPM.Note") == 0) {
EmailMessage msg = EmailMessage.bind(service, item.getId());
MessageBody mbdy = msg.getBody();
if (msg.getHasAttachments() == false) {
System.out.println("Message without attachments..");
cur.contents = mbdy.toString();
if (mbdy.getBodyType() == BodyType.HTML) {
cur.contType = "text/html";
} else {
cur.contType = "text/plain";
}
} else {
System.out.println("Message with attachments..");
AttachmentCollection ac = msg.getAttachments();
for (Attachment at: ac) {
System.out.println("Attachment!");
if (at instanceof ItemAttachment) {
ItemAttachment ia = (ItemAttachment) at;
System.out.println("Item Attachment");
System.out.println("Message with attachments..");
ia.load();
cur.contType = ia.getContentType();
System.out.println("Message with attachments.. content type " + cur.contType);
System.out.println("Message with attachments..");
//cur.contents = new String(ia.getItem().getMimeContent().getContent());
System.out.println(ia.getItem().getMimeContent().getContent());
System.out.println("Message with attachments..");
} else if (at instanceof FileAttachment) {
System.out.println("File attachment.");
}
}
}
cur.headers = new mcpy.emailModel.Header[msg.getInternetMessageHeaders().getCount()];
int j = 0;
for (microsoft.exchange.webservices.data.property.complex.InternetMessageHeader mh: msg.getInternetMessageHeaders()) {
mcpy.emailModel.Header nh = new mcpy.emailModel.Header();
nh.name = mh.getName();
nh.value = mh.getValue();
cur.headers[j] = nh;
j++;
}
System.out.println("listItems");
cur.to = new mcpy.emailModel.EmailAddress[msg.getToRecipients().getCount()];
j = 0;
for (microsoft.exchange.webservices.data.property.complex.EmailAddress ea: msg.getToRecipients()) {
mcpy.emailModel.EmailAddress ead = new mcpy.emailModel.EmailAddress();
ead.address = ea.getAddress();
String nn = ea.getName();
if (nn.length() > 0) {
ead.hasFullName = true;
ead.fullName = nn;
}
cur.to[j] = ead;
j++;
}
System.out.println("listItems");
cur.cc = new mcpy.emailModel.EmailAddress[msg.getCcRecipients().getCount()];
j = 0;
for (microsoft.exchange.webservices.data.property.complex.EmailAddress ea: msg.getCcRecipients()) {
mcpy.emailModel.EmailAddress ead = new mcpy.emailModel.EmailAddress();
ead.address = ea.getAddress();
String nn = ea.getName();
if (nn.length() > 0) {
ead.hasFullName = true;
ead.fullName = nn;
}
cur.cc[j] = ead;
j++;
}
System.out.println("listItems");
mcpy.emailModel.EmailAddress from = new mcpy.emailModel.EmailAddress();
from.address = msg.getFrom().getAddress();
from.fullName = msg.getFrom().getName();
from.hasFullName = true;
cur.from = from;
System.out.println("listItems");
cur.isRead = msg.getIsRead();
if (!cur.isRead) {
msg.setIsRead(true);
msg.update(ConflictResolutionMode.AlwaysOverwrite);
}
cur.date = msg.getLastModifiedTime();
}
if (!cur.isRead) {
messages.addLast(cur);
}
System.out.println("\n\n");
offset++;
}
if (offset == inOffset) {
break;
}
}
return messages;
} catch (Exception e) {
System.out.println("listFirstTenItems exception!");
return new LinkedList < Email > ();
}
}
}