package nlib.mail;
 
import java.io.Reader;
import java.util.Properties;

import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;

import org.apache.ibatis.io.Resources;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import nlib.mail.service.EmailVO;
 
 
@Component
public class EmailSender  {
     
    @Autowired
    protected JavaMailSender  mailSender;
 
    public void SendEmail(EmailVO email) throws Exception {
    	String resource = "egovframework/egovProps/globals.properties";
        Properties properties = new Properties();
    	
        Reader reader = Resources.getResourceAsReader(resource);
        properties.load(reader);
        
        String senderName = properties.getProperty("system.notification.email.sender.name");
        String sender = properties.getProperty("system.notification.email.sender");
        
        MimeMessage msg = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(msg, true, "utf-8");
        helper.setText(email.getContent(), true);	//***HTML 적용
		helper.setTo(email.getReciver());
		helper.setSubject(email.getSubject());
		helper.setFrom(sender,senderName);

        mailSender.send(msg); 
    }
}