Advertisement
Omar_Natour

Natour, O. 4/28/16 Csc-112 Exam 3

Apr 28th, 2016
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.17 KB | None | 0 0
  1. /*
  2.  *  Skeleton Application Code for the Send Mail App
  3.  *
  4.  *  Prof. A.C. Silvestri
  5.  *  CSCI-211 Intermediate Java Programming
  6.  *  4/20/2015
  7.  *
  8.  *  Omar Natour
  9.  *  4/28/16
  10.  *  Csc-112 Java 2
  11.  *  Exam 3
  12.  *  Create an email client
  13.  *  Ojnatour0001@student.stcc.edu
  14.  */
  15.  
  16. import javafx.application.Application;
  17. import javafx.geometry.Insets;
  18. import javafx.geometry.Pos;
  19. import javafx.scene.Scene;
  20. import javafx.scene.control.Button;
  21. import javafx.scene.control.Label;
  22. import javafx.scene.control.TextArea;
  23. import javafx.scene.control.TextField;
  24. import javafx.scene.layout.BorderPane;
  25. import javafx.scene.layout.FlowPane;
  26. import javafx.stage.Stage;
  27.  
  28. import java.net.*;
  29. import java.util.Scanner;
  30. import java.io.*;
  31.  
  32. import javax.management.RuntimeErrorException;
  33.  
  34. public class SendMailApp extends Application {
  35.         private TextField txtTo;
  36.         private TextField txtFrom;
  37.         private TextField txtSubject;
  38.         private TextArea  txtBody;
  39.         private TextField txtStatus;
  40.         private Button btnSend;
  41.  
  42.         @Override
  43.         // Override the start method in the Application class
  44.         public void start(Stage primaryStage) {
  45.                 txtTo = new TextField();
  46.                 txtTo.setPrefColumnCount(25);
  47.                 txtFrom = new TextField();
  48.                 txtFrom.setPrefColumnCount(25);
  49.                 txtSubject = new TextField();
  50.                 txtSubject.setPrefColumnCount(25);
  51.                 txtBody = new TextArea();
  52.                 txtBody.setPrefColumnCount(28);
  53.                 txtBody.setPrefRowCount(10);
  54.                 btnSend = new Button("Send Email");
  55.                 txtStatus = new TextField();
  56.                 txtStatus.setPrefColumnCount(25);
  57.                 txtStatus.setEditable(false);
  58.  
  59.                 Label lblTo = new Label("Mail To:");
  60.                 lblTo.setPrefWidth(90);
  61.                 Label lblFrom = new Label("Mail From:");
  62.                 lblFrom.setPrefWidth(90);
  63.                 Label lblSubject = new Label("Subject:");
  64.                 lblSubject.setPrefWidth(90);
  65.  
  66.                 FlowPane tpane = new FlowPane(5, 5);
  67.                 tpane.setAlignment(Pos.BASELINE_LEFT);
  68.                 tpane.setPadding(new Insets(10, 5, 5, 5));
  69.                 tpane.setStyle("-fx-font-weight: bold; -fx-font-size: 12pt");
  70.                 tpane.getChildren().addAll(lblTo, txtTo, lblFrom, txtFrom, lblSubject, txtSubject);
  71.  
  72.                 FlowPane cpane = new FlowPane(5, 5);
  73.                 cpane.setAlignment(Pos.CENTER);
  74.                 cpane.setPadding(new Insets(5, 5, 5, 5));
  75.                 cpane.setStyle("-fx-font-weight: bold; -fx-font-size: 12pt");
  76.                 cpane.getChildren().addAll(new Label("Enter your message below:"), txtBody, btnSend);
  77.  
  78.                 Label lblStatus = new Label("Status:");
  79.                 lblStatus.setPrefWidth(90);
  80.                 FlowPane bpane = new FlowPane(5, 5);
  81.                 bpane.setAlignment(Pos.BASELINE_LEFT);
  82.                 bpane.setPadding(new Insets(5, 5, 10, 5));
  83.                 bpane.setStyle("-fx-font-weight: bold; -fx-font-size: 12pt");
  84.                 bpane.getChildren().addAll(lblStatus, txtStatus);
  85.  
  86.                 BorderPane bp = new BorderPane();
  87.                 bp.setTop(tpane);
  88.                 bp.setCenter(cpane);
  89.                 bp.setBottom(bpane);
  90.  
  91.                 btnSend.setOnAction(e -> {
  92.                         run();
  93.  
  94.                 });
  95.  
  96.                 Scene scene = new Scene(bp, 525, 520);
  97.                 primaryStage.setTitle("Send Mail Application");
  98.                 primaryStage.setScene(scene);
  99.                 primaryStage.show();
  100.                 primaryStage.setResizable(false);
  101.         }
  102.  
  103.         private void run() {
  104.                 Socket client = null;
  105.                 PrintWriter output = null;
  106.                 Scanner input = null;
  107.  
  108.                 try{
  109.  
  110.                         //create the connection
  111.                         client = new Socket("cs.stcc.edu", 25);
  112.  
  113.                         input = new Scanner(client.getInputStream());
  114.                         output = new PrintWriter(client.getOutputStream());
  115.  
  116.                         //greet the server
  117.                         output.println("Helo cs.stcc.edu");
  118.                         output.flush();
  119.                         input.nextLine();
  120.  
  121.                         //meet the server
  122.                         int err = input.nextInt();
  123.                         txtStatus.setText(err + input.nextLine());
  124.                         check(err);
  125.  
  126.                         //give the server the sending address
  127.                         String from = txtFrom.getText();
  128.                         output.println("MAIL FROM:" + from);
  129.                         output.flush();
  130.                         err = input.nextInt();
  131.                         txtStatus.setText(err + " Error getting sender");
  132.                         input.nextLine();
  133.                         check(err);
  134.  
  135.                         //give the server the receiving address
  136.                         String to = txtTo.getText();
  137.                         output.println("RCPT TO:" + to);
  138.                         output.flush();
  139.                         err = input.nextInt();
  140.                         txtStatus.setText(err + input.nextLine());
  141.                         check(err);
  142.  
  143.                         //telling the server were ready to send it data
  144.                         output.println("DATA");
  145.                         output.flush();
  146.                         err = input.nextInt();
  147.                         txtStatus.setText(err + input.nextLine());
  148.                         check(err);
  149.  
  150.                         //sending the server the data
  151.                         String message = txtBody.getText();
  152.                         String subject = txtSubject.getText();
  153.                         String data = ("Subject:" + subject + "\n" + "To:" + to + "\n" + "From:" + from
  154.                                         + "\n\n" + message + "\n" + ".");
  155.                         output.println(data);
  156.                         output.flush();
  157.                         err = input.nextInt();
  158.                         txtStatus.setText(err + input.nextLine());
  159.                         check(err);
  160.  
  161.                         txtStatus.setText("Message Sent Successfully");
  162.  
  163.                         output.println("QUIT");
  164.                         output.flush();
  165.  
  166.                 }catch(RuntimeErrorException rt){
  167.                         output.println("QUIT");
  168.                         output.flush();
  169.                 }
  170.                 catch(Exception e){
  171.                         System.out.print(e);
  172.                 }
  173.                 finally{
  174.                         output.close();
  175.                         input.close();
  176.                         try {
  177.                                 client.close();
  178.                         } catch (IOException e) {
  179.                                 e.printStackTrace();
  180.                         }
  181.                 }
  182.         }
  183.  
  184.         private void check(int err){
  185.                 if(err > 400)
  186.                         throw new RuntimeErrorException(null);
  187.         }
  188.  
  189.         public static void main(String[] args) {
  190.                 launch(args);
  191.         }
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement