본문 바로가기
IT관련/JAVA_자유 노트

MailService 코드

by XoX 2020. 7. 22.
728x90
개인적인 메모를 위해 작성된 글이므로 가벼운 참고용으로 봐주세요.

 

package com.sbs.java.blog.service;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

public class MailService {
	Session session;
	final String username;
	final String password;
	
	public MailService(String mailId, String mailPw) {
		this.username = mailId;
		this.password = mailPw;
	}
	
	public void mailStart() {
		Properties props = new Properties();
		props.put("mail.smtp.user", username);
		props.put("mail.smtp.password", password);
		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.port", "25");
		props.put("mail.debug", "true");
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.starttls.enable", "true");
		props.put("mail.smtp.EnableSSL.enable", "true");
		props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
		props.setProperty("mail.smtp.socketFactory.fallback", "false");
		props.setProperty("mail.smtp.port", "465");
		props.setProperty("mail.smtp.socketFactory.port", "465");

		this.session = Session.getInstance(props, new javax.mail.Authenticator() {
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(username, password);
			}
		});
	}

	public void sendWelcomeMail(String memberEmail, String title, String body) {
		this.mailStart();
		try {
			Message message = new MimeMessage(session);
			message.setFrom(new InternetAddress("ginitium@gmail.com"));
			message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(memberEmail));
			message.setSubject(String.format("%s", title));
			message.setContent(String.format("%s", body),"text/html; charset=utf-8");// 글내용을 html타입 charset설정
			Transport.send(message);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void sendPwInfoMail(String memberEmail, String title, String tempPw) {
		this.mailStart();
		try {
			Message message = new MimeMessage(session);
			message.setFrom(new InternetAddress("ginitium@gmail.com"));
			message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(memberEmail));
			message.setSubject(String.format("%s", title));
			message.setContent("임시비밀번호: "+String.format("%s", tempPw),"text/html; charset=utf-8");// 글내용을 html타입 charset설정
			Transport.send(message);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
728x90
반응형

댓글