반응형
MailEx01
MailEx01.WebContent.mail
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0">
<title>Insert title here</title>
</head>
<body>
<form action="./mail_ok.jsp" method="post">
이메일 주소 <input type="text" name="mail"><br /><br />
이름 <input type="text" name="name"><br /><br />
제목 <input type="text" name="subject"><br /><br />
내용 <br /><textarea name="content" cols="18" rows="6"></textarea>
<br /><br />
<input type="submit" value="메일보내기" />
</form>
</body>
</html>
MailEx01.WebContent.mail_ok
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="mail.MailSender" %>
<%
request.setCharacterEncoding("utf-8");
String mail = request.getParameter("mail");
String name = request.getParameter("name");
String subject = request.getParameter("subject");
String content = request.getParameter("content");
MailSender mailSender = new MailSender();
mailSender.sendMail(mail, name, subject, content);
out.println("전송이 완료되었습니다.");
%>
MailEx01.mail.MyAuth
package mail;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
public class MyAuth extends Authenticator {
private String fromMail;
private String password;
public MyAuth(String fromMail, String password) {
this.fromMail = fromMail;
this.password = password;
}
@Override
protected PasswordAuthentication getPasswordAuthentication() {
// TODO Auto-generated method stub
return new PasswordAuthentication(fromMail, password);
}
}
MailEx01.mail.MailSender
package mail;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class MailSender {
private String fromEmail = "bapjoo2"; // 구글 아이디
private String password = "qkrwlgh1@#"; // 구글 패스워드
public void sendMail(
String toEmail, String toName,
String subject, String content) {
try {
Properties props = new Properties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
MyAuth auth = new MyAuth(fromEmail, password);
Session session = Session.getDefaultInstance(props, auth);
MimeMessage msg = new MimeMessage(session);
msg.setHeader("Content-type", "text/plain;charset=utf-8");
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmail, toName, "utf-8"));
msg.setSubject(subject);
msg.setContent(content, "text/plain;charset=utf-8");
msg.setSentDate(new java.util.Date());
Transport.send(msg);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
위 빨간박스안을 클릭하면 메일이 보내지게 하는 방법
위 방법으로 하면 좀 복잡해서 우측 하단에 메일 보내기 버튼을 생성하여 하는 방법으로 진행함
MailEx01.board2.board_mail1
<%@page import="model1.BoardDAO"%>
<%@page import="model1.BoardTO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("utf-8");
String seq = request.getParameter("seq");
String mail = request.getParameter("mail");
BoardTO to = new BoardTO();
to.setSeq(seq);
to.setMail(mail);
%>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0">
<title>Insert title here</title>
</head>
<body>
<form action="./board_mail1_ok.jsp" method="post">
<input type="hidden" name="seq" value="<%=seq %>" />
이메일 주소 <input type="text" name="mail" value="<%=mail %>"><br /><br />
이름 <input type="text" name="name"><br /><br />
제목 <input type="text" name="subject"><br /><br />
내용 <br /><textarea name="content" cols="18" rows="6"></textarea>
<br /><br />
<input type="submit" value="메일보내기" />
</form>
</body>
</html>
MailEx01.board2.board_mail1_ok
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="mail.MailSender" %>
<%
request.setCharacterEncoding("utf-8");
String mail = request.getParameter("mail");
String name = request.getParameter("name");
String subject = request.getParameter("subject");
String content = request.getParameter("content");
MailSender mailSender = new MailSender();
mailSender.sendMail(mail, name, subject, content);
out.println("전송이 완료되었습니다.");
%>
MailEx01.board2.board_view1
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="model1.BoardTO" %>
<%@ page import="model1.BoardDAO" %>
<%
request.setCharacterEncoding("utf-8");
String seq = request.getParameter("seq");
BoardTO to = new BoardTO();
to.setSeq(seq);
BoardDAO dao = new BoardDAO();
to = dao.boardView(to);
String subject = to.getSubject();
String writer = to.getWriter();
String wdate = to.getWdate();
String hit = to.getHit();
String content = to.getContent();
String mail = to.getMail();
%>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Insert title here</title>
<link rel="stylesheet" type="text/css" href="../css/board_view.css">
</head>
<body>
<!-- 상단 디자인 -->
<div class="con_title">
<h3>게시판</h3>
<p>HOME > 게시판 > <strong>게시판</strong></p>
</div>
<div class="con_txt">
<div class="contents_sub">
<!--게시판-->
<div class="board_view">
<table>
<tr>
<th width="10%">제목</th>
<td width="60%"><%= subject %></td>
<th width="10%">등록일</th>
<td width="20%"><%= wdate %></td>
</tr>
<tr>
<th>글쓴이</th>
<td><%= writer %><%= '(' + mail + ')' %></td>
<th>조회</th>
<td><%= hit %></td>
</tr>
<tr>
<td colspan="4" height="200" valign="top" style="padding: 20px; line-height: 160%"><%= content %></td>
</tr>
</table>
</div>
<div class="btn_area">
<div class="align_left">
<button type="button" class="btn_list btn_txt02" style="cursor: pointer;" onclick="location.href='board_list1.jsp'">목록</button>
</div>
<div class="align_right">
<button type="button" class="btn_list btn_txt02" style="cursor: pointer;" onclick="location.href='board_mail1.jsp?seq=<%= seq %>'">메일보내기</button>
<button type="button" class="btn_list btn_txt02" style="cursor: pointer;" onclick="location.href='board_modify1.jsp?seq=<%= seq %>'">수정</button>
<button type="button" class="btn_list btn_txt02" style="cursor: pointer;" onclick="location.href='board_delete1.jsp?seq=<%= seq %>'">삭제</button>
<button type="button" class="btn_write btn_txt01" style="cursor: pointer;" onclick="location.href='board_write1.jsp'">쓰기</button>
</div>
</div>
<!--//게시판-->
</div>
</div>
<!-- 하단 디자인 -->
</body>
</html>
MailEx01.model1.BoardDAO
public BoardTO boardMailOk(BoardTO to) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = datasource.getConnection();
// 조회수
String sql = "select mail from board1 where seq=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, to.getSeq());
rs = pstmt.executeQuery();
if(rs.next()) {
to.setMail(rs.getString("mail"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if(rs != null) try {rs.close();} catch(SQLException e) {};
if(pstmt != null) try {pstmt.close();} catch(SQLException e) {};
if(conn != null) try {conn.close();} catch(SQLException e) {};
}
return to;
}
위를 추가해준다
반응형
'Web & Mobile > JSP' 카테고리의 다른 글
Lecture 60 - JSP(18) JSTL, 우편번호검색 (0) | 2019.06.12 |
---|---|
Lecture 59 - JSP(17) EL, <%= %>, EL을 Model1 게시판에 적용, JSTL (0) | 2019.06.11 |
Lecture 57 - JSP(15) MVC Model2 기반 게시판(2) (0) | 2019.06.05 |
Lecture 56 - JSP(14) MVC Model2 기반 게시판(1) (0) | 2019.06.04 |
Lecture 55 - JSP(13) annotation, filter, 한글 encoding, decoding, 로그인 검사 필터, ServletContextListener, MVC 패턴 (0) | 2019.06.03 |
댓글