Spring
Spring mail 보내기
pooney
2020. 1. 3. 00:04
web.xml(mail-context.xml를 읽어 빈등록)
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/*.xml
</param-value>
</context-param>
pom.xml
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context-support -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.mail/javax.mail-api -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>javax.mail-api</artifactId>
<version>1.5.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.sun.mail/javax.mail -->
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.5.3</version>
</dependency>
mail-context.xml (port를 465와 587이 존재하는데 587를 사용해야한다.)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host"><value>smtp.gmail.com</value></property>
<property name="port"><value>587</value></property>
<property name="username"><value>아이디@gmail.com</value></property>
<property name="password"><value>패스워드</value></property>
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.transport.protocol">smtp</prop>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.starttls.enable">true</prop>
<prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
<prop key="mail.debug">true</prop>
</props>
</property>
</bean>
</beans>
MailSendException: Mail server connection failed ~~~ 라는 아래와 같은 에러가 나오면 보안설정이나 , 방화벽, avast와 같은 백신으로 인하여 충돌이 나서 날 확율이 크니 백신을 끄거나 방화벽을 확인 해야한다. 이것도 안되면 (https://myaccount.google.com/lesssecureapps) 링크로가서 허용을 한다
org.springframework.mail.MailSendException: Mail server connection failed; nested exception is javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465, response: -1. Failed messages: javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465, response: -1; message exception details (1) are:
Failed message 1:
Request processing failed; nested exception is org.springframework.mail.MailSendException: Mail server connection failed; nested exception is javax.mail.MessagingException: Could not convert socket to TLS;
is SSL false은 포트가 잘못 됬을 확률 이높다 구글은 ssl을 지원함으로 587로 바꾼다
DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 465, isSSL false
DEBUG SMTP: could not connect to host "smtp.gmail.com", port: 465, response: -1
controller(메시지를 보낸다)
String to="blue672@naver.com";
String subject = "안녕";
String body = "이야 최고";
/* html형식으로 메일을 보내고 싶은경우 setText(body,true)를 해야한다
String body = "<html>\n" + "\n" + "<head>\n" + "\n" + "</head>\n" + "\n" +
"<body>\n" + "\n" +
"<img src=\"http://comshop.kro.kr/computer/images/hard.jpg\"/>\n" + "\n" +
"</body>\n" + "\n" + "</html>";
*/
try {
System.out.println("메시지 전송시작2");
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper messageHelper = new MimeMessageHelper(message, true, "UTF-8");
messageHelper.setFrom("aoaoaoal329@gmail.com");
messageHelper.setSubject(subject); //제목
messageHelper.setTo(to); //받는이
messageHelper.setText(body); //내용
// messageHelper.setText(body,true); //html 형식으로 보내는 경우 반드시 true해야한다
mailSender.send(message);
System.out.println("메시지 전송완료");
} catch (Exception e) {
System.out.println("에러남");
e.printStackTrace();
}
avast와 같은 백신을 반드시 꺼야한다 아니면 블루스크린이 뜬다.