상세 컨텐츠

본문 제목

gmail을 이용한 java mail sender 프로그램

소프트웨어/java

by 야솔아빠 2013. 11. 11. 16:01

본문

반응형


dsn.jar


gimap.jar


imap.jar


mailapi.jar


pop3.jar


smtp.jar

gmail을 이용한 java mail sender 프로그램


관련 라이브러리 필요(다운받으세요)


import java.io.UnsupportedEncodingException;


import java.util.Properties;

 

import javax.mail.Message;

import javax.mail.MessagingException;

import javax.mail.PasswordAuthentication;

import javax.mail.Session;

import javax.mail.Transport;

import javax.mail.internet.InternetAddress;

import javax.mail.internet.MimeMessage;

 

public class SendMailFromGmail {

  Session session = null;

  

  public SendMailFromGmail() {

    final String username = "inhojung502@gmail.com";

    final String password = "암호넣을것";

    

    Properties props = new Properties();

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

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

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

    props.put("mail.smtp.port", "587");

//    props.put("mail.smtp.port", "465");

    props.put("mail.smtp.ssl.trust", "smtp.gmail.com");

        props.put("mail.debug", "true");

    

    

    this.session = Session.getInstance(props,

      new javax.mail.Authenticator() {

      protected PasswordAuthentication getPasswordAuthentication() {

        return new PasswordAuthentication(username, password);

      }

      });

  }

 

public void sendMail(String subject, String content) throws UnsupportedEncodingException {

  try {

 

 InternetAddress[] recieveAddress = new InternetAddress[2];

    recieveAddress[0] = new InternetAddress("david.jung@oracle.com");

 recieveAddress[1] = new InternetAddress("inhojung502@gmail.com");

 

 

 

      

 MimeMessage msg = new MimeMessage(session);

 msg.setHeader("Content-Type", "text/html; charset=UTF-8");

      msg.setFrom(new InternetAddress("inhojung502@gmail.com", "정인호"));

 msg.setRecipients(Message.RecipientType.TO, recieveAddress);

      msg.setSubject(subject, "UTF-8");

      msg.setText(content, "UTF-8");


 Transport.send(msg);

      

 

System.out.println("e-mail send");

 

} catch (MessagingException e) {

throw new RuntimeException(e);

}

}

  

  public static void main(String[] args) throws UnsupportedEncodingException {

    SendMailFromGmail mail = new SendMailFromGmail();

    mail.sendMail("PNR 리스트", "정인호");

  }

}




반응형

관련글 더보기

댓글 영역