Java Mail/HTML Contents
HTML Contents with JavaMail
Soul-Learner
2010. 2. 2. 20:40
JavaMail에 HTML 과 이미지를 포함하는 예
package org.dodream.java.mail; import javax.mail.*; import javax.mail.internet.*; import javax.activation.*; import java.io.*; import java.util.*; import java.security.Security; public class JavaMailTest { public static void main(String[] args) { Properties p = new Properties(); p.put("mail.smtp.user", Auth.email_addr); // Google계정@gmail.com으로 설정 p.put("mail.smtp.host", "smtp.gmail.com"); p.put("mail.smtp.port", "465"); p.put("mail.smtp.starttls.enable","true"); p.put( "mail.smtp.auth", "true"); p.put("mail.smtp.debug", "true"); p.put("mail.smtp.socketFactory.port", "465"); p.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); p.put("mail.smtp.socketFactory.fallback", "false"); try { Authenticator auth = new SMTPAuthenticator(); Session session = Session.getInstance(p, auth); session.setDebug(true); // 메일을 전송할 때 상세한 상황을 콘솔에 출력한다. //session = Session.getDefaultInstance(p); MimeMessage msg = new MimeMessage(session); msg.setSubject("Gmail SMTP 서버를 이용한 JavaMail 테스트"); Address fromAddr = new InternetAddress(Auth.from); // 보내는 사람의 메일주소 msg.setFrom(fromAddr); Address toAddr = new InternetAddress(Auth.to); // 받는 사람의 메일주소 msg.addRecipient(Message.RecipientType.TO, toAddr); //msg.setContent(message, "text/plain;charset=KSC5601"); msg.setContent("<h1>Hello world</h1>", "text/html;charset=utf-8"); System.out.println("Message: " + msg.getContent()); Transport.send(msg); System.out.println("Gmail SMTP서버를 이용한 메일보내기 성공"); } catch (Exception mex) { // Prints all nested (chained) exceptions as well System.out.println("I am here??? "); mex.printStackTrace(); } } private static class SMTPAuthenticator extends javax.mail.Authenticator { public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(Auth.id, Auth.pwd); // Google id, pwd, 주의) @gmail.com 은 제외하세요 } } }
웹기반의 메일 클라이언트 프로그램들은 수신자를 보안상 보호하기 위한 목적으로 송신자가 보낸 HTML메일에 포함된 이미지를 화면에 출력하지 않을 수도 있으므로 아래와 같이 이미지를 첨부파일로 전송하면서 보낸 HTML코드 안에서 첨부된 이미지를 참조하도록 설정할 수도 있다. 첨부된 이미지를 참조할 때는[cid] 라는 프로토콜과 메일헤더에 [content-id]를 사용하게 된다.
package org.dodream.java.mail; import javax.mail.*; import javax.mail.internet.*; import javax.activation.*; import java.io.*; import java.util.*; import java.security.Security; public class JavaMailTest { public static void main(String[] args) { Properties p = new Properties(); p.put("mail.smtp.user", Auth.email_addr); // Google계정@gmail.com으로 설정 p.put("mail.smtp.host", "smtp.gmail.com"); p.put("mail.smtp.port", "465"); p.put("mail.smtp.starttls.enable","true"); p.put( "mail.smtp.auth", "true"); p.put("mail.smtp.debug", "true"); p.put("mail.smtp.socketFactory.port", "465"); p.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); p.put("mail.smtp.socketFactory.fallback", "false"); try { Authenticator auth = new SMTPAuthenticator(); Session session = Session.getInstance(p, auth); session.setDebug(true); // 메일을 전송할 때 상세한 상황을 콘솔에 출력한다. //session = Session.getDefaultInstance(p); MimeMessage msg = new MimeMessage(session); //String message = "Gmail SMTP 서버를 이용한 JavaMail 테스트"; msg.setSubject("Gmail SMTP 서버를 이용한 JavaMail 테스트"); Address fromAddr = new InternetAddress(Auth.from); // 보내는 사람의 메일주소 msg.setFrom(fromAddr); Address toAddr = new InternetAddress(Auth.to); // 받는 사람의 메일주소 msg.addRecipient(Message.RecipientType.TO, toAddr); // // 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(); String htmlText = "<H1>Hello</H1><img src=\"cid:my-image\">"; messageBodyPart.setContent(htmlText, "text/html"); // add it multipart.addBodyPart(messageBodyPart); // second part (the image) messageBodyPart = new MimeBodyPart(); DataSource fds = new FileDataSource ("D:\\test\\sample.jpg"); messageBodyPart .setDataHandler(new DataHandler(fds)); messageBodyPart .setHeader("Content-ID","<my-image>"); // add it multipart.addBodyPart(messageBodyPart ); //msg.setContent(message, "text/plain;charset=KSC5601"); msg.setContent(multipart); System.out.println("Message: " + msg.getContent()); Transport.send(msg); System.out.println("Gmail SMTP서버를 이용한 메일보내기 성공"); } catch (Exception mex) { // Prints all nested (chained) exceptions as well System.out.println("I am here??? "); mex.printStackTrace(); } } private static class SMTPAuthenticator extends javax.mail.Authenticator { public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(Auth.id, Auth.pwd); // Google id, pwd, 주의) @gmail.com 은 제외하세요 } } }