DBILITY

javamail gmail smtp error 본문

java/basic

javamail gmail smtp error

DBILITY 2016. 9. 20. 16:34
반응형

개발환경에 avast antivirus사용시 Could not convert socket to TLS...오류가 발생하였습니다.
avast를 잠시 중지하고 오류나는 app를 실행하면 정상동작 합니다.

 

gmail smtp설정시 property

mailProps = new Properties();
		
mailProps.put("mail.smtp.auth", "true");
mailProps.put("mail.smtp.starttls.enable", "true");
mailProps.put("mail.smtp.host", "smtp.gmail.com");
mailProps.put("mail.smtp.port", "587");
//mailProps.put("mail.smtp.debug", "true");   
//mailProps.put("mail.smtps.ssl.checkserveridentity", "false"); // 없어도 됨
mailProps.put("mail.smtp.ssl.trust", "smtp.gmail.com"); //PKIX path building failed 제거

CommonMailService.java

package com.dbility.batch.cmm;

import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
 * Description
 * 
 * 
 * @author hyperrookie@gmail.com
 * 
 * @version 1.0.0
 * @date 2015. 11. 24.
 */

public class CommonMailService {

	private static final Logger LOG = LoggerFactory
			.getLogger(CommonMailService.class);

	private Properties mailProps = null;

	private String from;
	private String smtpServer;
	private String smtpPort = "9999";
	private String smtpUser;
	private String smtpPassword;
	private String contentType = "text/html; charset=UTF-8";

	public void setFrom(String from) {
		this.from = from;
	}

	public void setSmtpServer(String smtpServer) {
		this.smtpServer = smtpServer;
	}

	public void setSmtpPort(String smtpPort) {
		this.smtpPort= smtpPort;
	}

	public void setSmtpUser(String smtpUser) {
		this.smtpUser = smtpUser;
	}

	public void setSmtpPassword(String smtpPassword) {
		this.smtpPassword = smtpPassword;
	}
	/**
	 * 생성자 설명
	 * 
	 * 
	 */
	public CommonMailService() {
	}

	public int sendMail(String to, String subject, String content) {
		int ret = 1;
		mailProps = new Properties();
		
		mailProps.put("mail.smtp.host", this.smtpServer);
		mailProps.put("mail.smtp.port", this.smtpPort);
		mailProps.put("mail.smtp.auth", "false");
		mailProps.put("mail.smtp.starttls.enable", "true");
		mailProps.put("mail.debug", "false");

		try {

			EmailAuthenticator authenticator = new EmailAuthenticator(
					this.smtpUser, this.smtpPassword);

			Session session = Session.getInstance(mailProps, authenticator);

			Message msg = new MimeMessage(session);

			msg.setFrom(new InternetAddress(this.from));
			// Recipients
			msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to));
			//msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
			msg.setSubject(subject);
			msg.setContent(content, this.contentType);
			msg.setSentDate(new Date());
	           
	        Transport.send(msg);

			LOG.debug("mail Send OK");

		} catch (Exception e) {
			ret = 0;
			LOG.debug("mail Send ERROR");
			e.printStackTrace();
		}
		
		return ret;
	}

	class EmailAuthenticator extends Authenticator {

		private String id;
		private String pw;

		public EmailAuthenticator(String id, String pw) {
			this.id = id;
			this.pw = pw;
		}

		protected PasswordAuthentication getPasswordAuthentication() {
			return new PasswordAuthentication(id, pw);
		}
	}
}
반응형
Comments