質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
JUnit

JUnitは、Javaで開発されたプログラムのユニットテストを行うためのアプリケーションフレームワークです。簡単にプログラムのユニットテストを自動化することができ、結果もわかりやすく表示されるため効率的に開発時間を短縮できます。

Spring Boot

Spring Bootは、Javaのフレームワークの一つ。Springプロジェクトが提供する様々なフレームワークを統合した、アプリケーションを高速で開発するために設計されたフレームワークです。

Q&A

0回答

526閲覧

wiserを使ったメールの送信結果の取得方法を教えてください。

yuki1111

総合スコア72

JUnit

JUnitは、Javaで開発されたプログラムのユニットテストを行うためのアプリケーションフレームワークです。簡単にプログラムのユニットテストを自動化することができ、結果もわかりやすく表示されるため効率的に開発時間を短縮できます。

Spring Boot

Spring Bootは、Javaのフレームワークの一つ。Springプロジェクトが提供する様々なフレームワークを統合した、アプリケーションを高速で開発するために設計されたフレームワークです。

0グッド

1クリップ

投稿2019/06/30 08:15

編集2020/10/30 02:43

教えていただきたいこと

WiserでGmailの送信結果を取得する方法。

やりたいこと

Spring BootでGmailから送信できるかどうかの単体テスト。
SendMailServiceImplTestの100行目でメールの送信結果を取得したい。

何卒よろしくお願いします。

Git Hub

project1

SendMailServiceImplTest

Java

1package com.koikeya.project1.domain.service.impl; 2 3import static org.hamcrest.CoreMatchers.*; 4import static org.junit.Assert.*; 5 6import java.io.BufferedReader; 7import java.io.IOException; 8import java.io.InputStream; 9import java.io.InputStreamReader; 10import java.nio.charset.Charset; 11import java.util.List; 12 13import javax.mail.MessagingException; 14 15import org.junit.AfterClass; 16import org.junit.BeforeClass; 17import org.junit.Test; 18import org.junit.runner.RunWith; 19import org.springframework.beans.factory.annotation.Autowired; 20import org.springframework.boot.test.context.SpringBootTest; 21import org.springframework.test.context.junit4.SpringRunner; 22import org.springframework.test.context.web.WebAppConfiguration; 23import org.springframework.transaction.annotation.Transactional; 24import org.subethamail.wiser.Wiser; 25import org.subethamail.wiser.WiserMessage; 26 27import com.koikeya.project1.domain.model.MailForm; 28import com.koikeya.project1.domain.service.SendMailService; 29 30/** 31 * メール送信サービステストクラス 32 */ 33@RunWith(SpringRunner.class) 34@SpringBootTest 35@WebAppConfiguration 36@Transactional 37public class SendMailServiceImplTest { 38 39 /** 40 * メール送信サービス 41 */ 42 @Autowired 43 SendMailService SendMailService; 44 45 /** 46 * メールフォームクラス 47 */ 48 @Autowired 49 MailForm mailForm; 50 51 private static Wiser wiser; 52 53 54 /** 55 * メールサーバーを立てる 56 * 57 * @throws Exception 例外 58 */ 59 // @BeforeClass・・・ pubic void なメソッドに付与することで,各テストメソッドを実行する前に1度だけ実行してくれる. 60 // いわゆる setup メソッド 61 @BeforeClass 62 public static void メールサーバを立てる() throws Exception { 63 wiser = new Wiser(); 64// wiser.setPort(9999); 65// wiser.setHostname("localhost"); 66 wiser.setPort(587); 67 wiser.setHostname("smtp.gmail.com"); 68 wiser.start(); 69 70 } 71 72 /** 73 * メールサーバを落とす 74 * 75 * @throws Exception 例外 76 */ 77 // pubic void なメソッドに付与することで,各テストメソッドを実行した前に1度だけ実行してくれる. 78 // いわゆる teardown メソッド 79 @AfterClass 80 public static void メールサーバを落とす() throws Exception { 81 wiser.stop(); 82 } 83 84 /** 85 * メールを送信できることをテストする 86 * 87 * @throws MessagingException MessagingException 88 * @throws IOException 入出力例外 89 */ 90 @Test 91 public void メール通知() throws MessagingException, IOException { 92 93 mailForm.setContent("aaaa"); 94 mailForm.setName("sssss"); 95 mailForm.setSex("男"); 96 mailForm.setMail("○○○@gmail.com"); 97 SendMailService.send(mailForm); 98 99 //検証 100 List<WiserMessage> messages = wiser.getMessages(); 101 102 assertThat(messages.size(), is(1)); 103 // 送信元 104 assertThat(messages.get(0).getEnvelopeSender(), is("○○○@gmail.com")); 105 // 送信先 106 assertThat(messages.get(0).getEnvelopeReceiver(), is("yuki.koike8527@gmail.com")); 107 assertThat(messages.get(0).getMimeMessage().getSubject(), is("お問い合わせがありました")); 108 109 // 本文取得 110 InputStream inputStream = messages.get(0).getMimeMessage().getInputStream(); 111 BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, Charset.forName("UTF-8"))); 112 113 assertThat(reader.readLine(), is("名前: sssss")); 114 assertThat(reader.readLine(), is("性別: 男")); 115 assertThat(reader.readLine(), is("問い合わせ内容: aaaa")); 116 } 117 118} 119

application.properties

properties

1server.servlet.context-path=/project1 2spring.thymeleaf.cache=false 3spring.datasource.url=jdbc:mysql://localhost:3306/project1 4spring.datasource.username=project1 5spring.datasource.password=abcd0711 6spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver 7spring.jpa.database=MYSQL 8#spring.jpa.hibernate.ddl-auto=update 9logging.file=logs/myapp.log 10logging.level.org.springframework.web=INFO 11security.basic.enabled=false 12management.security.enabled=false 13spring.mail.host=smtp.gmail.com 14spring.mail.port=587 15spring.mail.username=○○○@gmail.com 16spring.mail.password=*********** 17spring.mail.properties.mail.smtp.auth=true 18spring.mail.properties.mail.smtp.starttls.enable=true 19# TODO 30分にもどす 20server.servlet.session.timeout=1

SendMailServiceImpl

Java

1package com.koikeya.project1.domain.service.impl; 2 3import org.springframework.beans.factory.annotation.Autowired; 4import org.springframework.mail.MailSender; 5import org.springframework.mail.SimpleMailMessage; 6import org.springframework.stereotype.Service; 7 8import com.koikeya.project1.domain.model.MailForm; 9import com.koikeya.project1.domain.service.SendMailService; 10 11/** 12 * メール送信サービス実装クラス 13 */ 14@Service 15public class SendMailServiceImpl implements SendMailService { 16 17 /** 18 * MailSender 19 */ 20 @Autowired 21 private MailSender mailSender; 22 23 24 /* 25 * (非 Javadoc) 26 * @see com.koikeya.project1.domain.service.SendMailService#send(com.koikeya.project1.domain.model.MailForm) 27 */ 28 @Override 29 public void send(MailForm mailForm) { 30 SimpleMailMessage msg = new SimpleMailMessage(); 31 msg.setFrom(mailForm.getMail()); 32 msg.setTo("yuki.koike8527@gmail.com"); // 管理者アドレス 33 msg.setSubject("お問い合わせがありました"); 34 msg.setText(makeContent(mailForm)); 35 mailSender.send(msg); 36 } 37 38 /** 39 * コンテンツを編集する 40 * 41 * @param mailForm メールフォーム 42 * @return メールフォームの内容 43 */ 44 private String makeContent(MailForm mailForm){ 45 return "名前: " + mailForm.getName() + "\n" + 46 "性別: " + mailForm.getSex() + "\n" + 47 "問い合わせ内容: " + mailForm.getContent(); 48 } 49}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問