I'm new to JavaFX and want to make something interesting.
A month ago I signed up SendGrid, because my old colleague @nakansuke starts to new business with SendGrid company.
He advised me that there is a good Java wrapper library of SendGrid WebAPI, it's sendgrid4j.
http://oss.flect.co.jp/libs/en/sendgrid4j.html
Exactly, it's good and easy to use SendGrid WebAPI.
Today I was having fun with JavaFX and SendGrid.
My simple idea is sending HTML mail with HTMLEditor on JavaFX.
This is my simple application.
I only use Label, TextField, Button and HTMLEditor.
Only input sending e-mail address and subject and contents of mail by HTML.
HTMLEditor component on JavaFX is good to write HTML.
I sent this mail to my web-mail of Yahoo, so checked it.
I think JavaFX programmer might say "Hey guy, why don't you check your web mail by WebView?" Yeah, that's true. I did it :)
This is my JavaFX application includes WebView.
Yes! I got the mail!
I opened it and saw HTML mail.
Of course, it's same looking of HTMLEditor :)
I show my code of JavaFX Controller class. It's very simple.
package jp.co.hoge.sendgridfx;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.web.HTMLEditor;
import jp.co.flect.sendgrid.SendGridClient;
import jp.co.flect.sendgrid.SendGridException;
import jp.co.flect.sendgrid.model.WebMail;
public class SendGridFXController implements Initializable {
private static final String SMTP_AUTH_USER = "blah-blah-blah";
private static final String SMTP_AUTH_PWD = "blah-blah-blah";
private static final String MAIL_FROM = "kiku@test.com";
private static final String MAIL_FROM_NAME = "Test from Kiku";
SendGridClient client;
@FXML
private HTMLEditor editor;
@FXML
private TextField textTo;
@FXML
private TextField textTitle;
@FXML
private Label message;
@FXML
private void sendButtonAction(ActionEvent event) {
try {
WebMail mail = new WebMail();
mail.setFrom(MAIL_FROM);
mail.setFromName(MAIL_FROM_NAME);
mail.setTo(textTo.getText());
mail.setSubject(textTitle.getText());
mail.setHtml(editor.getHtmlText());
client.mail(mail);
message.setText("Sending mail is success.");
} catch (IOException | SendGridException ex) {
message.setText("Sending mail is failed.");
}
}
@Override
public void initialize(URL url, ResourceBundle rb) {
client = new SendGridClient(SMTP_AUTH_USER, SMTP_AUTH_PWD);
}
}
We can use sendgrid4j by maven.
Here is pom.xml.
Repository setting of sendgrid4j.
<repositories>
<repository>
<id>jp.co.flect</id>
<name>FLECT maven repository</name>
<url>http://flect.github.io/maven-repo/</url>
</repository>
</repositories>
Dependency is here.
<dependencies>
<dependency>
<groupId>jp.co.flect</groupId>
<artifactId>sendgrid4j</artifactId>
<version>0.9.1</version>
</dependency>
</dependencies>
That's it :)