Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
Tags
- react
- R
- SQL
- Eclipse
- mybatis
- JavaScript
- SPC
- GIT
- mapreduce
- Sqoop
- IntelliJ
- es6
- Python
- MSSQL
- table
- tomcat
- Kotlin
- window
- 보조정렬
- hadoop
- SSL
- plugin
- NPM
- Android
- xPlatform
- Express
- Spring
- vaadin
- 공정능력
- Java
Archives
- Today
- Total
DBILITY
javamail gmail smtp error 본문
반응형
개발환경에 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);
}
}
}
반응형
'java > basic' 카테고리의 다른 글
java swing file open (0) | 2016.10.28 |
---|---|
maven jar build시 meta-inf 제거 (0) | 2016.10.21 |
maven jar packaging specific class excluding ( java 클래스 제외 ) (0) | 2016.10.03 |
java spring atomikos Connection Pool Exhausted Exception (0) | 2016.09.23 |
maven build scp copy (0) | 2016.09.20 |
Comments