pom.xml
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>$</version>
</dependency>
<!-- mail -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>javax.mail-api</artifactId>
<version>1.5.4</version>
</dependency>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.5.3</version>
</dependency>
대부분의 문서가 이 3개중에 javax.mail-api만 표기한 경우가 많았는데 3개가 모두 있어야 메일발송이 정상적으로 된다.
servlet-context.xml
<!-- mail bean -->
<beans:bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<beans:property name="host" value="smtp.gmail.com"/>
<beans:property name="port" value="25"/>
<beans:property name="username" value="gmailid@gmail.com"/>
<beans:property name="password" value="gmail password"/>
<beans:property name="javaMailProperties">
<beans:props>
<beans:prop key="mail.smtp.auth">true</beans:prop>
<beans:prop key="mail.smtp.starttls.enable">true</beans:prop>
<beans:prop key="mail.smtp.timeout">8500</beans:prop>
<beans:prop key="mail.debug">true</beans:prop>
<beans:prop key="mail.transport.protocol">smtp</beans:prop>
</beans:props>
</beans:property>
</beans:bean>
이건 흔한 gmail 설정이다.
service
@Autowired
private JavaMailSender mailSender;
{
MimeMessage message = mailSender.createMimeMessage();
message.setFrom(new InternetAddress(from));
message.addRecipient(RecipientType.TO, new InternetAddress(to));
message.setSubject(subject);
message.setText(text, "utf-8", "html");
mailSender.send(message);
}
기본 html형식의 메일발송법이다. 첨부파일은 해본 다음에 수정하겠다.
참고:
http://www.mkyong.com/java/java-lang-classnotfoundexception-com-sun-mail-util-messageremovedioexception/ 외