Advertisement
Guest User

Untitled

a guest
Oct 30th, 2016
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.21 KB | None | 0 0
  1. //HTTP, SMTP, POP
  2.  
  3. // HTTP
  4.  
  5. import java.io.*;
  6. import java.net.*;
  7. public class HTTP
  8. {
  9. public static void main(String[] args)
  10. {
  11. try
  12. {
  13. OutputStream to_file = new FileOutputStream("f:\\temp.txt");
  14. URL url = new
  15. URL("http://www.wickedlysmart.com/HeadFirst/HeadFirstJava/HeadFirstJavaIndex.html");
  16. String protocol = url.getProtocol();
  17. String host = url.getHost();
  18. int port = url.getPort();
  19. if(port == -1) port = 80;
  20. String filename =url.getFile();
  21. System.out.println(filename);
  22. Socket socket = new Socket(host, port);
  23. InputStream from_server = socket.getInputStream();
  24. PrintWriter to_server = new PrintWriter(socket.getOutputStream());
  25. to_server.print("Get" + filename +"\n\n");
  26. to_server.flush();
  27. byte[] buffer = new byte[4096];
  28. int byte_read;
  29. while((byte_read = from_server.read(buffer)) != -1)
  30. {
  31. to_file.write(buffer,0,byte_read);
  32. System.out.print((char)byte_read);
  33. }
  34. socket.close();
  35. to_file.close();
  36. }
  37. catch(Exception e)
  38. {
  39. e.printStackTrace();
  40. }
  41. }}
  42.  
  43. //SMTP
  44. import java.io.*;
  45. import java.net.Socket;
  46. public class SMTPClient {
  47. public static void main(String[] args) throws Exception {
  48. String results = send("localhost", 25, "sender@somewhere.com", "localhost/localdomain", "Test Email",
  49. "<b>You got mail!</b>");
  50. System.out.println(results);
  51. }
  52. public static String send(String host,int port,String from,String to,String subject,
  53. String message) throws Exception
  54. {
  55. StringBuffer buffer = new StringBuffer();
  56. try {
  57. Socket smtpSocket = new Socket(host, port);
  58. DataOutputStream output = new
  59. DataOutputStream(smtpSocket.getOutputStream());
  60. BufferedReader input =new BufferedReader(new InputStreamReader(
  61. new
  62. DataInputStream(smtpSocket.getInputStream())));
  63. try {
  64. read(input, buffer);
  65. send(output, "HELO localhost.localdomain\r\n", buffer);
  66. read(input, buffer);
  67. send(output, "MAIL FROM: " + from + "\r\n", buffer);
  68. read(input, buffer);
  69. send(output, "RCPT to: " + to + "\r\n", buffer);
  70. read(input, buffer);
  71. send(output, "DATA\r\n", buffer);
  72. read(input, buffer);
  73. send(output, "Subject: " + subject + "\r\n", buffer);
  74. send(output, message, buffer);
  75. send(output, "\r\n.\r\n", buffer);
  76. read(input, buffer);
  77. smtpSocket.close();
  78. }
  79. catch (IOException e) {
  80. System.out.println("Cannot send email as an error occurred.");
  81. }
  82. }
  83. catch (Exception e) {
  84. System.out.println("Host unknown");
  85. }
  86. return buffer.toString();
  87. }
  88. private static void send(DataOutputStream output,String data,StringBuffer buffer)
  89. throws IOException
  90. {
  91. output.writeBytes(data);
  92. buffer.append(data);
  93. }
  94. private static void read(BufferedReader br, StringBuffer buffer) throws IOException
  95. {
  96. int c;
  97. while ((c = br.read()) != -1)
  98. {
  99. buffer.append((char) c);
  100. if (c == '\n')
  101. {
  102. break;
  103. }
  104. }
  105. }
  106. }
  107.  
  108. //POP
  109. import java.io.*;
  110. import java.net.*;
  111. import java.util.*;
  112. public class Pop3ClientDemo
  113. {
  114. protected int port = 110;
  115. protected String hostname = "localhost";
  116. protected String username = "";
  117. protected String password = "";
  118. protected Socket socket;
  119. protected BufferedReader br;
  120. protected PrintWriter pw;
  121. // Constructs a new instance of the POP3 client
  122. public Pop3ClientDemo() throws Exception
  123. {
  124. try
  125. {
  126. // Get user input
  127. getInput();
  128. // Get mail messages
  129. displayEmails();
  130. }
  131. catch(Exception e)
  132. {
  133. System.err.println ("Error occured - details follow");
  134. e.printStackTrace();
  135. System.out.println(e.getMessage());
  136. }
  137. }
  138. // Returns TRUE if POP response indicates success, FALSE if failure
  139. protected boolean responseIsOk() throws Exception
  140. {
  141. String line = br.readLine();
  142. System.out.println("< "+line);
  143. return line.toUpperCase().startsWith("+OK");
  144. }
  145. // Reads a line from the POP server, and displays it to screen
  146. protected String readLine(boolean debug) throws Exception
  147. {
  148. String line = br.readLine();
  149. // Append a < character to indicate this is a server protocol response
  150. if (debug)
  151. System.out.println("< "+line);
  152. else
  153. System.out.println(line);
  154. return line;
  155. }
  156. // Writes a line to the POP server, and displays it to the screen
  157. protected void writeMsg(String msg) throws Exception
  158. {
  159. pw.println(msg);
  160. pw.flush();
  161. System.out.println("> "+msg);
  162. }
  163. // Close all writers, streams and sockets
  164. protected void closeConnection() throws Exception
  165. {
  166. pw.flush();
  167. pw.close();
  168. br.close();
  169. socket.close();
  170. }
  171. // Send the QUIT command, and close connection
  172. protected void sendQuit() throws Exception
  173. {
  174. System.out.println("Sending QUIT");
  175. writeMsg("QUIT");
  176. readLine(true);
  177. System.out.println("Closing Connection");
  178. closeConnection();
  179. }
  180. // Display emails in a message
  181. protected void displayEmails() throws Exception
  182. {
  183. BufferedReader userinput = new BufferedReader( new InputStreamReader
  184. (System.in) );
  185. System.out.println("Displaying mailbox with protocol commands and responses below");
  186. System.out.println("--------------------------------------------------------------");
  187. // Open a connection to POP3 server
  188. System.out.println("Opening Socket");
  189. socket = new Socket(this.hostname, this.port);
  190. br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  191. pw = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
  192. // If response from server is not okay
  193. if(! responseIsOk())
  194. {
  195. socket.close();
  196. throw new Exception("Invalid POP3 Server");
  197. }
  198. // Login by sending USER and PASS commands
  199. System.out.println("Sending username");
  200. writeMsg("USER "+this.username);
  201. if(!responseIsOk())
  202. {
  203. sendQuit();
  204. throw new Exception("Invalid username");
  205. }
  206. System.out.println("Sending password");
  207. writeMsg("PASS "+this.password);
  208. if(!responseIsOk())
  209. {
  210. sendQuit();
  211. throw new Exception("Invalid password");
  212. }
  213. // Get mail count from server ....
  214. System.out.println("Checking mail");
  215. writeMsg("STAT");
  216. // ... and parse for number of messages
  217. String line = readLine(true);
  218. StringTokenizer tokens = new StringTokenizer(line," ");
  219. tokens.nextToken();
  220. int messages = Integer.parseInt(tokens.nextToken());
  221. int maxsize = Integer.parseInt(tokens.nextToken());
  222. if (messages == 0)
  223. {
  224. System.out.println ("There are no messages.");
  225. sendQuit();
  226. return;
  227. }
  228. System.out.println ("There are " + messages + " messages.");
  229. System.out.println("Press enter to continue.");
  230. userinput.readLine();
  231. for(int i = 1; i <= messages ; i++)
  232. {
  233. System.out.println("Retrieving message number "+i);
  234. writeMsg("RETR "+i);
  235. System.out.println("--------------------");
  236. line = readLine(false);
  237. while(line != null && !line.equals("."))
  238. {
  239. line = readLine(false);
  240. }
  241. System.out.println("--------------------");
  242. System.out.println("Press enter to continue. To stop, type Q then enter");
  243. String response = userinput.readLine();
  244. if (response.toUpperCase().startsWith("Q"))
  245. break;
  246. }
  247. sendQuit();
  248. }
  249. public static void main(String[] args) throws Exception
  250. {
  251. Pop3ClientDemo client = new Pop3ClientDemo();
  252. }
  253. // Read user input
  254. protected void getInput() throws Exception
  255. {
  256. String data=null;
  257. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  258. System.out.print("Please enter POP3 server hostname:");
  259. data = br.readLine();
  260. if(data == null || data.equals("")) hostname="localhost";
  261. else
  262. hostname=data;
  263. System.out.print("Please enter mailbox username:");
  264. data = br.readLine();
  265. if(!(data == null || data.equals("")))
  266. username=data;
  267. System.out.print("Please enter mailbox password:");
  268. data = br.readLine();
  269. if(!(data == null || data.equals("")))
  270. password=data;
  271. }
  272. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement