Skip to content
Snippets Groups Projects
Commit 3827cba6 authored by Masson Lea's avatar Masson Lea
Browse files

trying to make email work but not yet working

parent a0c1138f
No related branches found
No related tags found
No related merge requests found
File added
......@@ -31,6 +31,7 @@ endorsed.classpath=
excludes=
file.reference.bluecove-2.1.1-SNAPSHOT.jar=bluecove-2.1.1-SNAPSHOT.jar
file.reference.javax.mail-api-1.4.7.jar=javax.mail-api-1.4.7.jar
file.reference.javax.mail.jar-1=javax.mail.jar
includes=**
# Non-JavaFX jar file creation is deactivated in JavaFX 2.0+ projects
jar.archive.disabled=true
......@@ -38,7 +39,8 @@ jar.compress=false
javac.classpath=\
${javafx.classpath.extension}:\
${file.reference.bluecove-2.1.1-SNAPSHOT.jar}:\
${file.reference.javax.mail-api-1.4.7.jar}
${file.reference.javax.mail-api-1.4.7.jar}:\
${file.reference.javax.mail.jar-1}
# Space-separated list of extra javac options
javac.compilerargs=
javac.deprecation=false
......
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package MailUtil;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
/**
*
* @author lmasson
*/
public class MailUtil {
private static String USER_NAME = "plantequipleure"; // GMail user name (just the part before "@gmail.com")
private static String PASSWORD = "cryingplant12"; // GMail password
private static String RECIPIENT = "theo.coulin@gmail.com";
public static void sendDistressMail() {
String from = USER_NAME;
String pass = PASSWORD;
String subject = "Votre plante pleure";
sendFromGMail(from, pass, RECIPIENT, subject, "Ta plante pleure arrose-la");
}
private static void sendFromGMail(String from, String pass, String to, String subject, String body) {
Properties props = System.getProperties();
String host = "smtp.gmail.com";
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", pass);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props);
MimeMessage message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(from));
InternetAddress toAddress = new InternetAddress(to);
message.addRecipient(Message.RecipientType.TO, toAddress);
message.setSubject(subject);
message.setText(body);
Transport transport = session.getTransport("smtp");
transport.connect(host, from, pass);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
catch (AddressException ae) {
ae.printStackTrace();
}
catch (MessagingException me) {
me.printStackTrace();
}
}
}
......@@ -9,17 +9,15 @@ import Bluetooth.BluetoothManager;
import Bluetooth.BluetoothThread;
import Interfaces.Observable;
import Interfaces.Observer;
import MailUtil.MailUtil;
import Util.CalendarUtil;
import java.io.IOException;
import java.net.URL;
import java.text.NumberFormat;
import java.time.LocalDate;
import java.time.Period;
import java.util.Calendar;
import java.util.Formatter;
import java.util.Locale;
import java.util.ResourceBundle;
import javafx.application.Platform;
import javafx.beans.InvalidationListener;
import javafx.beans.binding.Bindings;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
......@@ -35,11 +33,14 @@ import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.Slider;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
/**
* FXML Controller class
*
......@@ -112,6 +113,7 @@ public class FXMLVueController implements Initializable, Observer {
private boolean isConnected = false;
private BluetoothManager bluetoothManager;
private BluetoothThread workingThread;
LocalDate lastMailDay;
/**
......@@ -126,6 +128,9 @@ public class FXMLVueController implements Initializable, Observer {
currentGridPane = accueil;
currentButton = buttonAccueil;
openPage(accueil, buttonAccueil);
//TODO initialize lastMailDay with database
lastMailDay = LocalDate.of(2018, 12, 12);
seuil.valueProperty().addListener(new ChangeListener<Number>() {
@Override
......@@ -163,14 +168,13 @@ public class FXMLVueController implements Initializable, Observer {
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
System.out.println(oldValue);
System.out.println(newValue);
if (oldValue != null && newValue != null && humiditySeuil.getText() != null && Integer.parseInt(newValue) < Integer.parseInt(humiditySeuil.getText())) {
if (humiditySeuil.getText() != null && Integer.parseInt(newValue) < Integer.parseInt(humiditySeuil.getText())) {
changeImagePlante(true);
alert = true;
if(Integer.parseInt(oldValue) > Integer.parseInt(humiditySeuil.getText())){
work();
}
}
else {
} else {
if(alert){
changeImagePlante(false);
}
......@@ -182,7 +186,7 @@ public class FXMLVueController implements Initializable, Observer {
tmp = false;
} catch(IOException e){
e.printStackTrace();
}
}
}
}
}
......@@ -218,7 +222,7 @@ public class FXMLVueController implements Initializable, Observer {
@FXML
private void clickActivationBluetooth(ActionEvent event) {
if(!this.isConnected){
isConnected = bluetoothManager.launch();
isConnected = bluetoothManager.launch();
}
if(isConnected){
......@@ -307,14 +311,18 @@ public class FXMLVueController implements Initializable, Observer {
}
private void work(){
computeGravity();
if(CalendarUtil.isBusinessDay(Calendar.getInstance())){
System.out.println("is business");
// compute & send
computeGravity();
System.out.println("is business day");
} else {
System.out.println("is not");
// send mail, not this
computeGravity();
System.out.println("is not business day");
LocalDate today = LocalDate.now();
Period difference = Period.between(lastMailDay, today);
if(difference.getDays() > 1){
// send mail
MailUtil.sendDistressMail();
lastMailDay = today;
}
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment