Advertisement
Guest User

Untitled

a guest
Oct 18th, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 KB | None | 0 0
  1. /*************************************
  2. * Filename: SMTPConnect.java
  3. * Names: Thomas Patterson
  4. * Student-IDs: 201263503
  5. * Date: 12/10/2018
  6. *************************************/
  7. import java.net.*;
  8. import java.io.*;
  9. import java.security.cert.CRL;
  10. import java.util.*;
  11.  
  12. /**
  13. * Open an SMTP connection to mailserver and send one mail.
  14. *
  15. */
  16. public class SMTPConnect {
  17. /* The socket to the server */
  18. private Socket connection;
  19.  
  20. /* Streams for reading from and writing to socket */
  21. private BufferedReader fromServer;
  22. private DataOutputStream toServer;
  23.  
  24. private static final String CRLF = "\r\n";
  25.  
  26. /* Are we connected? Used in close() to determine what to do. */
  27. private boolean isConnected = false;
  28.  
  29. /* Create an SMTPConnect object. Create the socket and the
  30. associated streams. Initialise SMTP connection. */
  31. public SMTPConnect(EmailMessage mailmessage) throws IOException {
  32. // Open a TCP client socket with hostname and portnumber specified in
  33. // mailmessage.DestHost and mailmessage.DestHostPort, respectively.
  34. connection = new Socket(mailmessage.DestHost, mailmessage.DestHostPort);
  35.  
  36. // attach the BufferedReader fromServer to read from the socket and
  37. // the DataOutputStream toServer to write to the socket
  38. fromServer = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  39. toServer = new DataOutputStream(connection.getOutputStream);
  40.  
  41. /* Fill in */
  42. String response = fromServer.readLine();
  43. System.out.println(response);
  44. if (!response.startsWith("220")) {
  45. throw new Exception("220 reply not received from server.");
  46. }
  47. /* Read one line from server and check that the reply code is 220.
  48. If not, throw an IOException. */
  49. /* Fill in */
  50.  
  51. /* SMTP handshake. We need the name of the local machine.
  52. Send the appropriate SMTP handshake command. */
  53. String localhost = InetAddress.getLocalHost().getHostName();
  54. sendCommand("HELO"+ localhost + CRLF + 250);
  55. isConnected = true;
  56. }
  57.  
  58. /* Send message. Write the correct SMTP-commands in the
  59. correct order. No checking for errors, just throw them to the
  60. caller. */
  61. public void send(EmailMessage mailmessage) throws IOException {
  62. /* Fill in */
  63. /* Send all the necessary commands to send a message. Call
  64. sendCommand() to do the dirty work. Do _not_ catch the
  65. exception thrown from sendCommand(). */
  66. sendCommand("MAIL FROM: "+mailmessage.Sender+CRLF, 250);
  67. sendCommand("RCPT TO: " + mailmessage.Recipient+CRLF, 250);
  68. sendCommand("DATA" +mailmessage.Headers + CRLF + mailmessage.Body +CRLF + "." + CRLF, 354);
  69.  
  70. /* Fill in */
  71. }
  72.  
  73. /* Close SMTP connection. First, terminate on SMTP level, then
  74. close the socket. */
  75. public void close() {
  76. isConnected = false;
  77. try {
  78. sendCommand("QUIT" + CRLF, 221);
  79. connection.close();
  80. } catch (IOException e) {
  81. System.out.println("Unable to close connection: " + e);
  82. isConnected = true;
  83. }
  84. }
  85.  
  86. /* Send an SMTP command to the server. Check that the reply code is
  87. what is is supposed to be according to RFC 821. */
  88. private void sendCommand(String command, int rc) throws IOException {
  89. /* Fill in */
  90. System.out.print(command);
  91. toServer.writeBytes(command);
  92. String response = fromServer.readline();
  93. System.out.println(response);
  94.  
  95. /* Fill in */
  96.  
  97. /* Fill in */
  98. if (!response.startsWith(Integer.toString(rc))) {
  99. throw new Exception(rc + " reply not received from server.");
  100. }
  101. /* Fill in */
  102. }
  103.  
  104. /* Destructor. Closes the connection if something bad happens. */
  105. protected void finalize() throws Throwable {
  106. if(isConnected) {
  107. close();
  108. }
  109. super.finalize();
  110. }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement