// Get session
Session session = Session.getDefaultInstance(props, null);
// Get the store
Store store = session.getStore(getServerType());
store.connect(getServer(), getUserName(), getPassword());
// Get folder
Folder folder = store.getFolder(getFolder());
if (deleteMessages) {
folder.open(Folder.READ_WRITE);
} else {
folder.open(Folder.READ_ONLY);
}
// Get directory
Message messages[] = folder.getMessages();
Message message;
StringBuffer pdata = new StringBuffer();
pdata.append(messages.length);
pdata.append(" messages found\n");
int n = getNumMessages();
if (n == ALL_MESSAGES || n > messages.length) {
n = messages.length;
}
for (int i = 0; i < n; i++) {
StringBuffer cdata = new StringBuffer();
SampleResult child = new SampleResult();
child.sampleStart();
message = messages[i];
//if (i == 0)
{ // Assumes all the messaged have the same type ...
child.setContentType(message.getContentType());
}
cdata.append("Message "); // $NON-NLS-1$
cdata.append(message.getMessageNumber());
child.setSampleLabel(cdata.toString());
child.setSamplerData(cdata.toString());
cdata.setLength(0);
cdata.append("Date: "); // $NON-NLS-1$
cdata.append(message.getSentDate());
cdata.append(NEW_LINE);
cdata.append("To: "); // $NON-NLS-1$
Address[] recips = message.getAllRecipients();
for (int j = 0; j < recips.length; j++) {
cdata.append(recips[j].toString());
if (j < recips.length - 1) {
cdata.append("; "); // $NON-NLS-1$
}
}
cdata.append(NEW_LINE);
cdata.append("From: "); // $NON-NLS-1$
Address[] from = message.getFrom();
for (int j = 0; j < from.length; j++) {
cdata.append(from[j].toString());
if (j < from.length - 1) {
cdata.append("; "); // $NON-NLS-1$
}
}
cdata.append(NEW_LINE);
cdata.append("Subject: "); // $NON-NLS-1$
cdata.append(message.getSubject());
cdata.append(NEW_LINE);
cdata.append(NEW_LINE);
Object content = message.getContent();
if (content instanceof MimeMultipart) {
MimeMultipart mmp = (MimeMultipart) content;
int count = mmp.getCount();
cdata.append("Multipart. Count: ");
cdata.append(count);
cdata.append(NEW_LINE);
for (int j=0; j<count;j++){
BodyPart bodyPart = mmp.getBodyPart(j);
cdata.append("Type: ");
cdata.append(bodyPart.getContentType());
cdata.append(NEW_LINE);
try {
cdata.append(bodyPart.getContent());
} catch (UnsupportedEncodingException ex){
cdata.append(ex.getLocalizedMessage());
}
cdata.append(NEW_LINE);
}
} else {
cdata.append(content);
cdata.append(NEW_LINE);
}
if (deleteMessages) {
message.setFlag(Flags.Flag.DELETED, true);
}
child.setResponseData(cdata.toString().getBytes());
child.setDataType(SampleResult.TEXT);
child.setResponseCodeOK();
child.setResponseMessage("OK"); // $NON-NLS-1$
child.setSuccessful(true);
child.sampleEnd();
parent.addSubResult(child);
}
// Close connection
folder.close(true);
store.close();
/*
* Set up the sample result details
*/
parent.setResponseData(pdata.toString().getBytes());