Spring 이메일 발송

Back-End/Spring 2019. 8. 7. 11:46
728x90
반응형

어제 구현한 이메일을 활용한 인증을 조금 수정해서 메인페이지에서 이메일을 발송할 수 있도록 기능을 추가함


받는 사람의 이메일 주소와 이메일 제목, 내용을 입력후 버튼을 누르면 이메일이 발송되도록 추가함



home.jsp (메인페이지)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
 <!-- 이메일 보내기 폼 -->
<form action ="e_mailForm.do" method = "post">
<table border = "1" align="right" width = "450" height = "250" >
 
<tr>
<td>
<center>
<br>
<span style="color: green; font-weight: bold;">이메일 보내기</span>
 </center>
    <ul>
      <li>보내는 사람 : <input type="text" name="sender_front" placeholder="이메일 아이디 입력">
      <select type = "text" name = "sender_back"  >
              
                  <option value = "@naver.com">@naver.com</option>                
                  <option value = "@hanmail.net">@hanmail.net</option>
                  <option value = "@gmail.com">@gmail.com</option>
      
      </select>
      </li>
      <br>
      
      
      <li>받는 사람 : <input type="text" name = "recipient_front" placeholder="이메일 아이디 입력">
      <select name="recipient_back" type="text" >
      
                  <option value = "@naver.com">@naver.com</option>                
                  <option value = "@hanmail.net">@hanmail.net</option>
                  <option value = "@gmail.com">@gmail.com</option>
                  <option value = "@chol.com">@chol.com</option>                
                  <option value = "@empal.com">@empal.com</option>
                  <option value = "@freechal.com">@freechal.com</option>
                  <option value = "@hanmir.com">@hanmir.com</option>                
                  <option value = "@hitel.net">@hitel.net</option>
                  <option value = "@nete.com">@nate.com</option>
                  
            
      </select>
      </li>
      <br>
      
      
      <li>제목 : <input type="text" name="title" placeholder=" 이메일의 제목 입력"/></li><br>
      <li>내용 : <textarea name="text" name = "text" placeholder=" 보낼 내용 입력 "></textarea>    </li><br>
      <center>
      <button type = "submit" name = "submit" >이메일 전송</button>
      </center>
    </ul>
 
    </td>
    </tr>
    </table>
  </form>
 
cs



MemberController.java (컨트롤러 중 일부)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// mailSending 코드 (메인페이지에서 메일을 보낼때 맵핑되는 메소드)
            @RequestMapping(value = "e_mailForm.do" , method=RequestMethod.POST )
            public ModelAndView main_mailSending(HttpServletRequest request, String sender_front, String sender_back, 
            String recipient_front, String recipient_back, String title, String text, HttpServletResponse response_email) throws IOException {
                
        
                String setfrom = request.getParameter("sender_front")+request.getParameter("sender_back");            //보내는 사람 이메일
                String tomail = request.getParameter("recipient_front")+request.getParameter("recipient_back");     // 받는 사람 이메일
                String mail_title = request.getParameter("title");                                                     // 제목
                String content = request.getParameter("text");                                                        //이메일 내용
                
                System.out.println(setfrom);         //값이 잘 담겼는지 테스트
                System.out.println(tomail);
                System.out.println(mail_title);
                System.out.println(content);
                
                
                try {
                    MimeMessage message = mailSender.createMimeMessage();
                    MimeMessageHelper messageHelper = new MimeMessageHelper(message,
                            true"UTF-8");
 
                    messageHelper.setFrom(setfrom); // 보내는사람 생략하면 정상작동을 안함
                    messageHelper.setTo(tomail); // 받는사람 이메일
                    messageHelper.setSubject(mail_title); // 메일제목은 생략이 가능하다
                    messageHelper.setText(content); // 메일 내용
                    
                    mailSender.send(message);
                } catch (Exception e) {
                    System.out.println(e);
                }
                
                ModelAndView mv = new ModelAndView();    //ModelAndView로 보낼 페이지를 지정하고, 보낼 값을 지정한다.
                mv.setViewName("home");     //뷰의이름
 
 
                //이메일이 발송될때 자바스크립트로 발송되었다고 출력시킴
                response_email.setContentType("text/html; charset=UTF-8");
                PrintWriter out_email = response_email.getWriter();
                out_email.println("<script>alert('이메일이 발송되었습니다.');</script>");
                out_email.flush();
                
                
                return mv;
                
            }
cs




728x90
반응형
: