IT/development

[spring] egovframework(4.1 ver) 단순 메일 발송

알 수 없는 사용자 2023. 12. 19. 08:14
반응형

목차

    pom.xml 

    <!-- Email -->
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-email</artifactId>
        <version>1.5</version>
    </dependency>
    <dependency>
        <groupId>egovframework.com.ems</groupId>
        <artifactId>sndng-mail</artifactId>
        <version>1.0</version>
    </dependency>

    context-mail.xml

    <?xml version="1.0" encoding="UTF-8"?> 
    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns:p="http://www.springframework.org/schema/p"  
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
    
        <bean id="EMSMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl" 
            p:host="smtp.daum.net"
            p:port="465"
            p:protocol="smtps"
            p:username="id" 
            p:password="password">
            <property name="javaMailProperties">
                <props>
                    <prop key="mail.smtps.auth">true</prop>
                    <prop key="mail.smtps.startls.enable">true</prop>
                    <prop key="mail.smtps.debug">true</prop>
                </props>
            </property>
        </bean>
    
    </beans>

    SndngMailVO

    package egovframework.test.com.cop.ems.dto;
    import lombok.Data;
    
    @Data
    public class SndngMailVO {
    
        /** 메세지ID */
        private String mssageId;
        /** 발신자 */
        private String dsptchPerson;
        /** 수신자 */
        private String recptnPerson;
        /** 제목 */
        private String sj;
        /** 발송결과코드 */
        private String sndngResultCode;
        /** 메일내용 */
        private String emailCn;
        /** 첨부파일ID */
        private String atchFileId;
        /** 첨부파일경로 */
        private String fileStreCours;
        /** 첨부파일이름 */
        private String orignlFileNm;
        /** 발신일자 */
        private String sndngDe;
        /** 첨부파일ID 리스트 */
        private String atchFileIdList;
        /** 발송요청XML내용 */
        private String xmlContent;
        /** 팝업링크여부(Y/N) */
        private String link;
    }

    Service

    package egovframework.test.com.cop.ems.service;
    
    import egovframework.test.com.cop.ems.dto.SndngMailVO;
    
    public interface SndngMailService {
    
    	/**
    	 * 단순 텍스트 메일 발송
    	 * @param vo
    	 * @return
    	 * @throws Exception
    	 */
    	boolean sndSimpleMail(SndngMailVO vo) throws Exception;
    }

    ServiceImpl

    package egovframework.test.com.cop.ems.service.impl;
    
    import egovframework.test.com.cop.ems.dto.SndngMailVO;
    import egovframework.test.com.cop.ems.service.SndngMailService;
    import org.egovframe.rte.fdl.cmmn.EgovAbstractServiceImpl;
    import org.springframework.mail.SimpleMailMessage;
    import org.springframework.mail.javamail.JavaMailSender;
    import org.springframework.stereotype.Service;
    
    import javax.annotation.Resource;
    import javax.mail.internet.InternetAddress;
    
    @Service("egovSndngMailService")
    public class SndngMailServiceImpl extends EgovAbstractServiceImpl implements SndngMailService {
        
        // 단순 텍스트 메일 발송
        @Resource(name = "EMSMailSender") // context-mail.xml의 bean id
        private JavaMailSender mailSender;
    
        /**
         * 단순 텍스트 메일 발송
         * @param vo
         * @return
         * @throws Exception
         */
        @Override
        public boolean sndSimpleMail(SndngMailVO vo) throws Exception {
    		//이걸 호출하는 클래스에서 SndngMailVO 세팅해야 함
            String recptnPerson = (vo.getRecptnPerson() == null) ? "" : vo.getRecptnPerson();          // 수신자
            String subject = (vo.getSj() == null) ? "" : vo.getSj();                                   // 메일제목
            String emailCn = (vo.getEmailCn() == null) ? "" : vo.getEmailCn();                         // 메일내용
    
            SimpleMailMessage msg = new SimpleMailMessage();
            msg.setFrom(String.valueOf(new InternetAddress("system@test.kr","관리자", "UTF-8")));
            msg.setTo(recptnPerson);
            msg.setSubject(subject);
            msg.setText(emailCn);
            mailSender.send(msg);
    
            return true;
        }
    }

     

    반응형