mailapi.jar
activation.jar
smtp.jar
밑 3개는 꼭 필요한 것은 아니다
-> pop3.jar
-> imap.jar
-> mime.jar
차이점은 밑 사이트에서 비교!
https://sirius2k.tistory.com/49
package kakaoprotect;
import java.io.IOException;
import java.util.Properties;
import java.util.Random;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/SendCertification")
public class SendCertification extends HttpServlet {
private static final long serialVersionUID = 1L;
public SendCertification() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("dd");
//mail server 설정
String smtpEmail = "bogdudhkd123@gmail.com";
String password = "uregzrrzxkpfkcld";
//메일 받을 주소
String to_email = "bogdudhkd123@gmail.com";
//SMTP 서버 정보를 설정한다.
/* Properties p = new Properties(); */
Properties p = System.getProperties();
p.setProperty("mail.transport.protocol", "smtp");
/* p.setProperty("mail.host", "smtp.gmail.com"); */
p.put("mail.smtp.host", "smtp.gmail.com");
p.put("mail.smtp.port", "587");
p.put("mail.smtp.auth", "true");
p.put("mail.smtp.debug", "true");
p.put("mail.smtp.starttls.enable", "true");
p.put("mail.smtp.ssl.protocols", "TLSv1.2");
p.put("mail.smtp.socketFactory.port", "587");
p.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
//인증 번호 생성기
StringBuffer temp =new StringBuffer();
Random rnd = new Random();
for(int i=0;i<10;i++)
{
int rIndex = rnd.nextInt(3);
switch (rIndex) {
case 0:
// a-z
temp.append((char) ((int) (rnd.nextInt(26)) + 97));
break;
case 1:
// A-Z
temp.append((char) ((int) (rnd.nextInt(26)) + 65));
break;
case 2:
// 0-9
temp.append((rnd.nextInt(10)));
break;
}
}
String AuthenticationKey = temp.toString();
System.out.println(AuthenticationKey);
Session session = Session.getDefaultInstance(p, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(smtpEmail,password);
}
});
//email 전송
try {
MimeMessage msg = new MimeMessage(session);
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to_email));
System.out.println(msg);
//메일 제목
msg.setSubject("제목");
//메일 내용
msg.setText("인증 번호는 ["+temp +"] 입니다");
Transport t = session.getTransport("smtp");
t.connect(smtpEmail,password);
t.sendMessage(msg, msg.getAllRecipients());
t.close();
System.out.println("이메일 전송");
}catch (Exception e) {
e.printStackTrace();// TODO: handle exception
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}