Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-12-2008, 09:14 AM
Member
 
Join Date: Nov 2008
Posts: 2
Rep Power: 0
babbulu99 is on a distinguished road
Default SMTPConncetion class
Can anyone know how to fill in blanks as /* Fill in */ fro below


This is the code for the SMTPConncetion class that you will need to complete. The code for
the other three classes is provided on this page.
import java.net.*;
import java.io.*;
import java.util.*;
/**
* Open an SMTP connection to a mailserver and send one mail.
*
*/
public class SMTPConnection {
/* The socket to the server */
private Socket connection;
/* Streams for reading and writing the socket */
private BufferedReader fromServer;
private DataOutputStream toServer;
private static final int SMTP_PORT = 25;
private static final String CRLF = "\r\n";
/* Are we connected? Used in close() to determine what to do. */
private boolean isConnected = false;
/* Create an SMTPConnection object. Create the socket and the
associated streams. Initialize SMTP connection. */
public SMTPConnection(Envelope envelope) throws IOException {
// connection = /* Fill in */;
fromServer = /* Fill in */;
toServer = /* Fill in */;
/* Fill in */
/* Read a line from server and check that the reply code is
220.
If not, throw an IOException. */
/* Fill in */
/* SMTP handshake. We need the name of the local machine.
Send the appropriate SMTP handshake command. */
String localhost = /* Fill in */;
sendCommand( /* Fill in */ );
isConnected = true;
}
/* Send the message. Write 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 {
/* Fill in */
/* Send all the necessary commands to send a message. Call
sendCommand() to do the dirty work. Do _not_ catch the
exception thrown from sendCommand(). */
/* Fill in */
}
/* Close the connection. First, terminate on SMTP level, then
close the socket. */
public void close() {
isConnected = false;
try {
sendCommand( /* Fill in */ );
// connection.close();
} catch (IOException e) {
System.out.println("Unable to close connection: " + e);
isConnected = true;
}
}
/* Send an SMTP command to the server. Check that the reply code
is
what is is supposed to be according to RFC 821. */
private void sendCommand(String command, int rc) throws
IOException {
/* Fill in */
/* Write command to server and read reply from server. */
/* Fill in */
/* Fill in */
/* Check that the server's reply code is the same as the
parameter
rc. If not, throw an IOException. */
/* Fill in */
}
/* Parse the reply line from the server. Returns the reply code.
*/
private int parseReply(String reply) {
/* Fill in */
}
/* Destructor. Closes the connection if something bad happens. */
protected void finalize() throws Throwable {
if(isConnected) {
close();
}
super.finalize();
}
}
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 12-14-2008, 06:56 AM
Member
 
Join Date: Dec 2008
Posts: 1
Rep Power: 0
dolly86 is on a distinguished road
Smile Program with blanks filled.... njoy
import java.net.*;
import java.io.*;
import java.util.*;

/**
* Open an SMTP connection to a remote machine and send one mail.
*
*/
public class SMTPConnection {
/* The socket to the server */
private Socket connection;

/* Streams for reading and writing the socket */
private BufferedReader fromServer;
private DataOutputStream toServer;

private static final int SMTP_PORT = 25;
private static final String CRLF = "\r\n";

/* Are we connected? Used in close() to determine what to do. */
private boolean isConnected = false;

/* Create an SMTPConnection object. Create the socket and the
associated streams. Initialize SMTP connection. */
public SMTPConnection(Envelope envelope) throws IOException {

connection = new Socket("postaci.ug.bilkent.edu.tr",SMTP_PORT);
fromServer = new BufferedReader(new InputStreamReader(connection.getInputStream()));
toServer = new DataOutputStream(connection.getOutputStream());


/* Read a line from server and check that the reply code is 220.
If not, throw an IOException. */

String line = fromServer.readLine();
int replyCode = parseReply(line);
if(replyCode != 220)
throw new IOException("Invalid reply code");

/* SMTP handshake. We need the name of the local machine.
Send the appropriate SMTP handshake command. */
String localhost = "csulb.edu";
String handshake = "HELO " + localhost + CRLF;
sendCommand( handshake, 250 );

isConnected = true;

}

/* Send the message. Write 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 {

/* Send all the necessary commands to send a message. Call
sendCommand() to do the dirty work. Do _not_ catch the
exception thrown from sendCommand(). */


String mailFromLine = "MAIL FROM: " + envelope.Sender + CRLF;
String rcptToLine = "";
String dataLine = "DATA" + CRLF;
String messageLine = envelope.Message.Headers + envelope.Message.Body+CRLF +"." + CRLF;

sendCommand(mailFromLine, 250);

sendCommand(dataLine, 354);
sendCommand(messageLine, 250);



}

/* Close the connection. First, terminate on SMTP level, then
close the socket. */
public void close() {

isConnected = false;
try {
sendCommand( "QUIT "+CRLF, 221 );
connection.close();
}
catch (IOException e) {

System.out.println("Unable to close connection: " + e);
isConnected = true;
}
}

/* Send an SMTP command to the server. Check that the reply code
is what is is supposed to be according to RFC 821. */
private void sendCommand(String command, int rc) throws IOException

{

/* Write command to server and read reply from server. */

toServer.writeBytes(command);
String reply = fromServer.readLine();
int replyCode = parseReply(reply);


/* Check that the server's reply code is the same as the
parameter rc. If not, throw an IOException. */
if(replyCode != rc)
throw new IOException("Invalid reply code");



}

/* Parse the reply line from the server. Returns the reply
code. */
private int parseReply(String reply) {

StringTokenizer tokens = new StringTokenizer(reply," ");
String rc = tokens.nextToken();
int replyCode = Integer.parseInt(rc);

return replyCode;
}

/* Destructor. Closes the connection if something bad happens. */
protected void finalize() throws Throwable {
if(isConnected) {
close();
}
super.finalize();
}
}
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Calling a method on original class from created class kpedersen Advanced Java 4 08-20-2008 12:25 AM
Able to find class file in WEB-INF/classes but not after add sub folders in class dir vitalstrike82 Web Frameworks 0 05-13-2008 06:16 AM
Class Reflection: Finding super class names Java Tip java.lang 0 04-23-2008 08:12 PM
Class Reflection: Finding class modifiers Java Tip java.lang 0 04-23-2008 08:11 PM
what is the Priority for execution of Interface class and a Abstract class Santoshbk Advanced Java 0 04-02-2008 07:04 AM


All times are GMT +2. The time now is 03:09 AM.



VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org