この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。
はじめに
前回はSpringを使用したメール送信について書きましたが、ファイル添付を行ったりする方法について調べた事を書きます。
プログラムの目的
・Javaでメール送信を行う。 ・ローカルのファイルを添付する。 ・テンプレートを使用した文章を作成する。 ・メールの送信受信は共にGmailを使用。
環境
Mac OSX 10.10.5 Yosemite Java 1.8.0_91 Spring Boot 1.3.7 PostgreSQL 9.5.1 Eclipse Mars 2
コード
build.gradle
dependencies {
compile('org.springframework.boot:spring-boot-starter-mail')
compile('org.apache.velocity:velocity')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
application.properties
spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=送信者のメールアドレス
spring.mail.password=パスワード
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
前回と同じくメールを設定。
テンプレート : Message.vm
こんにちは $name1 さん。
私は $name2 です。
メールの本文になります。 「$〜」とした箇所に文字を当てられます。 src/main/resources/にフォルダ「templates」を作成して中に配置します。
メインクラス
package com.send.mail;
import java.io.StringWriter;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
@SpringBootApplication
public class MailSenderApplication {
public static void main(String[] args) {
try (ConfigurableApplicationContext cac = SpringApplication.run(MailSenderApplication.class, args);) {
cac.getBean(MailSenderApplication.class).send();
}
}
@Autowired
private JavaMailSender javaMailSender;
@Autowired
private VelocityEngine velocityEngine;
@Async
public void send() {
try {
MimeMessage msg = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(msg, true);
VelocityContext context = new VelocityContext();
context.put("name1", "Classmethod");
context.put("name2", "クラスメソッド");
StringWriter writer = new StringWriter();
velocityEngine.mergeTemplate("Message.vm", "UTF-8", context, writer);
helper.setText(writer.toString());
helper.setSubject("送信テスト");
helper.setTo("XXXXX@gmail.com");
FileSystemResource res = new FileSystemResource("/Users/user/Documents/counter.csv");
helper.addAttachment("counter.csv", res);
javaMailSender.send(msg);
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
24行目、自身のBeanを取得して、送信メソッドのsend()を実行。
28〜31行目、必要なクラスを@Autowired。
33〜59行目、send()。メール作成と送信を行うメソッド。 37~38行目、メッセージ作成準備。ファイルを添付する場合は「new MessageHelper」の第2引数をtrueにします。 40〜42行目、テンプレートの「$〜」の箇所に置換する文字を設定。 44行目、テンプレートと文字設定をまとめるStringWriter。 45行目、第1引数:テンプレートファイル名、第2引数:文字コード、第3引数:VelocityContext、第4引数:StringWriter。 47行目、setTextでメッセージを設定。 48行目、setSubtitleでメールタイトルを設定。 49行目、送信先アドレスを設定。 51〜52行目、添付ファイルのパスと送信時のファイル名を設定。 54行目、送信。
実行結果
さいごに
Springとそれ以外が混在しているのが気になりますが、Springを使えばこういった書き方ができるという理解をしました。