JUnit Smtp 테스트 코드

import java.util.Properties;

import javax.mail.Authenticator;
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.mail.internet.MimeMessage.RecipientType;

import org.junit.Before;
import org.junit.Test;


public class SmtpSend {

private String userid;
private String password;
private String email;
private String subject;
private String contents;
private String rcvEmail;
private Properties properties;

@Before
public void setUp() {
String host = "gw.konantech.com";

properties = new Properties();
properties.put("mail.smtp.host", host);
properties.put("mail.smtp.socketFactory.port", "25");
properties.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.port", "25");

userid = "???@konantech.com";
password = "???;
email = "???@konantech.com";
subject = "test subject";
contents = "test message";
rcvEmail = "???@konantech.com";

}

@Test
public void sendEmail() {
try{
Authenticator authenticator = new Authenticator() {
private PasswordAuthentication authentication;
{
authentication = new PasswordAuthentication(userid, password);
}

protected PasswordAuthentication getPasswordAuthentication() {
return authentication;
}
};

Session session = Session.getDefaultInstance(properties, authenticator);

Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(email));
message.addRecipient(RecipientType.TO, new InternetAddress(rcvEmail));

message.setSubject(subject);
message.setContent(contents, "text/plain");

Transport.send(message);
} catch(Exception e) {
e.printStackTrace();
}
}
}

댓글

이 블로그의 인기 게시물

Spring Batch - JobParameters Default Value 만들기

Hsqldb Server 재기동 시 오류