Challenge Engineer Life !

エンジニア人生を楽しみたい!仕事や趣味で学んだ技術的なことを書いていくブログです。

SendGridFX : JavaFX meets SendGrid ! #JavaFX #SendGrid

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.

f:id:kikutaro777:20131025233829j:plain

Only input sending e-mail address and subject and contents of mail by HTML.

f:id:kikutaro777:20131025234142j:plain

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!
f:id:kikutaro777:20131025234414j:plain

I opened it and saw HTML mail.

f:id:kikutaro777:20131025235010j:plain

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 {
    
    //SMTP authentication info of SendGrid
    private static final String SMTP_AUTH_USER = "blah-blah-blah";
    private static final String SMTP_AUTH_PWD  = "blah-blah-blah";
    
    //Mail from info
    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();
            
            //setting mail from
            mail.setFrom(MAIL_FROM);
            mail.setFromName(MAIL_FROM_NAME);
            
            //setting mail to
            mail.setTo(textTo.getText());
            
            //setting mail subject
            mail.setSubject(textTitle.getText());
            
            //setting HTML content
            mail.setHtml(editor.getHtmlText());
            
            //sending mail
            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 :)

にほんブログ村 IT技術ブログへ
にほんブログ村 にほんブログ村 IT技術ブログ Javaへ
にほんブログ村