/* * Close the connection. Try to send QUIT-commmand and then close the * socket. */ public void close() { isConnected = false; try { sendCommand("QUIT", 221); connection.close(); } catch (IOException e) { System.out.println("Unable to close connection: " + e); isConnected = true; } }
/* * Create an SMTPConnection object. Create the socket and the associated * streams. Send HELO-command and check for errors. */ public SMTPConnection(Envelope envelope) throws IOException { connection = new Socket(envelope.DestAddr, SMTP_PORT); fromServer = new BufferedReader(new InputStreamReader(connection.getInputStream())); toServer = new DataOutputStream(connection.getOutputStream()); String reply = fromServer.readLine(); if (parseReply(reply) != 220) { System.out.println("Error in connect."); System.out.println(reply); return; } String localhost = (InetAddress.getLocalHost()).getHostName(); try { sendCommand("HELO " + localhost, 250); } catch (IOException e) { System.out.println("HELO failed. Aborting."); return; } isConnected = true; }
/* * Send the message. Simply writes the correct SMTP-commands in the correct * order. No checking for errors, just throw them to the caller. */ public void send(Envelope envelope) throws IOException { sendCommand("MAIL FROM:<" + envelope.Sender + ">", 250); sendCommand("RCPT TO:<" + envelope.Recipient + ">", 250); sendCommand("DATA", 354); sendCommand(envelope.Message.toString() + CRLF + ".", 250); }