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
|
package com.t4hr.isiweb.impl.service.email.impl;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Locale;
import java.util.Properties;
import java.util.Set;
import javax.mail.Address;
import javax.mail.FetchProfile;
import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.UIDFolder;
import javax.mail.internet.InternetAddress;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.sun.mail.pop3.POP3Folder;
import com.sun.mail.pop3.POP3Message; // PHH New <> 1.06
import com.t4hr.isiweb.impl.service.email.Email;
import com.t4hr.isiweb.impl.service.email.EmailAddress;
import com.t4hr.isiweb.impl.service.email.EmailException;
import com.t4hr.isiweb.impl.service.email.EmailReceiver;
import com.t4hr.isiweb.impl.service.email.impl.uid.UidStore;
/**
*
* The POP3 implementation of the MailReceiver interface.
*
*/
public class Pop3EmailReceiver implements EmailReceiver {
private static final Log LOG = LogFactory.getLog(Pop3EmailReceiver.class);
// ---------------------------- Constants -----------------------------
private static final String SUB_LEVEL1 = "MMMMM.yyyy";
private static final String SUB_LEVEL2 = "dd.MMMMM.yyyy";
private static final String SUB_LEVEL3 = "HH'h'mm'm'ss's'.SS";
private static final Locale LOCALE = Locale.FRANCE;
private static final String POP3 = "pop3";
private static final String INBOX = "INBOX"; // POP3-> Only 1 folder :
// "INBOX"
private static final String RFC822_EXTENSION = ".eml";
private static final String DATA_EXTENSION = ".dat";
private static final String TEXT_EXTENSION = ".txt";
private static final String HTML_EXTENSION = ".html";
private static final String NO_FILENAME_TEXT = "(text)";
private static final String NO_FILENAME_HTML = "(html)";
private static final String NO_FILENAME_APPLICATION = "(application)";
private static final String NO_FILENAME_VIDEO = "(video)";
private static final String NO_FILENAME_AUDIO = "(audio)";
private static final String NO_FILENAME_IMAGE = "(image)";
private static final String NO_FILENAME_MESSAGE = "(message)";
// ---------------------------- Properties ----------------------------
/**
* Server used to receive mails
*/
private String pop3Server;
/**
* User name.
*/
private String userName;
/**
* User password.
*/
private String password;
/**
* Local folder for incoming mails
*/
private File repository;
/**
* Serialize the messages?
*/
private boolean autoSerialization;
/**
* Enable the retrieval determined by the server uids. Used to retrieve
* mails that were not retrieved.
*/
private UidStore uidStore;
/**
* Delete the mails that were successfully received
*/
private boolean autoDelete;
/**
* Debug infos in console..
*/
private boolean debug;
// ---------------------------- Constructors --------------------------
public Pop3EmailReceiver() {
this(null, null, null, null);
}
/**
* Creates a new POP3MailReceiver with the given POP3 server, the
* username/password informations and the repository where to detach the
* mails attachments. By default : * RFC822 auto-serialization of the
* messages to the local folder is enabled * No UID store used * Messages
* auto-delete on the server is disabled * Debug informations are hidden
*
* @param pop3Server
* String
* @param userName
* String
* @param password
* String
* @param repository
* File
*/
public Pop3EmailReceiver(String pop3Server, String userName, String password, File repository) {
this(pop3Server, userName, password, repository, true, null);
}
public Pop3EmailReceiver(String pop3Server, String userName, String password, File repository,
boolean autoSerialization) {
this(pop3Server, userName, password, repository, autoSerialization, null);
}
public Pop3EmailReceiver(String pop3Server, String userName, String password, File repository,
boolean autoSerialization, UidStore uidStore) {
this(pop3Server, userName, password, repository, autoSerialization, uidStore, false);
}
public Pop3EmailReceiver(String pop3Server, String userName, String password, File repository,
boolean autoSerialization, UidStore uidStore, boolean autoDelete) {
this(pop3Server, userName, password, repository, autoSerialization, uidStore, autoDelete, false);
}
public Pop3EmailReceiver(String pop3Server, String userName, String password, File repository,
boolean autoSerialization, UidStore uidStore, boolean autoDelete, boolean debug) {
this.pop3Server = pop3Server;
this.userName = userName;
this.password = password;
this.repository = repository;
this.autoSerialization = autoSerialization;
this.uidStore = uidStore;
this.autoDelete = autoDelete;
this.debug = debug;
}
// /////////////////////////////METHODS//////////////////////////////////////
// ------------- Public
/**
* Receives the emails contained in the "INBOX" folder.
*
* @return Set
*/
public Set<Email> receive() {
Set<Email> emails = null;
Store store = null;
POP3Folder inbox = null;
boolean expungeDeletedMails = false;
try {
// Create the Session
Properties props = System.getProperties();
Session session = Session.getInstance(props, null);
session.setDebug(this.debug);
// Connection on the server
store = session.getStore(POP3);
store.connect(this.pop3Server, this.userName, this.password);
// Check the "Inbox"
inbox = (POP3Folder) store.getFolder(INBOX);
inbox.open(Folder.READ_WRITE);
// >>>>>>>>>>>>>>>> ***** Nous y sommes *****
POP3Message[] messages = (POP3Message[]) inbox.getMessages(); // ***** Nous y sommes *****
// >>>>>>>>>>>>>>>>
/*
* If UID store is enabled -> UIDs for all messages in the folder
* are fetched using the POP3 UIDL command
*/
if (this.uidStore != null) {
FetchProfile profile = new FetchProfile();
profile.add(UIDFolder.FetchProfileItem.UID);
inbox.fetch(messages, profile);
}
// Mail
if ((messages != null) && (messages.length > 0)) {
for (POP3Message message : messages) {
// If the UID based checking is enabled
String uid = null;
if (this.uidStore != null) {
uid = inbox.getUID(message).trim();
}
if ((this.uidStore == null) || ((this.uidStore != null) && (!this.uidStore.contains(uid)))) {
// Process the message
Email email = this.map(message);
if (email != null) {
if (emails == null) {
emails = new LinkedHashSet<Email>();
}
emails.add(email);
}
// Store the new uid
if (this.uidStore != null) {
this.uidStore.store(uid);
}
} else {
// If the message is not new -> skip..
LOG.debug("Old mail skipped - UID : " + uid);
}
// Flag to "deleted"
if (this.autoDelete) {
message.setFlag(Flags.Flag.DELETED, true);
}
}
}
// Everything is ok until now -> allow delete
expungeDeletedMails = true;
} catch (Throwable throwable) {
StringBuilder msg = new StringBuilder();
msg.append("Cannot receive the mails ");
msg.append("[POP3: ");
msg.append(this.pop3Server);
msg.append("; UserName: ");
msg.append(this.userName);
msg.append("]");
throw new EmailException(msg.toString(), throwable);
} finally {
try {
if (inbox != null) {
// Close the "Inbox" and remove retreived mails
// (Flag.DELETED)
inbox.close(expungeDeletedMails);
}
if (store != null) {
store.close();
}
} catch (Throwable throwable) {
throw new EmailException(throwable);
}
}
if (emails == null) {
return new LinkedHashSet<Email>();
}
return emails;
}
// ------------- Private
/**
* Maps a Message to an Email.
*
* @param message
* Message
* @return Email
* @throws IOException
* @throws MessagingException
*/
private Email map(POP3Message message) throws IOException, MessagingException {
Email email = null;
/*
* Current date/time is used to build local subfolders
* message.getReceivedDate() returns null sometimes..
*/
Date receivedDate = new Date();
// Serialize to localfolder
if (this.autoSerialization) {
this.serialize(message, receivedDate);
}
// Recipients
Set<EmailAddress> fromAdresses = this.map(message.getFrom());
EmailAddress from = null;
if (fromAdresses != null) {
if (fromAdresses.size() > 0) {
LOG.error("More than one email address found in \"from\"");
}
Iterator<EmailAddress> iterator = fromAdresses.iterator();
from = iterator.next();
}
Set<EmailAddress> to = this.map(message.getRecipients(RecipientType.TO));
Set<EmailAddress> cc = this.map(message.getRecipients(RecipientType.CC));
Set<EmailAddress> bcc = this.map(message.getRecipients(RecipientType.BCC));
// Subject
String subject = message.getSubject();
email = new Email();
email.setFrom(from);
email.setTo(to);
email.setSubject(subject);
email.setCc(cc);
email.setBcc(bcc);
email.setReceivedDate(receivedDate);
email.setSentDate(message.getSentDate());
this.processPart(message, email, receivedDate);
return email;
}
.....
Getters et Setters
} |
Partager