Omar_Natour

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

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