activation.jar mail.jar 파일 들이 필요한데,
http://java.sun.com/products/javamail/downloads/index.html 에서 받을 수 있다.
JavaBeans Activation Framework와 JavaMail 에 포함되어 있다.

이 두 파일은 톰캣의 common/lib나 JRE 등의 경로 내에 있어야 한다.

<%@page contentType = "text/html; charset=euc-kr" %>
<%@ page import="java.util.*,java.io.*,javax.mail.*,javax.mail.internet.*,javax.activation.*" %>
<%
// javamail lib 이 필요합니다.
class MyAuthentication extends Authenticator {
PasswordAuthentication pa;
public MyAuthentication(){
pa = new PasswordAuthentication("아이디", "비밀번호");
}

public PasswordAuthentication getPasswordAuthentication() {
return pa;
}
}

String subject = "Test"; //subject
String msgText = "메일 가니? \n안가니?\n"; //message
String host = "kornet.net"; //smtp mail server
String from = "zxcasd@zxcasd.pe.kr"; //sender email address
String to = "zxcasd12@gmail.com"; //receiver email address

Properties props = new Properties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth","true");

Authenticator auth = new MyAuthentication();
Session sess = Session.getInstance(props, auth);

try {
Message msg = new MimeMessage(sess);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setContent(msgText,"text/html; charset=euc-kr"); // HTML 형식
// msg.setText(msgText); // TEXT 형식

Transport.send(msg);
out.println("^_^");
} catch (MessagingException mex) {
out.println(mex.getMessage()+"<br>");
out.println("-_-;;.");
}

%>


서버에 따라 빨간 부분 바꿔주고, 전송할 내용 바꿔주면 된다.
(어떤 서버는 smtp 로긴 아이디를 이메일 통째로 입력해야 하는 경우도 있다.)

'Development > Java' 카테고리의 다른 글

CustomTypeHandler를 이용한 Mybatis ResultType 설정  (0) 2014.06.16
JSP의 내장객체  (0) 2014.06.04
SuppressWarnings  (0) 2014.06.02
[펌] jenkins로 서버에 자동 배포하기  (0) 2014.05.22
spring-framework Download  (0) 2014.05.22

+ Recent posts