JavaでメールといえばJavaMail一択だと思っていたのですが、Simple Java Mailという中々良さげなライブラリがありました。
2007年ぐらいから開発されていて、2017年現在でもメンテや改造が進んでいるようです。中身が気になる方はGitHubへ。
GitHub - bbottema/simple-java-mail: Simple API, Complex Emails (JavaMail smtp wrapper)
「Simple API, Complex Emails」ってちょっとカッコイイですね。ちなみに中ではJavaMailを利用しているので、ラッパーライブラリという位置づけです。
細かい記述方法とかは別途解説していくとして(結構色んなことができそう)、シンプルなコードでJavaMailと簡単に比べてみます。
SendGridのSMTPサーバに接続して、テキストとHTML両方含めたマルチパートメールを送るというもの。JavaMailだとこんな感じですね。
JavaMailで書いた場合
package jp.co.kke.simplejavamailsample; import java.util.Properties; import javax.mail.Authenticator; import javax.mail.BodyPart; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Multipart; import javax.mail.NoSuchProviderException; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; /** * JavaMailを利用したメール送信 * * @author kikuta */publicclass Main { publicstaticvoid main(String[] args) throws MessagingException { sendByJavaMail(); } privatestaticvoid sendByJavaMail() throws NoSuchProviderException, MessagingException { //接続先の設定 Properties props = new Properties(); props.put("mail.transport.protocol", "smtp"); props.put("mail.smtp.host", "smtp.sendgrid.net"); props.put("mail.smtp.port", 587); props.put("mail.smtp.auth", "true"); //Authenticatorを継承したクラスを作って(ここではSMTPAuthenticator)//getPasswordAuthentication()をオーバーライドして認証情報をセット Authenticator auth = new SMTPAuthenticator(); //セッション Session mailSession = Session.getDefaultInstance(props, auth); //メール送信のため Transport transport = mailSession.getTransport(); //メールの情報を設定 MimeMessage message = new MimeMessage(mailSession); //マルチパート・メディアタイプはalternativeを指定 Multipart multipart = new MimeMultipart("alternative"); BodyPart part1 = new MimeBodyPart(); part1.setText("マルチパートのテキストだよ"); BodyPart part2 = new MimeBodyPart(); part2.setContent("<b>マルチパートのHTMLだよ</b>", "text/html;charset=UTF-8"); multipart.addBodyPart(part1); multipart.addBodyPart(part2); message.setContent(multipart); //送信元 message.setFrom(new InternetAddress("kikutaro_from@example.com")); //メールタイトル message.setSubject("サブジェクトだよ"); //送信先 message.addRecipient(Message.RecipientType.TO, new InternetAddress("kikutaro_to@example.com")); //メール送信 transport.connect(); transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO)); transport.close(); } privatestaticclass SMTPAuthenticator extends Authenticator { @Overridepublic PasswordAuthentication getPasswordAuthentication() { String username = "****id****"; //SendGridのログインID String password = "****pass****"; //SendGridのログインパスワードreturnnew PasswordAuthentication(username, password); } } }
Simle Java Mailで書いた場合
Simple Java Mailを利用したコードはこちらです。
package jp.co.kke.simplejavamailsample; import org.simplejavamail.email.Email; import org.simplejavamail.email.EmailBuilder; import org.simplejavamail.mailer.Mailer; /** * Simple Java Mailを利用したメール送信 * * @author kikuta */publicclass Main { publicstaticvoid main(String[] args) throws MessagingException { sendBySimpleJavaMail(); } privatestaticvoid sendBySimpleJavaMail() { Email email = new EmailBuilder() .from("kikutaro_from", "kikutaro_from@example.com") .to("kikutaro_to", "kikutaro_to@example.com") .subject("サブジェクトだよ") .text("マルチパートのテキストだよ") .textHTML("<b>マルチパートのHTMLだよ</b>") .build(); new Mailer("smtp.sendgrid.net", 587, "****id****", "****pass****").sendMail(email); } }
Builderでスッキリ!もちろんHeaderの設定とか色々なことができます。