Hey, I agree with pavan..
There is a call needed on the action performed method that will execute the code.have you closed your objects.again, implement threading & listener for a good email code. Have you tried apache commons http email client ? Thanks, jitesh dundas On 7/4/10, Prateek Rawal....... <[email protected]> wrote: > Hey everyone, i'm trying to develop a email program with client and server. > The client will send the message containing the details: > 1. To whom it should be send > 2. Subject > 3. Message Part > > This message will go to server which in turn will redirect it to the > expected destination(the one contained in the "To" part of the message) > > I have written the code for both client and server which are as follows: > > CLIENT code(I have written it in NetBeans): > > > 1. import java.io.*; > 2. import java.net.*; > 3. import javax.swing.JOptionPane; > 4. > 5. public class Client extends javax.swing.JFrame implements Runnable { > 6. > 7. /** Creates new form Client */ > 8. public Client() { > 9. initComponents(); > 10. } > 11. public void run(){ > 12. ServerSocket ssoc; > 13. Socket ss,sen; > 14. try { > 15. > 16. ss = new Socket(InetAddress.getByName("Invictus-PC"),5100 > ); > 17. output = new ObjectOutputStream(ss.getOutputStream()); > 18. output.flush(); > 19. ssoc = new ServerSocket(5000); > 20. > 21. while(true){ > 22. > 1. sen = ssoc.accept(); > 2. input = new ObjectInputStream(sen.getInputStream()); > > 23. String s = (String) input.readObject(); > 24. int i = s.indexOf(",",0); > 25. int j = s.indexOf(",",i+1); > 26. int k = s.indexOf(",",j+1); > 27. String s1 = s.substring(0,i); > 28. String s2 = s.substring(i+1,j); > 29. String s3 = s.substring(j+1); > 30. jTextArea2.append( > "New Message Recieved and the Details are as under:\n"); > 31. jTextArea2.append("Message sent by: "+s1+"\n"); > 32. jTextArea2.append("Message subject is: "+s2+"\n"); > 33. jTextArea2.append("Actual message is: "+s3+"\n \n \n \n" > ); > 34. > jTextArea2.setCaretPosition(jTextArea2.getText().length()); > 35. > 36. sen.close(); > 37. input.close(); > 38. } > 39. } > 40. catch(Exception e){ > 41. e.printStackTrace(); > 42. } > 43. > 44. } > 45. > 46. > 47. @SuppressWarnings("unchecked") > 48. > // <editor-fold defaultstate="collapsed" desc="Generated Code"> > > 49. private void initComponents() { > 50. > 51. jTextField1 = new javax.swing.JTextField(); > 52. jTextField2 = new javax.swing.JTextField(); > 53. jTextField3 = new javax.swing.JTextField(); > 54. jButton1 = new javax.swing.JButton(); > 55. jButton2 = new javax.swing.JButton(); > 56. jLabel1 = new javax.swing.JLabel(); > 57. jLabel2 = new javax.swing.JLabel(); > 58. jLabel3 = new javax.swing.JLabel(); > 59. jScrollPane1 = new javax.swing.JScrollPane(); > 60. jTextArea1 = new javax.swing.JTextArea(); > 61. jScrollPane2 = new javax.swing.JScrollPane(); > 62. jTextArea2 = new javax.swing.JTextArea(); > 63. jLabel4 = new javax.swing.JLabel(); > 64. jLabel5 = new javax.swing.JLabel(); > 65. > 66. > > setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); > 67. getContentPane().setLayout(null); > 68. getContentPane().add(jTextField1); > 69. jTextField1.setBounds(60, 30, 260, 50); > 70. getContentPane().add(jTextField2); > 71. jTextField2.setBounds(60, 100, 260, 40); > 72. getContentPane().add(jTextField3); > 73. jTextField3.setBounds(60, 160, 260, 140); > 74. > 75. jButton1.setText("Send"); > 76. jButton1.addActionListener(new > java.awt.event.ActionListener() { > 77. public void > actionPerformed(java.awt.event.ActionEvent evt) { > 78. jButton1ActionPerformed(evt); > 79. } > 80. }); > 81. getContentPane().add(jButton1); > 82. jButton1.setBounds(60, 340, 100, 30); > 83. > 84. jButton2.setText("Reset"); > 85. jButton2.addActionListener(new > java.awt.event.ActionListener() { > 86. public void > actionPerformed(java.awt.event.ActionEvent evt) { > 87. jButton2ActionPerformed(evt); > 88. } > 89. }); > 90. getContentPane().add(jButton2); > 91. jButton2.setBounds(220, 340, 100, 30); > 92. > 93. jLabel1.setText("To:"); > 94. getContentPane().add(jLabel1); > 95. jLabel1.setBounds(20, 20, 40, 50); > 96. > 97. jLabel2.setText("Subject:"); > 98. getContentPane().add(jLabel2); > 99. jLabel2.setBounds(10, 100, 50, 40); > 100. > 101. jLabel3.setText("Message:"); > 102. getContentPane().add(jLabel3); > 103. jLabel3.setBounds(10, 150, 46, 40); > 104. > 105. jTextArea1.setColumns(20); > 106. jTextArea1.setRows(5); > 107. jScrollPane1.setViewportView(jTextArea1); > 108. > 109. getContentPane().add(jScrollPane1); > 110. jScrollPane1.setBounds(500, 30, 380, 150); > 111. > 112. jTextArea2.setColumns(20); > 113. jTextArea2.setRows(5); > 114. jScrollPane2.setViewportView(jTextArea2); > 115. > 116. getContentPane().add(jScrollPane2); > 117. jScrollPane2.setBounds(500, 230, 380, 170); > 118. > 119. jLabel4.setText("Sending Message Details:"); > 120. getContentPane().add(jLabel4); > 121. jLabel4.setBounds(360, 30, 130, 150); > 122. > 123. jLabel5.setText("Recieving Message Details:"); > 124. getContentPane().add(jLabel5); > 125. jLabel5.setBounds(360, 230, 130, 170); > 126. > 127. pack(); > 128. }// </editor-fold> > 129. > 130. private void > jButton2ActionPerformed(java.awt.event.ActionEvent evt) { > 131. jTextField1.setText(""); > 132. jTextField2.setText(""); > 133. jTextField3.setText(""); > 134. } > 135. > 136. private void > jButton1ActionPerformed(java.awt.event.ActionEvent evt) { > 137. try { > 138. > 139. if( (jTextField1.getText().equalsIgnoreCase("" > )) || (jTextField1.getText().equalsIgnoreCase("" > )) || (jTextField1.getText().equalsIgnoreCase("")) ) > 140. { > 141. JOptionPane.showMessageDialog(this, > "Please fill out the details properly"); > 142. } > 143. else{ > 144. String s = new String(); > 145. s = jTextField1.getText()+","+jTextField2.getText()+"," > +jTextField3.getText(); > 146. output.writeObject(s); > 147. jTextArea1.append( > "Message is being sent and the details are as under:\n"); > 148. jTextArea1.append("Message sent to server to be send to : " > +jTextField1.getText()+"\n"); > 149. jTextArea1.append("Message subject is: "+jTextField2.getText()+ > "\n"); > 150. jTextArea1.append("Actual Message is: "+jTextField3.getText()+ > "\n"); > 151. jTextArea1.append("WAITING FOR REPLY........\n \n \n \n "); > 152. jTextArea1.setCaretPosition(jTextArea1.getText().length()); > 153. }} > 154. catch(Exception e){} > 155. > 156. > 157. } > 158. > 159. > 160. public static void main(String args[]) { > 161. java.awt.EventQueue.invokeLater(new Runnable() { > 162. public void run() { > 163. Client u = new Client(); > 164. u.setVisible(true); > 165. Thread t = new Thread(u); > 166. t.start(); > 167. } > 168. }); > 169. } > 170. ObjectOutputStream output; > 171. ObjectInputStream input; > 172. // Variables declaration - do not modify > 173. private javax.swing.JButton jButton1; > 174. private javax.swing.JButton jButton2; > 175. private javax.swing.JLabel jLabel1; > 176. private javax.swing.JLabel jLabel2; > 177. private javax.swing.JLabel jLabel3; > 178. private javax.swing.JLabel jLabel4; > 179. private javax.swing.JLabel jLabel5; > 180. private javax.swing.JScrollPane jScrollPane1; > 181. private javax.swing.JScrollPane jScrollPane2; > 182. private javax.swing.JTextArea jTextArea1; > 183. private javax.swing.JTextArea jTextArea2; > 184. private javax.swing.JTextField jTextField1; > 185. private javax.swing.JTextField jTextField2; > 186. private javax.swing.JTextField jTextField3; > 187. // End of variables declaration > 188. > 189. } > > > > > > > SERVER Code: > > > > 1. import java.net.*; > 2. import java.io.*; > 3. import java.io.ObjectInputStream; > 4. import java.io.ObjectOutputStream; > 5. > 6. > 7. public class Server extends javax.swing.JFrame implements Runnable { > 8. > 9. /** Creates new form Server */ > 10. public Server() { > 11. initComponents(); > 12. } > 13. public void run(){ > 14. ServerSocket ss; > 15. Socket s; > 16. try { > 17. > 18. ss = new ServerSocket(5100,10); > 19. while(true){ > 20. > 21. > 22. s = ss.accept(); > 23. > 24. > 25. input = new ObjectInputStream(s.getInputStream()); > 26. > 27. String str = (String) input.readObject(); > 28. int i = str.indexOf(",",0); > 29. int j = str.indexOf(",",i+1); > 30. int k = str.indexOf(",",j+1); > 31. String s1 = str.substring(0,i); > 32. String s2 = str.substring(i+1,j); > 33. String s3 = str.substring(j+1); > 34. jTextArea1.append("Message Recieved\n"); > 35. jTextArea1.append("Details are as under:\n"); > 36. jTextArea1.append("Message sent by: "+s.getInetAddress()+ > "\n"); > 37. jTextArea1.append("Message sent to: "+s1+"\n"); > 38. jTextArea1.append("Message subject is: "+s2+"\n"); > 39. jTextArea1.append("Actual Message is :"+s3+"\n"); > 40. jTextArea1.append("Sending Message from: " > +s.getInetAddress()+" to: "+s1+"\n"); > 41. > jTextArea1.setCaretPosition(jTextArea1.getText().length()); > 42. Socket another = new Socket(InetAddress.getByName(s1), > 5000); > 43. ObjectOutputStream o1 = new > ObjectOutputStream(another.getOutputStream()); > 44. String s4 = s.getInetAddress()+","+s2+","+s3; > 45. o1.writeObject(s4); > 46. jTextArea1.append("Message Send: "+s4+"\n"); > 47. > 48. s.close(); > 49. another.close(); > 50. input.close(); > 51. o1.close(); > 52. } > 53. } > 54. catch(Exception e){ > 55. e.printStackTrace(); > 56. } > 57. } > 58. > 59. /** This method is called from within the constructor to > 60. * initialize the form. > 61. > * WARNING: Do NOT modify this code. The content of this method is > 62. * always regenerated by the Form Editor. > 63. */ > 64. @SuppressWarnings("unchecked") > 65. > // <editor-fold defaultstate="collapsed" desc="Generated Code"> > > 66. private void initComponents() { > 67. > 68. jScrollPane1 = new javax.swing.JScrollPane(); > 69. jTextArea1 = new javax.swing.JTextArea(); > 70. > 71. > > setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); > 72. > 73. jTextArea1.setColumns(20); > 74. jTextArea1.setRows(5); > 75. jScrollPane1.setViewportView(jTextArea1); > 76. > 77. > getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER); > 78. > 79. pack(); > 80. }// </editor-fold> > 81. > 82. /** > 83. * @param args the command line arguments > 84. */ > 85. public static void main(String args[]) { > 86. java.awt.EventQueue.invokeLater(new Runnable() { > 87. public void run() { > 88. > 89. Server u = new Server(); > 90. u.setVisible(true); > 91. Thread t = new Thread(u); > 92. t.start(); > 93. } > 94. }); > 95. } > 96. ObjectOutputStream output; > 97. ObjectInputStream input; > 98. // Variables declaration - do not modify > 99. private javax.swing.JScrollPane jScrollPane1; > 100. private javax.swing.JTextArea jTextArea1; > 101. // End of variables declaration > 102. > 103. } > 104. > > > > I tried testing it on local machine, > i run server and then client, > after than i filled out the details in the client form, mentioning the > localhost in the to field, > > For the first time,Client sends succesfully, Server also recieves > Successfully and forwards it to the expected destination(in this case the > localmachine) Successfully, and is recieved successfully at the client(which > is reflected in the recieved message details textarea) > > But then when i do it for second time,Client sends succesfully, Server > DOESNOT recieve it, and hence do not forward it, and hence message not > received at the destination. > > THE PROBLEM is that the loop in the SERVER CODE runs only once(I don't know > why is this so, im really frustated). > Please help me pointing out where am i going wrong. > Just run the code(after making the changes you told) and tell me why is the > loop in the SERVER code runs only once(for the first time), I think that is > the CORE PROBLEM........ > PLEASE HELP! > > -- > To post to this group, send email to > [email protected] > To unsubscribe from this group, send email to > [email protected] > For more options, visit this group at > http://groups.google.com/group/javaprogrammingwithpassion?hl=en -- To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/javaprogrammingwithpassion?hl=en
