caTea 블로그

java mail sand example 본문

spring(java)

java mail sand example

ZaRas 2015. 9. 10. 16:23
반응형

<!-- Mail maven setting-->

<dependency>

       <groupId>javax.mail</groupId>

       <artifactId>mail</artifactId>

       <version>1.4.7</version>

  </dependency>



import java.util.Date;

import java.util.Properties;


import javax.mail.BodyPart;

import javax.mail.PasswordAuthentication;

import javax.mail.Session;

import javax.mail.Transport;

import javax.mail.internet.InternetAddress;

import javax.mail.internet.MimeBodyPart;

import javax.mail.internet.MimeMessage;

import javax.mail.internet.MimeMessage.RecipientType;

import javax.mail.internet.MimeMultipart;


import org.apache.log4j.Logger;


public class MailSendManger {

private Logger log = Logger.getLogger(MailSendManger.class);

private final String sendId = ""; 

private final String sendPwd = ""; 

public String send(String toMailAddr, String title, String content){

Properties prop = new Properties();

//-- gmail account 이용

prop.put("mail.smtp.host", "smtp.gmail.com");

prop.put("mail.smtp.starttls.enable","true");

prop.put("mail.transport.protocol", "smtp");

prop.setProperty("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");

prop.put("mail.smtp.port", "465");

prop.put("mail.smtp.auth", "true");

Session session = Session.getDefaultInstance(prop, new javax.mail.Authenticator() {

            protected PasswordAuthentication getPasswordAuthentication() {

                return new PasswordAuthentication(sendId, sendPwd);

            }

        });

session.setDebug(true); //for debug

try{ 

MimeMessage message = new MimeMessage(session);

            message.setSubject(title,"UTF-8");

            message.setSentDate(new Date());

            message.setRecipient(RecipientType.TO, new InternetAddress(toMailAddr)); //RecipientType.TO:수신자 RecipientType.CC:참조자 

            

            // This HTML mail have to 2 part, the BODY and the embedded image

            MimeMultipart multipart = new MimeMultipart("related");


            // first part  (the html)

            BodyPart messageBodyPart = new MimeBodyPart();

            messageBodyPart.setContent(content, "text/html;charset=utf-8");


            // add it

            multipart.addBodyPart(messageBodyPart);

            

            // second part (the image)

            // messageBodyPart = new MimeBodyPart();

            //DataSource fds = new FileDataSource(imgFile);

            //messageBodyPart.setDataHandler(new DataHandler(fds));

            //messageBodyPart.setHeader("Content-ID","imgFile");


            // add it

            //multipart.addBodyPart(messageBodyPart);


            // put everything together

            message.setContent(multipart);

Transport.send(message);

} catch (Exception e){

e.printStackTrace();

log.debug("============ Fail Send Mail ============");

return "false";

}

log.debug("============ Succes Send Mail ============");

return "succes";

}


}



728x90

'spring(java)' 카테고리의 다른 글

html convert pdf (html문서를 pdf 파일로 변환)  (0) 2016.05.11
http 데이터 송수신 클라이언트단  (0) 2016.01.29
java excel paser example  (0) 2015.09.10
apache ftpclient example  (0) 2015.09.10
Method 함수 사용해보자  (0) 2015.06.17