ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Spring] Mail 보내기
    기초/SPRING 2020. 9. 29. 14:47

    패스워드 분실시 메일로 인증번호를 발송하기 위해 프로시저 작성

     

    참고 블로그  (Gmail설정)

    heodolf.tistory.com/99

     

    pom.xml

    		
    		<!-- Mail -->
    		<dependency>
    			<groupId>javax.activation</groupId>
    			<artifactId>activation</artifactId>
    			<version>1.1</version>
    		</dependency>
    		
    		<dependency>
    			<groupId>javax.mail</groupId>
    			<artifactId>mail</artifactId>
    			<version>1.4.7</version>
    		</dependency>

     

    mail procedure (어디서 사용하든 상관 X)

    MailUrl : smtp.gmail.com

    Port : 587

     

    MimeMultiPart를 사용하는 이유 : 파일 첨부 가능

    public String sendMail(String userId) {
    		String result = "";
    		try {
    			MailVo mailVo = envDao.adminInfo(); //DB에서 정보를 가져옴
    
    			// from info
    			String FROM = mailVo.getMailId();
    			String FROMNAME = mailVo.getUserName();
    
    			// to info
    			String TO = userId;
    
    			// login
    			String SMTP_USERNAME = mailVo.getMailId();
    			String SMTP_PASSWORD = mailVo.getMailPw();
    
    			System.out.println(SMTP_USERNAME);
    			System.out.println(SMTP_PASSWORD);
    
    			// not use
    			String CONFIGSET = "ConfigSet";
    
    			// host info - gmail
    			String HOST = mailVo.getMailUrl();
    			int PORT = mailVo.getMailPort();
    
    			String SUBJECT = "안녕하세요. 홈페이지 관리자 "+ FROMNAME +"입니다.";
    
    			String CONTENT_TEXT = System.getProperty("line.separator") +
    					"<h4>안녕하세요. DG홈페이지에서 메일보내드립니다.</h4>" + "<br />" + "<br />" +
    					"Contents" +
    					"<br />" + "<br />" + "<h4>감사합니다.</h4>";
    
    
    			// Mail Info
    			Properties props = System.getProperties();
    			props.put("mail.transport.protocol", "smtp");
    			props.put("mail.smtp.port", PORT);
    			props.put("mail.smtp.starttls.enable", "true");
    			props.put("mail.smtp.auth", "true");
    
    			// Create a Session object to represent a mail session with the specified
    			// properties.
    			Session session = Session.getDefaultInstance(props);
    
    			// Create a message with the specified information.
    			MimeMessage msg = new MimeMessage(session);
    			msg.setFrom(new InternetAddress(FROM, FROMNAME));
    			msg.setRecipient(Message.RecipientType.TO, new InternetAddress(TO));
    			msg.setSubject(SUBJECT);
    
    			MimeBodyPart mbp = new MimeBodyPart();
    			mbp.setContent(CONTENT_TEXT, "text/html; charset=UTF-8");
    
    			Multipart mp = new MimeMultipart();
    			mp.addBodyPart(mbp);
    
    			msg.setContent(mp);
    
    			// Add a configuration set header. Comment or delete the
    			// next line if you are not using a configuration set
    			msg.setHeader("X-SES-CONFIGURATION-SET", CONFIGSET);
    
    			// Create a transport.
    			Transport transport = session.getTransport();
    
    			// Send the message.
    //				System.out.println("Sending...");
    
    			// Connect to Amazon SES using the SMTP username and password you specified
    			// above.
    			transport.connect(HOST, SMTP_USERNAME, SMTP_PASSWORD);
    
    			// Send the email.
    			transport.sendMessage(msg, msg.getAllRecipients());
    //				System.out.println("Email sent!");
    //				__LOG.info("");
    			// Close and terminate the connection.
    			transport.close();
    			result = "메일이 발송되었습니다.";
    		} catch (Exception e) {
    			result = "메일을 발송하는 도중 오류가 발생하였습니다.";
    			log.info(e.getMessage());
    		}
    
    		return result;
    	}

     

    MailVo

    public class MailVo {
    
    	private String mailId;
    	private String mailPw;
    	private String userName;
    	private String mailUrl;
    	private int mailPort;
    	private Date delYn;
    
    	public String getMailId() {
    		return mailId;
    	}
    	public void setMailId(String mailId) {
    		this.mailId = mailId;
    	}
    	public String getMailPw() {
    		return mailPw;
    	}
    	public void setMailPw(String mailPw) {
    		this.mailPw = mailPw;
    	}
    	public String getUserName() {
    		return userName;
    	}
    	public void setUserName(String userName) {
    		this.userName = userName;
    	}
    	public String getMailUrl() {
    		return mailUrl;
    	}
    	public void setMailUrl(String mailUrl) {
    		this.mailUrl = mailUrl;
    	}
    	public int getMailPort() {
    		return mailPort;
    	}
    	public void setMailPort(int mailPort) {
    		this.mailPort = mailPort;
    	}
    	public Date getDelYn() {
    		return delYn;
    	}
    	public void setDelYn(Date delYn) {
    		this.delYn = delYn;
    	}
    
    
    }

     

     

    '기초 > SPRING' 카테고리의 다른 글

    [Spring] 시큐어 코딩 가이드  (0) 2022.01.03
    전자정부 프레임워크 변경사항  (0) 2022.01.03
    [Spring] Login Session 추가  (0) 2020.09.28
    [Spring] Mysql 계층적 쿼리구조  (0) 2020.09.23
    [Spring] Maria DB 연결+SQL LOG  (0) 2020.09.09

    댓글

Designed by Tistory.