patternjavaMinor
JavaFX eMail client
Viewed 0 times
clientjavafxemail
Problem
I've just finished up a functional emailing client in the
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
Here is the link to download the
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:
Note for
Currently only supports
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/ {
static Session sesh;
static Pr
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/ {
static Session sesh;
static Pr
Solution
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
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
LayEditController.java
layedit.fxml
LayLogController.java
```
package org.tempestdesign.sen
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 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
\t # \t Body \t # \t
LayLogController.java
```
package org.tempestdesign.sen
Code Snippets
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();
}
}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);
}
}
}<?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>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,
<?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>Context
StackExchange Code Review Q#114005, answer score: 2
Revisions (0)
No revisions yet.