InputStream mailAsStream =
session.getMaildrop().getMailAsStream(messageNumber);
try {
session.getThread().sendResponse("+OK");
ExtraDotOutputStream dotOutputStream =
new ExtraDotOutputStream(session.getThread()
.getOutputStream());
CrLfInputStream in = new CrLfInputStream(mailAsStream);
byte[] buffer = new byte[1000];
int cRead;
// read headers
while (true) {
cRead = in.readLineWithEol(buffer);
if (cRead == -1)
break; // no body
dotOutputStream.write(buffer, 0, cRead);
if (buffer[0] == '\r' || buffer[0] == '\n')
break;
}
// read body
int cLines = 0;
while (cLines < lines) {
try {
cRead = in.readLineWithEol(buffer);
} catch (MaxLineLengthException e) {
// not a big problem, some body lines will be missing
break;
}
if (cRead == -1)
break; // end of mail
dotOutputStream.write(buffer, 0, cRead);
cLines++;
}
dotOutputStream.flush();
} finally {
mailAsStream.close();
}
session.getThread().sendResponse(".");
}