Simple mail client

UPDATE : Just changed colors. Blue screen fetish is not working anymore :D



I have a blue-screen fetish. Yes, since old QBasic IDE. So I like it, and I find it appropriate for this kind of a...low-level functionality software. When, and if, this app should grow, with it's usability, so will its' GUI :)
It can read newest mail ( text ) and send one. I've used Task just for a sake of ProgressIndicator.


bebicamc02.BebicaMC02.java
1:  /*  
2:   * Bebica Mail Client  
3:   * ver 0.2  
4:   */  
5:  package bebicamc02;  
6:    
7:  import java.io.IOException;  
8:  import java.net.URL;  
9:  import java.util.Properties;  
10:  import javafx.application.Application;  
11:  import javafx.concurrent.Task;  
12:  import javafx.fxml.FXML;  
13:  import javafx.fxml.FXMLLoader;  
14:  import javafx.scene.Scene;  
15:  import javafx.scene.control.*;  
16:  import javafx.scene.layout.AnchorPane;  
17:  import javafx.stage.Stage;  
18:  import javax.mail.*;  
19:  import javax.mail.internet.InternetAddress;  
20:  import javax.mail.internet.MimeMessage;  
21:  import javax.mail.internet.MimeMultipart;  
22:    
23:  /**  
24:   *  
25:   * @author Djordje Gavrilovic  
26:   */  
27:  public class BebicaMC02 extends Application {  
28:      
29:    private Task getMail;  
30:    private Task sendMail;  
31:    @FXML  
32:    private TextArea mailDisplayTa;  
33:    @FXML  
34:    private Button fetchBtn;  
35:    @FXML  
36:    private AnchorPane root;  
37:    @FXML  
38:    private ProgressIndicator progressIndicator;  
39:    @FXML  
40:    private TextField subjectTf; // Label would not listen (?!)  
41:    @FXML  
42:    private TextField fromTf;  
43:    @FXML  
44:    private TextArea mailComposeTa;  
45:    @FXML  
46:    private TextField inputSubjectTf;  
47:    @FXML  
48:    private TextField inputRecipientTf;  
49:    @FXML  
50:    private Label sentLbl;  
51:      
52:    public BebicaMC02() {}  
53:      
54:    @FXML  
55:    public void initialize() {      
56:      mailDisplayTa.prefWidthProperty().bind(root.widthProperty());  
57:      mailComposeTa.prefWidthProperty().bind(root.widthProperty());  
58:      progressIndicator.setVisible(false);  
59:    }  
60:      
61:    @Override  
62:    public void start(Stage primaryStage) throws IOException {  
63:      URL fxmlUrl = getClass().getClassLoader().getResource("view/BebicaMC02View.fxml");  
64:      AnchorPane root = FXMLLoader.<AnchorPane>load(fxmlUrl);  
65:      Scene scene = new Scene(root);  
66:      primaryStage.setScene(scene);  
67:      primaryStage.setTitle("Bebica MailClient");  
68:      primaryStage.centerOnScreen();  
69:      primaryStage.show();  
70:    }  
71:    
72:    /*  
73:     * GET AND DISPLAY LAST MAIL RECIEVED  
74:     */  
75:    @FXML  
76:    private void getMail() throws IOException {  
77:      getMail = recieve();  
78:      progressIndicator.setVisible(true);  
79:      new Thread(getMail).start();  
80:    }  
81:      
82:    private Task recieve() throws IOException {  
83:      return new Task() {  
84:        @Override  
85:        protected Object call() throws Exception {  
86:            
87:          Properties props = new Properties();  
88:          props.setProperty("mail.store.protocol", "imaps");  
89:          Session session = Session.getInstance(props);  
90:          // fetching the last recieved mail in inbox folder  
91:          try {  
92:            Store store = session.getStore();  
93:            Message msg = null;  
94:            store.connect("imap.mail.yahoo.com", "YOUR@EMAIL", "PASSWORD");  
95:            Folder inbox = store.getFolder("Inbox");  
96:            inbox.open(Folder.READ_ONLY);  
97:            msg = inbox.getMessage(inbox.getMessageCount());  
98:              
99:            StringBuilder from = new StringBuilder();  
100:            for (Address adr : msg.getFrom()) {  
101:              from.append(adr + "");  
102:            }  
103:            // reading content - I've kept it as it is for future usage  
104:            if (msg.isMimeType("text/plain")) {  
105:              System.out.println("TEXT/PLAIN");  
106:              mailDisplayTa.setText(msg.getContent().toString());  
107:              progressIndicator.setVisible(false);  
108:              subjectTf.setText("Subject: " + msg.getSubject());  
109:              fromTf.setText("From: " + msg.getFrom()[0].toString());  
110:            } else if (msg.isMimeType("multipart/*")) {  
111:              System.out.println("MULTI");  
112:              MimeMultipart mimeMultipart = (MimeMultipart) msg.getContent();              
113:              String result = "";  
114:              int count = mimeMultipart.getCount();  
115:              for (int i = 0; i < count; i++) {  
116:                BodyPart bodyPart = mimeMultipart.getBodyPart(i);  
117:                if (bodyPart.isMimeType("text/plain")) {  
118:                  result = result + "\n" + bodyPart.getContent();  
119:                  System.out.println("PLAIN");  
120:                } else if (bodyPart.isMimeType("text/html")) {  
121:                  String html = (String) bodyPart.getContent();  
122:                  result = result + "\n" + org.jsoup.Jsoup.parse(html).text();  
123:                  System.out.println("WITH HTML");  
124:                } else if (bodyPart.getContent() instanceof MimeMultipart) {  
125:                  result = result + (MimeMultipart) bodyPart.getContent();  
126:                }  
127:              }  
128:              subjectTf.setText("Subject: " + msg.getSubject());  
129:              fromTf.setText("From: " + msg.getFrom()[0].toString());  
130:              mailDisplayTa.setText(result);              
131:              progressIndicator.setVisible(false);  
132:            } else {  
133:              mailDisplayTa.setText("Your e-mail client can not read this type of message");  
134:            }  
135:          } catch (MessagingException ex) {  
136:            System.out.println("Mail fetching error: " + ex);;  
137:          }  
138:          return true;  
139:        }  
140:      };  
141:    }  
142:    
143:    /*  
144:     * SEND MAIL  
145:     */  
146:    @FXML  
147:    private void sendMail() throws IOException {  
148:      sendMail = send();  
149:      progressIndicator.setVisible(true);  
150:      new Thread(sendMail).start();  
151:    }  
152:      
153:    private Task send() throws IOException {  
154:      return new Task() {  
155:        @Override  
156:        protected Object call() throws Exception {  
157:            
158:          Properties props = new Properties();  
159:          props.setProperty("mail.smtp.host", "smtp.mail.yahoo.com");  
160:          props.setProperty("mail.smtp.socketFactory.port", "465");  
161:          props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");  
162:          props.setProperty("mail.smtp.auth", "true");  
163:          props.setProperty("mail.smtp.port", "465");  
164:            
165:          Session session = Session.getDefaultInstance(props, new Authenticator() {  
166:            @Override  
167:            protected PasswordAuthentication getPasswordAuthentication() {  
168:              return new PasswordAuthentication("YOUR@EMAIL", "PASSWORD");  
169:            }  
170:          });  
171:            
172:          try {  
173:            Message msg = new MimeMessage(session);  
174:            msg.setFrom(new InternetAddress("YOUR@EMAIL"));  
175:            msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(inputRecipientTf.getText()));  
176:            msg.setSubject(inputSubjectTf.getText());  
177:            msg.setText(mailComposeTa.getText());  
178:            Transport.send(msg);  
179:            System.out.println("MAIL SENT");  
180:            progressIndicator.setVisible(false);  
181:            sentLbl.setVisible(true);  
182:          } catch (MessagingException ex) {  
183:            System.err.println("Cannot send email. " + ex);  
184:          }          
185:          return true;  
186:        }  
187:      };  
188:    }  
189:      
190:    public static void main(String[] args) {  
191:      launch(args);  
192:    }  
193:      
194:  }  
195:    

view.BebicaMC02View.fxml
1:  <?xml version="1.0" encoding="UTF-8"?>  
2:    
3:  <?import java.lang.*?>  
4:  <?import java.net.*?>  
5:  <?import java.util.*?>  
6:  <?import javafx.scene.*?>  
7:  <?import javafx.scene.control.*?>  
8:  <?import javafx.scene.layout.*?>  
9:    
10:  <AnchorPane fx:id="root" prefHeight="800.0" prefWidth="1000.0" styleClass="mainFxmlClass"   
11:        xmlns:fx="http://javafx.com/fxml" fx:controller="bebicamc02.BebicaMC02">  
12:    <stylesheets>  
13:      <URL value="@BebicaMC02Style.css"/>  
14:    </stylesheets>  
15:    <Button fx:id="fetchBtn" text="Get mail" AnchorPane.leftAnchor="5.0" AnchorPane.topAnchor="5.0"  
16:        onAction="#getMail"/>  
17:    <TextField fx:id="subjectTf" prefWidth="500.0" editable="false" AnchorPane.topAnchor="5.0"   
18:          AnchorPane.leftAnchor="100.0"/>  
19:    <TextField fx:id="fromTf" prefWidth="300.0" editable="false" AnchorPane.rightAnchor="60.0"   
20:          AnchorPane.topAnchor="5.0"/>  
21:    <ProgressIndicator fx:id="progressIndicator" prefHeight="30" AnchorPane.rightAnchor="10.0"   
22:              AnchorPane.topAnchor="5.0"/>    
23:    <TextArea fx:id="mailDisplayTa" prefHeight="400.0" wrapText="true" text="..."   
24:         AnchorPane.leftAnchor="5.0"   
25:         AnchorPane.topAnchor="40.0" AnchorPane.rightAnchor="5.0"/>  
26:    <Label text="Compose new e-mail: " AnchorPane.topAnchor="450.0" AnchorPane.leftAnchor="15.0" />  
27:    <Label fx:id="sentLbl" text="Mail sent!" AnchorPane.topAnchor="450.0" AnchorPane.rightAnchor="15.0"   
28:        visible="false" />  
29:    <TextField fx:id="inputSubjectTf" text="input subject..." AnchorPane.leftAnchor="7.0"   
30:          AnchorPane.topAnchor="470.0"/>  
31:    <TextField fx:id="inputRecipientTf" text="input recipient..." AnchorPane.leftAnchor="250.0"   
32:          AnchorPane.topAnchor="470.0"/>  
33:    <TextArea fx:id="mailComposeTa" prefHeight="250.0" wrapText="true" AnchorPane.leftAnchor="5.0"   
34:         AnchorPane.topAnchor="505.0" AnchorPane.rightAnchor="5.0" AnchorPane.bottomAnchor="50.0"/>  
35:    <Button fx:id="sendBtn" text="Send mail" AnchorPane.leftAnchor="5.0" AnchorPane.bottomAnchor="15.0"   
36:        onAction="#sendMail"/>  
37:    <Label fx:id="mylbl" text="Djordje Gavrilovic 2017" AnchorPane.rightAnchor="5.0"   
38:        AnchorPane.bottomAnchor="15.0"/>      
39:  </AnchorPane>  
40:    

view.BebicaMC02Style.css
1:    
2:  .root {  
3:    -fx-background-color: blue;   
4:  }  
5:  .button {  
6:    -fx-background-color: white;  
7:    -fx-text-fill: blue;  
8:  }  
9:  .progress-indicator {  
10:    -fx-progress-color: white;  
11:  }  
12:  .label {  
13:    -fx-text-fill: white;  
14:  }  
15:  .text-area {  
16:    -fx-control-inner-background: blue;  
17:    -fx-text-fill: white;   
18:  }  
19:  #mylbl {  
20:    -fx-font-size: 10px;  
21:  }  
22:  #subjectTf {  
23:    -fx-background-color: blue;  
24:    -fx-text-fill: white;  
25:  }  
26:  #fromTf {  
27:    -fx-background-color: blue;  
28:    -fx-text-fill: white;  
29:  }  
30:  #inputSubjectTf {  
31:    -fx-background-color: blue;  
32:    -fx-text-fill: white;  
33:    -fx-border-color: white;    
34:  }  
35:  #inputRecipientTf {  
36:    -fx-background-color: blue;  
37:    -fx-text-fill: white;  
38:    -fx-border-color: white;   
39:  }  

Comments