9
\$\begingroup\$

I've just finished up a functional emailing client in the Java language. It applies the JavaFX libraries and the JavaMail/activation APIs.

Give the source a read through and make all the suggestions you think of, please. I'm in the process now of cleaning it up since it was just finished, but address any discrepancies or semantic/syntactic aspects in as much depth as you desire. I'd love to learn more about both of these amazing Java tools (JavaMail API and JavaFX).

Here is the link to download the .zip folder containing that runnable .jar program. Feel free to also use this extremely lightweight mailer at your leisure. I would feel greatly honored!!!

Edit: If you'd like to email me (using my app, if you feel like making me happy) with any information, you can reach me at any of these addresses:

philecarpenter@gmail.com/@tempestdesign.org
admin@tempestdesign.org    
jmapitest@gmail.com /** made exclusively for testing this service! :) */

Note for GMAIL users, you must have your access for less secure apps turned on.

Currently only supports GMAIL's smtp and YAHOO!'s smtps servers.

Here is the source, so unkempt as it is:

package org.tempestdesign.sendclient;

import java.util.Date;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.collections.*;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
//import javafx.event.ActionEvent;
//import javafx.event.EventHandler;
//import javafx.stage.WindowEvent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment;
//import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class Client extends Application  /*implements EventHandler<ActionEvent>*/ {

    static Session sesh;
    static Properties prop = new Properties();

    static Button btnLOG, btnSEND, btnBACK;
    static Label lblUN, lblPW, lblTO, lblSUB, lblTEXT, lblHEAD, lblHOST, lblTYPE;
    static Scene log, edit, sent;
    static Text UNfail, tSUC, tSENT;
    static TextField tUN, tto, tsub, thead;
    static TextArea ttext;
    static PasswordField tPW;
    static ComboBox<String> cmbHOST, cmbTYPE;
    static String UN, PW, host, port, mto, mhead, msub, cTEXT, cTYPE;
    static GridPane layLOG, layEDIT;
    static VBox laySENT;
    static Stage win;

    public static void main(String args[]) {
        launch(args);
    }

    //@SuppressWarnings("unused")
    @Override
    public void start(Stage primaryStage) throws Exception {
        win = primaryStage;

        win.setOnCloseRequest(e -> Platform.exit());
        win.setTitle("SendMail \u0020\u0020 | | \u0020\u0020 TDS");
        win.setResizable(false);

        //

        layLOG = new GridPane();
        layLOG.setAlignment(Pos.CENTER);
        layLOG.setHgap(30);
        layLOG.setVgap(15);
        layLOG.setPadding(new Insets(20, 10, 20, 10));
        layLOG.setStyle("-fx-font-size: 13px; -fx-background-color: linear-gradient(#ff2, #e5bb00), linear-gradient(#e43, #f84);"
                    + "-fx-background-radius: 2, 29;");
        //
        layEDIT = new GridPane();
        layEDIT.setAlignment(Pos.CENTER_LEFT);
        layEDIT.setHgap(10);
        layEDIT.setVgap(25);
        layEDIT.setPadding(new Insets(10, 15, 20, 10));
        layEDIT.setStyle("-fx-font-size: 13px; -fx-background-color: linear-gradient(#ff2, #e5bb00), linear-gradient(#f53, #f86);"
                + "-fx-background-radius: 2, 29;");
        //
        laySENT = new VBox(21);
        laySENT.setAlignment(Pos.CENTER);
        laySENT.setStyle("-fx-background-color: linear-gradient(#ff2, #e5bb00), linear-gradient(#f53, #f86);"
                + "-fx-background-radius: 2, 29;");
        //
        // layLOG layout //
        ObservableList<String> hostOps = 
                FXCollections.observableArrayList(
                    "smtp.gmail.com",
                    "smtp.mail.yahoo.com"
                );
        cmbHOST = new ComboBox<String>(hostOps);
        cmbHOST.setValue(" ");
        layLOG.add(cmbHOST, 1, 0);
        //
        lblHOST = new Label("SMTP Server: ");
        layLOG.add(lblHOST, 0, 0);
        lblUN = new Label("Username/email: ");
        layLOG.add(lblUN, 0, 1);
        lblPW = new Label("Password: ");
        layLOG.add(lblPW, 0, 2);
        //
        tUN = new TextField();
        tPW = new PasswordField();
        layLOG.add(tUN, 1, 1);
        layLOG.add(tPW, 1, 2);
        UNfail = new Text("Cannot authenticate");
        //
        btnLOG = new Button();
        btnLOG.setText("Verify");
        btnLOG.setOnAction(e -> {
            UN = tUN.getText();
            PW = tPW.getText();
            host = cmbHOST.getValue();
            if (host.equals("smtp.gmail.com")) {
                port = "587";
            } else if (host.equals("smtp.mail.yahoo.com")) {
                port = "465";
            } else {
                port = "80";
            }
            if(layLOG.getChildren().contains(UNfail)) {
                System.out.print("y");
                layLOG.getChildren().remove(UNfail);
            }
            auth();
        });
        layLOG.add(btnLOG, 1, 3);
        //
        // layEDIT layout //
        ObservableList<String> typeOps = FXCollections.observableArrayList(
                "text/plain",
                "text/html"
                );
        cmbTYPE = new ComboBox<String>(typeOps);
        lblTO = new Label("To: ");
        lblHEAD = new Label("Heading: ");
        lblSUB = new Label("Message subject: ");
        lblTEXT = new Label(" \u0020 \u0020 \u0020\t # # # # # \t > \t # \t Body \t # \t < \t # # # # #");
        lblTYPE = new Label("Message type: ");
        tto = new TextField();
        thead = new TextField();
        tsub = new TextField();
        ttext = new TextArea();
        tto.setPrefWidth(212);
        thead.setPrefWidth(212);
        tsub.setPrefWidth(212);
        ttext.setPrefSize(380, 510);
        ttext.setWrapText(true);
        layEDIT.add(lblTO, 0, 1);
        layEDIT.add(lblHEAD, 0, 2);
        layEDIT.add(lblSUB, 0, 3);
        layEDIT.add(lblTEXT, 0, 4);
        layEDIT.add(tto, 1, 1);
        layEDIT.add(thead, 1, 2);
        layEDIT.add(tsub, 1, 3);
        layEDIT.add(ttext, 0, 5);
        layEDIT.add(lblTYPE, 0, 0);
        layEDIT.add(cmbTYPE, 1, 0);
        cmbTYPE.setValue("text/plain");
        cmbTYPE.setPrefWidth(212);
        //
//  ##  //  ##  //
    //  ##  //  ##  //
        //  ##  //  ##  //
        //
        btnSEND = new Button();
        btnSEND.setText("Send eMail");
        btnSEND.setOnAction(e -> {
            cTYPE = cmbTYPE.getValue();
            mto = tto.getText();
            mhead = thead.getText();
            msub = tsub.getText();
            cTEXT = ttext.getText();
            Mail(mto, msub, cTEXT);
            if(!mto.isEmpty() || !cTEXT.isEmpty() || !msub.isEmpty()){
                win.setScene(sent);
            }
        });
        VBox vb = new VBox();
        vb.setAlignment(Pos.BOTTOM_CENTER);
        vb.getChildren().add(btnSEND);
        layEDIT.add(vb, 1, 5);

        //
        // laySENT layout //
        tSUC = new Text("Message mailed to recipient(s).");
        tSENT = new Text("Don't forget to check your inbox regarding \n send errors and replies.");
        tSUC.setStyle("-fx-font-size: 32px;");
        tSUC.setTextAlignment(TextAlignment.CENTER);
        tSUC.setFill(Color.web("#2b3"));
        tSENT.setStyle("-fx-font-size: 10px;");
        tSENT.setFill(Color.web("#676767"));
        tSENT.setTextAlignment(TextAlignment.CENTER);

        tSUC.setLayoutX(80);

        btnBACK = new Button();
        btnBACK.setText("Return to Editor");
        btnBACK.setOnAction(e -> {
            win.setScene(edit);
            tto.setText(mto);
            ttext.setText(cTEXT);
            tsub.setText(msub);
            thead.setText(mhead);
        });

        laySENT.getChildren().addAll(tSUC, tSENT, btnBACK);

        //
        // Scenes and Stages //
        //

        edit = new Scene(layEDIT, 640, 710);
        log = new Scene(layLOG, 480, 210);
        sent = new Scene(laySENT, 500, 260);
        //
        win.setX(150);
        win.setY(200);
        win.setScene(log);
        win.show();

    }

    public static void auth() {
        boolean auth = chk(UN, PW);
        if(!auth) {
            System.out.print("Not auth");
            layLOG.add(UNfail, 3, 1);
            tUN.clear();
            tPW.clear();
        } else if (auth) {
            System.out.print("Auth");
            win.setScene(edit);
        } else {
            System.out.print("Not auth");
            layLOG.add(UNfail, 3, 1);
            cmbHOST.setValue(" ");
            tUN.clear();
            tPW.clear();
        }



    } // end auth() //

    public static boolean chk(String UN, String PW) /*throws AuthenticationFailedException /*AddressException*/ {

        prop.put("mail.smtp.auth", "true");
        if(host.equals("smtp.gmail.com") || host.equals("smtp.mail.yahoo.com")){
            prop.put("mail.smtp.starttls.enable", "true");
        }
        prop.put("mail.smtp.host", host/*[0]*/);
        prop.put("mail.smtp.port", port);
        if(host.equals("smtp.mail.yahoo.com")) { prop.put("mail.smtp.ssl.enable", "true"); }

        boolean check = true;
        //
        try {
            InternetAddress e = new InternetAddress(UN);
            e.validate();
        } catch (AddressException e) {
            e.getStackTrace();
            check = false;
        }

        if(check) {
            sesh = Session.getInstance(prop,
                new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(UN, PW);
                    }
                });
        }

        return check;
    } // end chk() //

    public static void Mail(String to, String sub, String cont) /*throws IOException*/ {

        try {

            System.out.println("\n \n>> ?" + mto);
            System.out.println("\n \n>> ?" + to);
            Message m = new MimeMessage(sesh);
            m.setFrom(new InternetAddress(UN));
            m.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
            m.setSubject(sub);
            m.setSentDate(new Date());
            m.setContent(cont, cTYPE);
            m.setHeader("EMAIL HEAD", mhead);
            System.out.println("\n \n \n \t >> ??????? " + m.getContentType());
            System.out.println("\n \n \n \t >> ??????? " + m.getDataHandler());
            System.out.println("\n \n \n \t >> ??????? " + m.getSubject());

            Transport t;
            if(host.equals("smtp.mail.yahoo.com"))
                t = sesh.getTransport("smtps");
            else 
                t = sesh.getTransport("smtp");
             //
            System.out.println(">> ? smtp(s) ---> ## " + t.getURLName() + " \n>> ?");

            Transport.send(m);  


        } catch (MessagingException e) {
            throw new RuntimeException(e);
        } // end try/catch //

        // });

        } // end Mail() //

} // end Send Client program //

I look forward to any comments, concerns, suggestions, criticisms, and advice.

\$\endgroup\$
  • 2
    \$\begingroup\$ Is all that static data a JavaFX requirement? If not, that's an awful lot of visible mutable state that could be more strongly encapsulated. If it's an option, I'd make it no static for starters.., \$\endgroup\$ – NietzscheanAI Dec 15 '15 at 8:15
  • \$\begingroup\$ Very true. I ended up using most of my variables in a lot of places throughout the source, so making the globally static helped a lot for data-handling and such. I'm refactoring it a lot tomorrow, and finishing up adding a reading scene. I will update it as necessary. Thank you. \$\endgroup\$ – Phil C. Dec 15 '15 at 8:50
2
\$\begingroup\$

First of all, pretty much every single comment in your code is pointless. Any code that you've commented out should generally be removed, unless it is commented out because of a bug, in which case you should probably also have a comment about that (and if you have it as a GH repo or something log an issue and reference the issue number in the comment).

Comments such as // end try/catch // are pure noise - developers can read, and considering that you have pretty good indentation and none of the sections where you do that are obnoxiously long or busy enough to be hard to (visually) parse, they're pretty useless. Note the comments above - I'd say that fixing all of those things (if it is unclear where something like a try/catch ends) is a much better solution than a comment to that effect.

Also, you should be separating your concerns and putting the UI stuff into an fxml file. I've done that here, as well as made it quite a bit more modular, although I haven't removed all of the static global nastiness. I didn't make a ton of other edits to the code besides the fxml and modular transitions.

As a warning, not quite all of this is done (I ran out of time) and I wasn't able to test it too much (I don't have the Mail API and don't plan on getting it) however it should be able to get you on the right track.

If I get a chance when I get home, where I do have the Mail API installed, I'll see if I can finish it up.

Client.java

package org.tempestdesign.sendclient;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

public class Client extends Application   {

    static Session sesh;
    static String host;

    public static void main(String args[]) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        FXMLLoader logLoader = new FXMLLoader(getClass().getResource("laylog.fxml"));

        Pane llPane = logLoader.load();

        LayLogController llController = logLoader.getController();

        llController.setPrevStage(primaryStage);

        primaryStage.setOnCloseRequest(e -> Platform.exit());
        primaryStage.setTitle("SendMail \u0020\u0020 | | \u0020\u0020 TDS");
        primaryStage.setResizable(false);

        primaryStage.setX(150);
        primaryStage.setY(200);
        primaryStage.setScene(new Scene(llPane, 480, 210));
        primaryStage.show();
    }

}

LayEditController.java

package org.tempestdesign.sendclient;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;

import java.io.IOException;
import java.util.Date;

public class LayEditController extends TransitionController {

    @FXML private ComboBox<String> cmbTYPE;
    @FXML protected TextField tto;
    @FXML protected TextField thead;
    @FXML protected TextField tsub;
    @FXML protected TextArea ttext;

    private String mto, mhead, msub, cTYPE, cTEXT;

    @FXML
    protected void handleSendButton(ActionEvent e) throws IOException {
        cTYPE = cmbTYPE.getValue();
        mto = tto.getText();
        mhead = thead.getText();
        msub = tsub.getText();
        cTEXT = ttext.getText();
        Mail(mto, msub, cTEXT);
        if(!mto.isEmpty() || !cTEXT.isEmpty() || !msub.isEmpty()){
            transitionScene("Sent Email", "laysent.fxml", 500, 260);
        }
    }
    public void Mail(String to, String sub, String cont) {
        try {

            System.out.println("\n \n>> ?" + mto);
            System.out.println("\n \n>> ?" + to);
            Message m = new MimeMessage(sesh);
            m.setFrom(new InternetAddress(UN));
            m.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
            m.setSubject(sub);
            m.setSentDate(new Date());
            m.setContent(cont, cTYPE);
            m.setHeader("EMAIL HEAD", mhead);
            System.out.println("\n \n \n \t >> ??????? " + m.getContentType());
            System.out.println("\n \n \n \t >> ??????? " + m.getDataHandler());
            System.out.println("\n \n \n \t >> ??????? " + m.getSubject());

            Transport t;
            if(Client.host.equals("smtp.mail.yahoo.com"))
                t = Client.sesh.getTransport("smtps");
            else
                t = Client.sesh.getTransport("smtp");
            //
            System.out.println(">> ? smtp(s) ---> ## " + t.getURLName() + " \n>> ?");

            Transport.send(m);


        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }

    }
}

layedit.fxml

<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextArea?>
<?import javafx.collections.FXCollections?>
<?import java.lang.String?>

<?scenebuilder-stylesheet sendclient.css?>
<GridPane alignment="center_left" hgap="10" vgap="25"
          fx:id="layEDIT" styleClass="lay-edit"
          fx:controller="org.tempestdesign.sendclient.LayEditController"
          xmlns:fx="http://javafx.com/fxml">
    <padding><Insets top="10" right="15" bottom="20" left="10"/></padding>
    <ComboBox fx:id="cmbTYPE" value="text/plain" prefWidth="212"
              GridPane.columnIndex="0" GridPane.rowIndex="1">
        <items>
            <FXCollections fx:factory="observableArrayList">
                <String fx:value="text/plain"/>
                <String fx:value="text/html"/>
            </FXCollections>
        </items>
    </ComboBox>
    <Label text="Message type: " GridPane.columnIndex="0" GridPane.rowIndex="0"/>
    <Label text="To: " GridPane.columnIndex="0" GridPane.rowIndex="1"/>
    <TextField fx:id="tto" prefWidth="212" GridPane.columnIndex="1" GridPane.rowIndex="1"/>
    <Label text="Heading: " GridPane.columnIndex="0" GridPane.rowIndex="2"/>
    <TextField fx:id="thead" prefWidth="212" GridPane.columnIndex="1" GridPane.rowIndex="2"/>
    <Label text="Message subject: " GridPane.columnIndex="0" GridPane.rowIndex="3"/>
    <TextField fx:id="tsub" prefWidth="212" GridPane.columnIndex="1" GridPane.rowIndex="3"/>
    <Label text="\u0020 \u0020 \u0020\t # # # # # \t > \t # \t Body \t # \t < \t # # # # #"
           GridPane.columnIndex="0" GridPane.rowIndex="4"/>
    <TextArea fx:id="ttext" prefWidth="380" prefHeight="510" wrapText="true" GridPane.columnIndex="1" GridPane.rowIndex="4"/>
    <VBox fx:id="vb" alignment="bottom_center" GridPane.columnIndex="1" GridPane.rowIndex="5">
        <Button fx:id="btnSEND" onAction="#handleSendButton" text="Send eMail"/>
    </VBox>
</GridPane>

LayLogController.java

package org.tempestdesign.sendclient;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Text;
import javafx.stage.Stage;

import java.io.IOException;
import java.util.Properties;

public class LayLogController extends TransitionController {

    private final static String GMAIL_HOST = "smtp.gmail.com";
    private final static String GMAIL_PORT = "587";
    private final static String YAHOO_HOST = "smtp.mail.yahoo.com";
    private final static String YAHOO_PORT = "465";
    private final static String DEFAULT_PORT = "80";

    @FXML private GridPane layLOG;
    @FXML private ComboBox<String> cmbHOST;
    @FXML private TextField tUN, tPW;
    @FXML private static final Text UNfail = new Text("Cannot authenticate");

    static String UN, PW, port;
    static Properties prop = new Properties();

    private Stage prevStage;

    public void setPrevStage(Stage s) { this.prevStage = s; }

    @FXML
    protected void handleVerifyButton(ActionEvent e) throws IOException{
        UN = tUN.getText();
        PW = tPW.getText();
        switch (Client.host = cmbHOST.getValue()) {
            case GMAIL_HOST:
                port = GMAIL_PORT;
                break;
            case YAHOO_HOST:
                port = YAHOO_PORT;
                break;
            default:
                port = DEFAULT_PORT;
        }
        if(layLOG.getChildren().contains(UNfail)) {
            System.out.print("y");
            layLOG.getChildren().remove(UNfail);
        }
        auth();
    }

    private void auth() throws IOException{
        boolean auth = chk(UN, PW);
        if(!auth) {
            System.out.print("Not auth");
            layLOG.add(UNfail, 3, 1);
            tUN.clear();
            tPW.clear();
        } else if (auth) {
            System.out.print("Auth");
            transitionScene("Edit Email", "layedit.fxml", 640, 710);
        } else {
            System.out.print("Not auth");
            layLOG.add(UNfail, 3, 1);
            cmbHOST.setValue(" ");
            tUN.clear();
            tPW.clear();
        }
    }

    private boolean chk(String UN, String PW) {

        prop.put("mail.smtp.auth", "true");
        if(Client.host.equals("smtp.gmail.com") || Client.host.equals("smtp.mail.yahoo.com")){
            prop.put("mail.smtp.starttls.enable", "true");
        }
        prop.put("mail.smtp.host", Client.host);
        prop.put("mail.smtp.port", port);
        if(Client.host.equals("smtp.mail.yahoo.com")) { prop.put("mail.smtp.ssl.enable", "true"); }

        boolean check = true;
        //
        try {
            InternetAddress e = new InternetAddress(UN);
            e.validate();
        } catch (AddressException e) {
            e.getStackTrace();
            check = false;
        }

        if(check) {
            Client.sesh = Session.getInstance(prop,
                    new javax.mail.Authenticator() {
                        protected PasswordAuthentication getPasswordAuthentication() {
                            return new PasswordAuthentication(UN, PW);
                        }
                    });
        }

        return check;
    }
}

laylog.fxml

<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.control.Button?>
<?import javafx.collections.FXCollections?>
<?import java.lang.String?>

<?scenebuilder-stylesheet sendclient.css?>
<GridPane alignment="center" hgap="30" vgap="15"
          fx:id="layLOG" styleClass="lay-log"
          fx:controller="org.tempestdesign.sendclient.LayLogController"
          xmlns:fx="http://javafx.com/fxml">
    <padding><Insets top="10" right="15" bottom="20" left="10"/></padding>
    <Label fx:id="lblHOST" GridPane.columnIndex="0" GridPane.rowIndex="0" text="SMTP Server: "/>
    <ComboBox fx:id="cmbHOST" value=" " GridPane.columnIndex="1" GridPane.rowIndex="0">
        <items>
            <FXCollections fx:factory="observableArrayList">
                <String fx:value="smtp.gmail.com"/>
                <String fx:value="smtp.mail.yahoo.com"/>
            </FXCollections>
        </items>
    </ComboBox>
    <Label fx:id="lblUN" GridPane.columnIndex="0" GridPane.rowIndex="1" text="Username/email: "/>
    <TextField fx:id="tUN" GridPane.columnIndex="1" GridPane.rowIndex="1" />
    <Label fx:id="lblPW" GridPane.columnIndex="0" GridPane.rowIndex="2" text="Password: "/>
    <PasswordField fx:id="tPW" GridPane.columnIndex="1" GridPane.rowIndex="2" />
    <Button fx:id="btnLOG" text="Verify" GridPane.columnIndex="1" GridPane.rowIndex="3" onAction="#handleVerifyButton"/>
</GridPane>

LaySentController.java

package org.tempestdesign.sendclient;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;

import java.io.IOException;

public class LaySentController extends TransitionController {

    @FXML
    protected void handleBackButton(ActionEvent e) throws IOException {
        tto.setText(mto);
        ttext.setText(cTEXT);
        tsub.setText(msub);
        thead.setText(mhead);
        transitionScene("Edit Email", "layedit.fxml", 640, 710);
    }
}

laysent.fxml

<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Text?>
<?import javafx.scene.control.Button?>

<?scenebuilder-stylesheet sendclient.css?>
<VBox spacing="21" alignment="center" fx:id="laySENT" styleClass="lay-sent"
      fx:controller="org.tempestdesign.sendclient.LaySentController"
      xmlns:fx="http://javafx.com/fxml">
    <Text text="Message mailed to recipient(s)." styleClass="t-suc" layoutX="80"/>
    <Text text="Don't forget to check your inbox regarding \n send errors and replies." styleClass="t-sent"/>
    <Button fx:id="btnBACK" text="Return to Editor" onAction="#handleBackButton"/>
</VBox>

sendclient.css

.lay-log {
    -fx-font-size: 13px;
    -fx-background-color: linear-gradient(#ff2, #e5bb00), linear-gradient(#e43, #f84);
    -fx-background-radius: 2, 29;
}

.lay-edit {
    -fx-font-size: 13px;
    -fx-background-color: linear-gradient(#ff2, #e5bb00), linear-gradient(#f53, #f86);
    -fx-background-radius: 2, 29
}

.lay-sent {
    -fx-background-color: linear-gradient(#ff2, #e5bb00), linear-gradient(#f53, #f86);
    -fx-background-radius: 2, 29;
}

.t-suc {
    -fx-font-size: 32px;
    -fx-alignment: CENTER;
    -fx-fill: #2b3;
}

.t-sent {
    -fx-font-size: 10px;
    -fx-fill: #676767
    -fx-alignment: CENTER;
}

TransitionController.java

package org.tempestdesign.sendclient;

import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

import java.io.IOException;

public class TransitionController {

    private Stage prevStage;

    public void setPrevStage(Stage s) { this.prevStage = s; }

    protected void transitionScene(String sceneTitle, String fxmlFileName, double width, double height) throws IOException {
        Stage s = new Stage();
        s.setTitle(sceneTitle);
        Pane p = FXMLLoader.load(getClass().getResource(fxmlFileName));
        s.setScene(new Scene(p, width, height));
        prevStage.close();
        s.show();
    }
}
\$\endgroup\$
  • 1
    \$\begingroup\$ Oops, just noticed that I never setPrevStage for any but the first one - I'll add that later if I remember. \$\endgroup\$ – Dannnno Jan 6 '16 at 17:39
  • \$\begingroup\$ Wonderful. Thanks for not being brief. I've never worked with fxml, so this is a great chance for me to pick it up. I'm definitely marking this for reference; your code is so legible that it hurts. \$\endgroup\$ – Phil C. Jan 11 '16 at 1:43
0
\$\begingroup\$

Could remove the string literals from the source, "smtp.mail.yahoo.com", replace with constants. Retrieve values from a properties collection, specify values outside of the source in XML.

The Mail method has multiple System.out.println, could consolidate into one call. Suggest Stringbuilder and append to prepare entire message in advance.

Guava Throwables or defining custom exceptions may help here in Mail method:

} catch (MessagingException e) {
    throw new RuntimeException(e);
} // end try/catch //
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.