Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Skeleton Application Code for the Send Mail App
- *
- * Prof. A.C. Silvestri
- * CSCI-211 Intermediate Java Programming
- * 4/20/2015
- *
- * Omar Natour
- * 4/28/16
- * Csc-112 Java 2
- * Exam 3
- * Create an email client
- * Ojnatour0001@student.stcc.edu
- */
- import javafx.application.Application;
- import javafx.geometry.Insets;
- import javafx.geometry.Pos;
- import javafx.scene.Scene;
- import javafx.scene.control.Button;
- import javafx.scene.control.Label;
- import javafx.scene.control.TextArea;
- import javafx.scene.control.TextField;
- import javafx.scene.layout.BorderPane;
- import javafx.scene.layout.FlowPane;
- import javafx.stage.Stage;
- import java.net.*;
- import java.util.Scanner;
- import java.io.*;
- import javax.management.RuntimeErrorException;
- public class SendMailApp extends Application {
- private TextField txtTo;
- private TextField txtFrom;
- private TextField txtSubject;
- private TextArea txtBody;
- private TextField txtStatus;
- private Button btnSend;
- @Override
- // Override the start method in the Application class
- public void start(Stage primaryStage) {
- txtTo = new TextField();
- txtTo.setPrefColumnCount(25);
- txtFrom = new TextField();
- txtFrom.setPrefColumnCount(25);
- txtSubject = new TextField();
- txtSubject.setPrefColumnCount(25);
- txtBody = new TextArea();
- txtBody.setPrefColumnCount(28);
- txtBody.setPrefRowCount(10);
- btnSend = new Button("Send Email");
- txtStatus = new TextField();
- txtStatus.setPrefColumnCount(25);
- txtStatus.setEditable(false);
- Label lblTo = new Label("Mail To:");
- lblTo.setPrefWidth(90);
- Label lblFrom = new Label("Mail From:");
- lblFrom.setPrefWidth(90);
- Label lblSubject = new Label("Subject:");
- lblSubject.setPrefWidth(90);
- FlowPane tpane = new FlowPane(5, 5);
- tpane.setAlignment(Pos.BASELINE_LEFT);
- tpane.setPadding(new Insets(10, 5, 5, 5));
- tpane.setStyle("-fx-font-weight: bold; -fx-font-size: 12pt");
- tpane.getChildren().addAll(lblTo, txtTo, lblFrom, txtFrom, lblSubject, txtSubject);
- FlowPane cpane = new FlowPane(5, 5);
- cpane.setAlignment(Pos.CENTER);
- cpane.setPadding(new Insets(5, 5, 5, 5));
- cpane.setStyle("-fx-font-weight: bold; -fx-font-size: 12pt");
- cpane.getChildren().addAll(new Label("Enter your message below:"), txtBody, btnSend);
- Label lblStatus = new Label("Status:");
- lblStatus.setPrefWidth(90);
- FlowPane bpane = new FlowPane(5, 5);
- bpane.setAlignment(Pos.BASELINE_LEFT);
- bpane.setPadding(new Insets(5, 5, 10, 5));
- bpane.setStyle("-fx-font-weight: bold; -fx-font-size: 12pt");
- bpane.getChildren().addAll(lblStatus, txtStatus);
- BorderPane bp = new BorderPane();
- bp.setTop(tpane);
- bp.setCenter(cpane);
- bp.setBottom(bpane);
- btnSend.setOnAction(e -> {
- run();
- });
- Scene scene = new Scene(bp, 525, 520);
- primaryStage.setTitle("Send Mail Application");
- primaryStage.setScene(scene);
- primaryStage.show();
- primaryStage.setResizable(false);
- }
- private void run() {
- Socket client = null;
- PrintWriter output = null;
- Scanner input = null;
- try{
- //create the connection
- client = new Socket("cs.stcc.edu", 25);
- input = new Scanner(client.getInputStream());
- output = new PrintWriter(client.getOutputStream());
- //greet the server
- output.println("Helo cs.stcc.edu");
- output.flush();
- input.nextLine();
- //meet the server
- int err = input.nextInt();
- txtStatus.setText(err + input.nextLine());
- check(err);
- //give the server the sending address
- String from = txtFrom.getText();
- output.println("MAIL FROM:" + from);
- output.flush();
- err = input.nextInt();
- txtStatus.setText(err + " Error getting sender");
- input.nextLine();
- check(err);
- //give the server the receiving address
- String to = txtTo.getText();
- output.println("RCPT TO:" + to);
- output.flush();
- err = input.nextInt();
- txtStatus.setText(err + input.nextLine());
- check(err);
- //telling the server were ready to send it data
- output.println("DATA");
- output.flush();
- err = input.nextInt();
- txtStatus.setText(err + input.nextLine());
- check(err);
- //sending the server the data
- String message = txtBody.getText();
- String subject = txtSubject.getText();
- String data = ("Subject:" + subject + "\n" + "To:" + to + "\n" + "From:" + from
- + "\n\n" + message + "\n" + ".");
- output.println(data);
- output.flush();
- err = input.nextInt();
- txtStatus.setText(err + input.nextLine());
- check(err);
- txtStatus.setText("Message Sent Successfully");
- output.println("QUIT");
- output.flush();
- }catch(RuntimeErrorException rt){
- output.println("QUIT");
- output.flush();
- }
- catch(Exception e){
- System.out.print(e);
- }
- finally{
- output.close();
- input.close();
- try {
- client.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- private void check(int err){
- if(err > 400)
- throw new RuntimeErrorException(null);
- }
- public static void main(String[] args) {
- launch(args);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement