Recent Changes - Search:

Network Programming

This website demonstrates using wikis as teaching and learning tool.

The course instructor is happy to share the teaching materials here with those who find it readable.

WikiSandbox

Feel free to use this page to experiment with the Text Formatting Rules. Just click the "Edit Page" link at the bottom of the page.


public void sendMail(String toAddress, String fromAddress,String subject, String messageBody) throws IOException, ProtocolException, UnknownHostException {
        messageArea.setText("");
        printMessage("** Sending Mail Now **");
        printMessage("** From     : " + fromAddress);
        printMessage("** To       : " + toAddress);
        printMessage("** Subject  : " + subject);
        printMessage("** Message  : " + messageBody);
        printMessage("-----------------------------------------------");

        String messageRecv;
        // KL: Use your own ISP's SMTP server instead!!
        // String host = new String("mail.netvigator.com");
        String host = new String("smtp.myrealbox.com");
        Socket socket = new Socket(host, 25);
        //DataInputStream in = new DataInputStream(socket.getInputStream());
        BufferedReader in = new BufferedReader(
                        new InputStreamReader(socket.getInputStream()));
        PrintStream out = new PrintStream(socket.getOutputStream());
        messageRecv = in.readLine();
        printMessage(messageRecv);
        if (!messageRecv.startsWith("220"))
                throw new ProtocolException(messageRecv);

        while (messageRecv.indexOf('-') == 3) {
                messageRecv = in.readLine();
                printMessage(messageRecv);
                if (!messageRecv.startsWith("220"))
                        throw new ProtocolException(messageRecv);
        }

        //out.println( "HELO " + host );     out.flush();
        //messageRecv = in.readLine();  printMessage(messageRecv);

        //if (!messageRecv.startsWith("250"))
        //      throw new ProtocolException(messageRecv);
        out.println( "MAIL FROM: " + fromAddress ); out.flush();
        messageRecv = in.readLine();
        printMessage(messageRecv);

        if (!messageRecv.startsWith("250"))
                throw new ProtocolException(messageRecv);

        out.println( "RCPT TO: " + toAddress ); out.flush();
        messageRecv = in.readLine();

        printMessage(messageRecv);
        if (!messageRecv.startsWith("250"))
                throw new ProtocolException(messageRecv);

        out.println( "DATA" ); out.flush();
        messageRecv = in.readLine();

        printMessage(messageRecv);
        if (!messageRecv.startsWith("354"))
                throw new ProtocolException(messageRecv);

        out.println("From: " + fromAddress);
        out.println("To: " + toAddress);
        out.println( "Subject: " + subject + "\n" );
        out.flush();
        out.println("Comment: Unauthenticated sender");
        out.println("X-Mailer: SimpleSendMail");
        out.println("");
        out.println( messageBody );
        out.println(".");
        out.flush();
        messageRecv = in.readLine();
        printMessage(messageRecv);

        if (!messageRecv.startsWith("250"))
                throw new ProtocolException(messageRecv);
        out.println("QUIT");
        out.flush();
        in.close();

        printMessage("** Your mail has been sent.");
        socket.close();
        return;
}

public void printMessage(String str) {
        messageArea.append(str + "\n");
        return;
}
Edit - History - Print - Recent Changes - Search
Page last modified on June 18, 2009, at 10:04 PM