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
| import com.sun.mail.pop3.POP3Store;
import java.util.Date;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.FetchProfile;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Part;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.URLName;
import javax.mail.internet.ContentType;
import javax.mail.internet.ParseException;
public class ExchangeUtilities {
private Session session = null;
private Store store = null;
private String username, password;
private Folder folder;
public ExchangeUtilities() {
}
public void setUserPass(String username, String password) {
this.username = username;
this.password = password;
}
public void connect() {
try {
int port = 110; // 995
Properties pop3Props = new Properties();
URLName url = new URLName("pop3", "exchange2010.exch.com", port,
"", username, password);
session = Session.getInstance(pop3Props, null);
this.store = new POP3Store(session, url);
System.out.println("Sore : " + store.getURLName().toString());
this.store.connect();
} catch (MessagingException e) {
e.printStackTrace();
}
}
public void openFolder(String folderName) throws Exception {
// Open the Folder
folder = store.getDefaultFolder();
// folder = folder.getFolder(folderName);
folder = folder.getFolder(folderName);
if (folder == null) {
throw new Exception("Invalid folder");
}
// try to open read/write and if that fails try read-only
try {
folder.open(Folder.READ_WRITE);
} catch (MessagingException ex) {
folder.open(Folder.READ_ONLY);
}
}
public void closeFolder() throws Exception {
folder.close(false);
}
public int getMessageCount() {
int messageCount = -1;
try {
messageCount = folder.getMessageCount();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return messageCount;
}
public int getNewMessageCount() throws Exception {
return folder.getNewMessageCount();
}
public void disconnect() throws Exception {
store.close();
}
public void printMessage(int messageNo) throws Exception {
System.out.println("Getting message number: " + messageNo);
Message m = null;
try {
m = folder.getMessage(messageNo);
dumpPart(m);
} catch (IndexOutOfBoundsException iex) {
System.out.println("Message number out of range");
}
}
public void printAllMessageEnvelopes() throws Exception {
// Attributes & Flags for all messages ..
Message[] msgs = folder.getMessages();
// Use a suitable FetchProfile
FetchProfile fp = new FetchProfile();
fp.add(FetchProfile.Item.ENVELOPE);
folder.fetch(msgs, fp);
for (int i = 0; i < msgs.length; i++) {
System.out.println("--------------------------");
System.out.println("MESSAGE #" + (i + 1) + ":");
dumpEnvelope(msgs[i]);
}
}
public void printAllMessages() throws Exception {
// Attributes & Flags for all messages ..
Message[] msgs = folder.getMessages();
// Use a suitable FetchProfile
FetchProfile fp = new FetchProfile();
fp.add(FetchProfile.Item.ENVELOPE);
folder.fetch(msgs, fp);
for (int i = 0; i < msgs.length; i++) {
// i++
System.out.println("--------------------------");
System.out.println("MESSAGE #" + (i + 1) + ":");
dumpPart(msgs[i]);
}
}
public void printAllMessages3() throws Exception {
try {
Message[] msgs;
int messageCount = folder.getMessageCount();
int nbr = messageCount;
int lot = 100;
int i = 0;
int start, end;
// Use a suitable FetchProfile
FetchProfile fp = new FetchProfile();
fp.add(FetchProfile.Item.ENVELOPE);
while (nbr > 0) {
int x = Math.min(lot, nbr);
start = i * lot;
end = ((i * lot) + x) - 1;
i++;
System.out.println("Start : " + start + " | End : " + end); // FT
msgs = folder.getMessages(start + 1, end + 1);
folder.fetch(msgs, fp);
for (int j = start, k = 0; j < start + msgs.length; j++) {
System.out.println("--------------------------");
System.out.println("MESSAGE #" + (j + 1) + ":");
dumpPart(msgs[k]);
dumpEnvelope((Message) msgs[k]);
String ct = msgs[k].getContentType();
try {
pr("CONTENT-TYPE: " + (new ContentType(ct)).toString());
} catch (ParseException pex) {
pr("BAD CONTENT-TYPE: " + ct);
}
/*
* Using isMimeType to determine the content type avoids
* fetching the actual content data until we need it.
*/
if (msgs[k].isMimeType("text/plain")) {
pr("This is plain text");
pr("---------------------------");
System.out.println((String) msgs[k].getContent());
} else {
// just a separator
pr("---------------------------");
}
k++;
}
nbr -= lot;
msgs = null;
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void dumpPart(Part p) throws Exception {
if (p instanceof Message)
dumpEnvelope((Message) p);
String ct = p.getContentType();
try {
pr("CONTENT-TYPE: " + (new ContentType(ct)).toString());
} catch (ParseException pex) {
pr("BAD CONTENT-TYPE: " + ct);
}
/*
* Using isMimeType to determine the content type avoids fetching the
* actual content data until we need it.
*/
if (p.isMimeType("text/plain")) {
pr("This is plain text");
pr("---------------------------");
System.out.println((String) p.getContent());
} else {
// just a separator
pr("---------------------------");
}
}
public static void dumpEnvelope(Message m) throws Exception {
pr(" ");
Address[] a;
// FROM
if ((a = m.getFrom()) != null) {
for (int j = 0; j < a.length; j++)
pr("FROM: " + a[j].toString());
}
// TO
if ((a = m.getRecipients(Message.RecipientType.TO)) != null) {
for (int j = 0; j < a.length; j++) {
pr("TO: " + a[j].toString());
}
}
// SUBJECT
pr("SUBJECT: " + m.getSubject());
// DATE
Date d = m.getSentDate();
pr("SendDate: " + (d != null ? d.toString() : "UNKNOWN"));
}
static String indentStr = " ";
static int level = 0;
/**
* Print a, possibly indented, string.
*/
public static void pr(String s) {
System.out.print(indentStr.substring(0, level * 2));
System.out.println(s);
}
} |
Partager