자바 smtp 이너클래스 없이 $1 메일 보내기
요새는 자바 개발할때 스프링 프레임워크를 많이 사용해서 스프링에서 제공하는 api 이용하면 쉽게 보낼 수 있다.
기존방식으로 하면..
자바 smtp로 구글링 하면 대부분이
Session session = Session.getDefaultInstance(prop, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, password);
}
});
이런식으로 많이 되어 있다.
하지만..
컴파일시 이너클래스에 의해 $1.class가 본인도 모르게 만들어 진다.
이게 큰문제를 일으킨다..
리눅스 서버 에서는 $가 예약어 이다. 그래서 rm 처럼 삭제 명령어를 날리면 엉뚱하게 $1이 안붙은 파일이 삭제가 되어 장애를 일으킨다.
그냥
Session session = null;
session=session.getInstance(prop, null);
MimeMessage message=new MimeMessage(session);
이렇게 해도 되고
Session msgSession = null;
if(authMode.equals("true")) {
Authenticator auth = new MyAuthentication(mailId, mailPassword);
msgSession = Session.getInstance(mailProps, auth);
} else {
msgSession = Session.getInstance(mailProps, null);
}
class MyAuthentication extends Authenticator {
PasswordAuthentication pa;
public MyAuthentication(String mailId, String mailPass) {
pa = new PasswordAuthentication(mailId, mailPass);
}
public PasswordAuthentication getPasswordAuthentication() {
return pa;
}
}
이런식으로 이너클래스를 안쓰게 끔 코딩해야 한다.