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 리스트", "정인호");
}
}
java simple logging (0) | 2014.01.23 |
---|---|
JDBC 엑사데이터 RAC 접속 string (0) | 2013.11.29 |
oracle.xml.parser.v2 library namespace parse (0) | 2013.08.20 |
mybatis ORA-01861: literal does not match format string (0) | 2012.11.01 |
주(week)로 부터 날짜 리턴 (0) | 2012.10.25 |
댓글 영역