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

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

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

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Spring

Spring Framework は、Javaプラットフォーム向けのオープンソースアプリケーションフレームワークです。 Java Platform上に、 Web ベースのアプリケーションを設計するための拡張機能が数多く用意されています。

Q&A

解決済

1回答

6488閲覧

Springでのメール機能の実装について

heavyuseman

総合スコア42

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Spring

Spring Framework は、Javaプラットフォーム向けのオープンソースアプリケーションフレームワークです。 Java Platform上に、 Web ベースのアプリケーションを設計するための拡張機能が数多く用意されています。

0グッド

2クリップ

投稿2017/06/24 11:51

編集2017/07/01 07:26

いつもお世話になっております。
springframeworkでメール機能実装しており、質問があります。
下記の通りに

html

1First.html 2 <form action="/mailsend" method="POST"> 3 <button>メール送信</button> 4 </form>

でメール送信ボタンを押下した際、指定したメールアドレスにメールを送る機能を
実装しております。
しかし、メール送信ボタンを押下してもメールが送信されません。
原因が不明ですのでご教授宜しくお願いします。
下記がメール送信するためのjavaソースとなります。

java

1Mailsend.java 2package com.tuyano.springboot.mail; 3 4import org.springframework.beans.factory.annotation.Autowired; 5import org.springframework.beans.factory.annotation.Value; 6import org.springframework.mail.SimpleMailMessage; 7import org.springframework.stereotype.Component; 8import org.springframework.web.bind.annotation.RequestMapping; 9import org.springframework.web.bind.annotation.RequestMethod; 10import org.springframework.web.bind.annotation.RestController; 11import org.springframework.mail.MailSender; 12 13@RestController 14public class MailSend { 15//送信先のメールアドレス 16@Value("${spring.mail.username}") 17private String mailFrom; 18@Autowired 19MailSender mailSender; 20//メール送信メソッド 21@RequestMapping(path = "/mailsend", method = RequestMethod.POST) 22public void sendMail(){ 23SimpleMailMessage mailmsg = new SimpleMailMessage(); 24 mailmsg.setFrom(mailFrom); 25 mailmsg.setTo("送信するメールアドレス"); 26 mailmsg.setSubject("テストメール");//タイトルの設定 27 mailmsg.setText("Spring Boot より本文送信"); //本文の設定 28 mailSender.send(mailmsg); 29} 30} 31

java

1Securityconfig.java 2package com.tuyano.springboot.springsecurity; 3 4import org.springframework.context.annotation.Bean; 5import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; 6import org.springframework.security.config.annotation.web.builders.HttpSecurity; 7import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; 8import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 9import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 10 11import javax.sql.DataSource; 12 13import org.springframework.beans.factory.annotation.Autowired; 14import org.springframework.beans.factory.annotation.Qualifier; 15 16 17 18@EnableWebSecurity 19public class SecurityConfig extends WebSecurityConfigurerAdapter{ 20 @Autowired 21 private DataSource dataSource; 22 23 private static final String USER_QUERY="select name, password, 1 from Newaccount where name = ?"; 24 private static final 25 String ROLES_QUERY="select username, authority from AUTHORITIES where username = ?"; 26 27 @Override 28 protected void configure(HttpSecurity http) throws Exception{ 29 http 30 .authorizeRequests() 31 .antMatchers("/First.html").permitAll() 32 .antMatchers("/sucess.html").permitAll() 33 .antMatchers("/failure.html").permitAll() 34 .antMatchers("/mailsend.html").permitAll() 35 .antMatchers("/js/**").permitAll() 36 .antMatchers("/templates/**").hasAnyAuthority("ROLE_ADMIN") 37 .anyRequest().authenticated() 38 .and() 39 .formLogin() 40 .loginPage("/First.html") 41 .loginProcessingUrl("/processLogin") 42 .defaultSuccessUrl("/sucess.html") 43 .failureUrl("/failure.html") 44 .usernameParameter("name") 45 .passwordParameter("password") 46 .and() 47 .logout() 48 .logoutUrl("/processLogout") 49 .logoutSuccessUrl("/First.html") 50 .and() 51 .csrf() 52 .disable(); 53 54 } 55 @Override 56 public void configure(AuthenticationManagerBuilder auth) throws Exception { 57 auth.jdbcAuthentication() 58 .dataSource(dataSource) 59 .usersByUsernameQuery(USER_QUERY) 60 .authoritiesByUsernameQuery(ROLES_QUERY); 61 //passwordEncoder(new BCryptPasswordEncoder()); 62 //.authoritiesByUsernameQuery( 63 // "select mail_address, role from accounts where mail_address = ?"); 64 } 65 66 67} 68

html

1mailsend.html 2<!DOCTYPE html> 3<html xmlns:th="http://www.thymeleaf.org"> 4<head> 5<title>top page</title> 6<meta http-equiv="Content-Type" 7content="text/html" charset="UTF-8"/> 8 9<!-- css --> 10 11<style> 12 html { height: 100% } 13 body { height: 100%; margin: 0; padding: 0 } 14 #map { height: 100% } 15</style> 16</head> 17<body> 18<h1>メール送信完了</h1> 19 <form action="/mailsend" method="POST"> 20 <button>メール送信</button> 21 </form> 22</body> 23</html>

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

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

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

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

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

guest

回答1

0

ベストアンサー

メール送信のSMTP設定はお済みでしょうか。

https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html

Qiita:Spring-bootからメールを送信するまで

他にもメール送信したとしても、Fromアドレスが実在しないメールアドレスの場合や、Googleのアカウント設定にて「安全性の低いアプリの許可: 無効」にしていると、送信は完了してもブロックされます(これはエラーが出力されますが)

ちなみに、正しくメール送信完了→受信まで行えているapplication.ymlも添えますのでご確認ください。

spring: mail: host: smtp.gmail.com port: 587 username: メール送信に利用するGMAILアカウント password: 上記アカウントのパスワード properties: mail.smtp.auth: true mail.smtp.starttls.enable: true

投稿2017/06/24 12:25

編集2017/06/25 01:42
A-pZ

総合スコア12011

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

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

heavyuseman

2017/06/24 12:36

追記です。またmainが実行されると同時にメールが送信されることは確認できております
A-pZ

2017/06/25 01:40

回答を編集しました。
heavyuseman

2017/06/25 05:34

ちなみに、正しくメール送信完了→受信まで行えているapplication.ymlも添えますのでご確認ください。 →application.propertiesに記載していただいた設定情報を設定しましたが 送信ができていないです。 MailSend.javaにログを仕込んだのですが、ログが出ていませんでした。 MailSend.javaが呼ばれていませんでした。 MailSend.javaを呼び出すために@RestControllerを付与しているのですが 使い方として誤っているでしょうか?
A-pZ

2017/06/25 10:01

まず確認したいのが、 「mainが実行されると同時にメールが送信されることは確認できております」は送信されていなかった、であっていますでしょうか。 また、RestControllerで定義されていますので、HTTPリクエストで実行されていますか? @RequestMapping(path = "/mailsend", method = RequestMethod.POST) でメソッドが定義されているので、 http://127.0.0.1:8080/mailsend で実行されますが、HTTPメソッドはPOSTでないと動きません。
heavyuseman

2017/07/01 07:24

返信が遅れてしまい申し訳ありません。 「mainが実行されると同時にメールが送信されることは確認できております」は送信されていなかった、であっていますでしょうか。 →メールは正常に送信できていることを確認できております。 動作の期待値としましては ①First.htmlのメール送信ボタンを押下 ②Mailsend.javaの mailmsg.setTo("送信するメールアドレス"); にメールが送信され、mailsend.htmlに画面遷移する としております。 実際の結果は①を実施してもmailsend.htmlに画面遷移せず、 First.htmlからFirst.htmlに画面遷移します。 Securityconfig.javaに .antMatchers("/mailsend.html").permitAll() を設定したのですが、mailsend.htmlに画面遷移しません。 原因が不明ですのでご回答宜しくお願い致します。
mr-hisa-child

2017/07/04 04:08

コメントで失礼します。 ソースを見る限り、mailsend.htmlに遷移する実装が記述されていないようです。
A-pZ

2017/07/04 05:19

SpringSecurityの設定に不備があるのか、メール送信のMailSendが正しく呼ばれているかを確認するため、一度SpringSecurityの機能を外して、メール送信がMailSendから行われているかを確認した方が良いかと存じます。(質問内容が更新されており、以前まではSpringSecurityの設定は書かれていなかったと記憶しております)
heavyuseman

2017/07/09 02:00

>mr-hisa-child 返信が遅れてしまい申し訳ありません ご回答ありがとうございます。 mailsend.htmlの実装しました。
heavyuseman

2017/07/09 02:01

>A-pZ 返信が遅れてしまい申し訳ありません Springsecurityの機能を外したところメールを送信できました。 Springsecurityの機能の設定を調査して、設定した状態でも送れるように 修正しておきます。 ご回答ありがとうございました
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問