Socket smtp = new Socket("localhost", 25);
PrintWriter out = new PrintWriter(smtp.getOutputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(smtp.getInputStream()));
- readResponse(in);
+ readSMTPResponse(in, 220);
out.print("HELO www.cacert.org\r\n");
out.flush();
- readResponse(in);
+ readSMTPResponse(in, 250);
out.print("MAIL FROM:<returns@cacert.org>\r\n");
out.flush();
- readResponse(in);
+ readSMTPResponse(in, 250);
bits = to.split(",");
for (String user : bits) {
out.print("RCPT TO:<" + user.trim() + ">\r\n");
out.flush();
- readResponse(in);
+ readSMTPResponse(in, 250);
}
out.print("DATA\r\n");
out.flush();
- readResponse(in);
+ readSMTPResponse(in, 250);
out.print("X-Mailer: CAcert.org Website\r\n");
// if (array_key_exists("REMOTE_ADDR", $_SERVER)) {
// out.print("X-OriginatingIP: ".$_SERVER["REMOTE_ADDR"]."\r\n");
// TODO
SimpleDateFormat emailDate = new SimpleDateFormat("E, d MMM yyyy HH:mm:ss ZZZZ (z)", Locale.ENGLISH);
out.print("Date: " + emailDate.format(new Date(System.currentTimeMillis())) + "\r\n");
- out.print("Sender: " + errorsto + "\r\n");
- out.print("Errors-To: " + errorsto + "\r\n");
+ if (errorsto != null) {
+ out.print("Sender: " + errorsto + "\r\n");
+ out.print("Errors-To: " + errorsto + "\r\n");
+ }
if (replyto != null) {
out.print("Reply-To: " + replyto + "\r\n");
} else {
smtp.close();
return;
}
- readResponse(in);
+ readSMTPResponse(in, 250);
out.print("QUIT\n");
out.flush();
- readResponse(in);
+ readSMTPResponse(in, 221);
smtp.close();
}
- private static void readResponse(BufferedReader in) throws IOException {
+ public static boolean readSMTPResponse(BufferedReader in, int code) throws IOException {
String line;
- while ((line = in.readLine()) != null && line.matches("\\d+-")) {
+ while ((line = in.readLine()) != null) {
+ if (line.startsWith(code + " ")) {
+ return true;
+ } else if ( !line.startsWith(code + "-")) {
+ return false;
+ }
}
+ return false;
}