์ฌ์ง ์ถ์ฒ
package com.koreait.board4.common;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
public class MailAuth extends Authenticator{
PasswordAuthentication pa;
public MailAuth() {
String mail_id = "goonturtle4u";
String mail_pw = "wkdguswls123";
pa = new PasswordAuthentication(mail_id, mail_pw);
}
public PasswordAuthentication getPasswordAuthentication() {
return pa;
}
}
package com.koreait.board4.common;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class MailSendPW {
public static void pwMailSend(HisVo hisVo) {
Properties prop = System.getProperties();
// ๋ก๊ทธ์ธ์ TLS๋ฅผ ์ฌ์ฉํ ๊ฒ์ธ์ง ์ค์
prop.put("mail.smtp.starttls.enable", "true");
// ์ด๋ฉ์ผ ๋ฐ์ก์ ์ฒ๋ฆฌํด์ค SMTP์๋ฒ
prop.put("mail.smtp.host", "smtp.gmail.com");
// SMTP ์๋ฒ์ ์ธ์ฆ์ ์ฌ์ฉํ๋ค๋ ์๋ฏธ
prop.put("mail.smtp.auth", "true");
// TLS์ ํฌํธ๋ฒํธ๋ 587์ด๋ฉฐ SSL์ ํฌํธ๋ฒํธ๋ 465์ด๋ค.
prop.put("mail.smtp.port", "587");
// soket๋ฌธ์ ์ protocol๋ฌธ์ ํด๊ฒฐ
prop.put("mail.smtp.ssl.trust", "smtp.gmail.com");
prop.put("mail.smtp.socketFactory.fallback", "false");
prop.put("mail.smtp.ssl.protocols", "TLSv1.2");
Authenticator auth = new MailAuth();
Session session = Session.getDefaultInstance(prop, auth);
MimeMessage msg = new MimeMessage(session);
try {
// ๋ณด๋ด๋ ๋ ์ง ์ง์
msg.setSentDate(new Date());
// ๋ฐ์ก์๋ฅผ ์ง์ ํ๋ค. ๋ฐ์ก์์ ๋ฉ์ผ, ๋ฐ์ก์๋ช
msg.setFrom(new InternetAddress("goonturtle4u@gmail.com", "๋๋์ผ๊ฑฐ๋ถ์ด"));
// ์์ ์์ ๋ฉ์ผ์ ์์ฑํ๋ค.
InternetAddress to = new InternetAddress(hisVo.getHisEmail());
// Message ํด๋์ค์ setRecipient() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ์์ ์๋ฅผ ์ค์ ํ๋ค. setRecipient() ๋ฉ์๋๋ก ์์ ์, ์ฐธ์กฐ,
// ์จ์ ์ฐธ์กฐ ์ค์ ์ด ๊ฐ๋ฅํ๋ค.
// Message.RecipientType.TO : ๋ฐ๋ ์ฌ๋
// Message.RecipientType.CC : ์ฐธ์กฐ
// Message.RecipientType.BCC : ์จ์ ์ฐธ์กฐ
msg.setRecipient(Message.RecipientType.TO, to);
String gender = "";
if (hisVo.getHisGender() == 0)
gender = "๋จ";
else
gender = "์ฌ";
String mailSubject = hisVo.getHisName() + "(" + hisVo.getHisId() + ")" + "(" + gender + ")"
+ "๋์ ๋น๋ฐ๋ฒํธ๋ฅผ ์๋ ค๋๋ฆฝ๋๋ค.";
String mailText = hisVo.getHisName() + "(" + hisVo.getHisId() + ")" + "(" + gender + ")"
+ "๋์ โ
์์โ
๋น๋ฐ๋ฒํธ๋ โ\n" + hisVo.getHisPw() + "\nโ ์ด๋๋๋ค~^^";
// ๋ฉ์ผ์ ์ ๋ชฉ ์ง์
msg.setSubject(mailSubject, "UTF-8");
// Transport๋ ๋ฉ์ผ์ ์ต์ข
์ ์ผ๋ก ๋ณด๋ด๋ ํด๋์ค๋ก ๋ฉ์ผ์ ๋ณด๋ด๋ ๋ถ๋ถ์ด๋ค.
msg.setText(mailText, "UTF-8");
Transport.send(msg);
} catch (AddressException ae) {
System.out.println("AddressException : " + ae.getMessage());
} catch (MessagingException me) {
System.out.println("MessagingException : " + me.getMessage());
} catch (UnsupportedEncodingException e) {
System.out.println("UnsupportedEncodingException : " + e.getMessage());
}
}
}
@WebServlet("/searchPw")
public class SearchPwServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String user_Name = request.getParameter("user_Name");
int gender = MyUtils.getIntParam("gender", request);
String user_Email = request.getParameter("user_Email");
String user_Id = request.getParameter("user_Id");
int emptyCount = MyUtils.getIntParam("emptyCount", request);
int noPwCount = MyUtils.getIntParam("noPwCount", request);
// ๊ณต๋ฐฑํ์ธ
if (MyUtils.isEmpty(user_Name) || MyUtils.isEmpty(user_Email) || MyUtils.isEmpty(user_Id)) {
emptyCount++;
if (emptyCount <= 5) {
response.sendRedirect("searchLogin?err=searchPw&emptyCount=" + emptyCount + "&msg=smtEmpty");
return;
}
if (emptyCount > 5) {
response.sendRedirect("/user/join?msg=noUser");
return;
}
return;
}
HisVo hisVo = new HisVo();
hisVo.setHisName(user_Name);
hisVo.setHisGender(gender);
hisVo.setHisEmail(user_Email);
hisVo.setHisId(user_Id);
// hisVo ๊ฒ์ฌ - Pw์ฉ - boolean
boolean checkHisVo4Pw = HisDao.checkHisVo4IPw(hisVo);
// Pw์ฐพ๊ธฐ - ์ ๋ณด ์ผ์น
if (checkHisVo4Pw) {
//๋ฉ์ผ ๋ณด๋ด๊ธฐ
MailSendPW.pwMailSend(hisVo);
response.setContentType("text/html; charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<script>alert('์ด๋ฉ์ผ๋ก PW๋ฅผ ๋ณด๋ด๋๋ ธ์ด์~^^ ํ์ธํด๋ณด์ธ์~!!'); location.href='/';</script>");
out.close();
return;
}
// Pw์ฐพ๊ธฐ - ์ ๋ณด ๋ถ์ผ์น
if (!checkHisVo4Pw) {
noPwCount++;
if (noPwCount <= 5) {
response.sendRedirect("/searchLogin?err=searchPw&noPwCount=" + noPwCount + "&msg=noPw");
return;
}
if (noPwCount > 5) {
response.sendRedirect("/user/join?msg=noUser");
return;
}
return;
}
}
}
์ ๋ง ๊ฐ์ฌํด์ ์ฃผ์๊น์ง ์ธ์ธํ๊ฒ ๋ฌ์์ฃผ์ ์ ์๋ธ๋ฆฟ ๋ฐฉ์์ผ๋ก ๋ฉ์ผ์ ์ก ์ฑ๊ณตํ์ด์ ๊ณ์ tls ๋ฒ์ ๋ฌธ์ ๋ก ๊ณ ์ํ๋๋ฐ