๐ŸŽฎ [JSP] | [EMAIL ๊ธฐ๋Šฅ] | JAVA๋กœ ์ด๋ฉ”์ผ ๋ณด๋‚ด๊ธฐ

1
post-thumbnail

์‚ฌ์ง„ ์ถœ์ฒ˜


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;
		}
	}

}
profile
๋ช‡ ๋ฒˆ์„ ๋„˜์–ด์ ธ๋„ ์•ž์œผ๋กœ ๊ณ„์† ๋‚˜์•„๊ฐ€์ž

1๊ฐœ์˜ ๋Œ“๊ธ€

comment-user-thumbnail
2021๋…„ 12์›” 8์ผ

์ •๋ง ๊ฐ์‚ฌํ•ด์š” ์ฃผ์„๊นŒ์ง€ ์„ธ์„ธํ•˜๊ฒŒ ๋‹ฌ์•„์ฃผ์…”์„œ ์„œ๋ธ”๋ฆฟ ๋ฐฉ์‹์œผ๋กœ ๋ฉ”์ผ์ „์†ก ์„ฑ๊ณตํ–ˆ์–ด์š” ๊ณ„์† tls ๋ฒ„์ „ ๋ฌธ์ œ๋กœ ๊ณ ์ƒํ–ˆ๋Š”๋ฐ

๋‹ต๊ธ€ ๋‹ฌ๊ธฐ