¢ºSending HTML / Image Mail
HTML¸Þ½ÃÁö ¹× Imageº¸³»±â
HTMLÀ» ¸Þ½ÃÁö·Î º¸³»´Â °ÍÀº text ¸Þ½ÃÁö¸¦ º¸³»´Â °Íº¸´Ù Á¶±Ý ´õ °£´ÜÇÏ°Ô ÀÌ·ç¾î ³¾¼ö ÀÖ´Ù.
ƯÁ¤°ª¸¸ ¼¼ÆÃÇÏ¸é µÇ±â ¶§¹®Àε¥, ¹Þ´Â ÂÊ¿¡¼ ÇØ´çµÇ´Â contentType¿¡ µû¶ó ¸Þ½ÃÁö¸¦ html·Î
Çؼ®ÇÒÁö ¾Æ´Ï¸é ÀÏ¹Ý text·Î Çؼ®ÇÒÁö¸¦ °áÁ¤Çϱ⠶§¹®¿¡ º¸³»´Â ÂÊ¿¡¼ messageÀÇ setContent()
¸Þ¼Òµå·Î ¿øÇϴ ŸÀÔ¸¸ ¼¼ÆÃÇÏ¸é ±×¸¸ÀÌ´Ù.
¿¹>
String htmlText = "<H1>Hello</H1>" +
"<img src=\"http://www.jguru.com/images/logo.gif\">";
message.setContent(htmlText, "text/html"));
¸Þ½ÃÁö¸¦ ¼ö½ÅÇÏ´Â Ãø¿¡¼ ¸ðµÎ ¹Þ¾ÒÀ»¶§, JavaMail API´Â HTML¸Þ½ÃÁö¸¦ Ç¥ÇöÇϱâ À§ÇÏ¿©
¾Æ¹«·± ÀÏÀ» ÇÏÁö ¾Ê¾Æµµ µÈ´Ù. ´Ü¼øÈ÷ º¸±â À§ÇØ SwingÀÇ JEditorPaneÀ̳ª HTMLÀ» º¼¼ö ÀÖ´Â
third-Party ÄÄÆ÷³ÍÆ®¸¸ ÀÖÀ¸¸é µÇ±â ¶§¹®ÀÌ´Ù.
¿¹·Î¼ JEditorPane¿¹Á¦¸¦ ¸ÞÀÏ¿¡ Æ÷ÇÔ½ÃÄÑ ³õÀº ÄÚµåÀÌ´Ù.
if (message.getContentType().equals("text/html")) {
String content = (String)message.getContent();
JFrame frame = new JFrame();
JEditorPane text = new JEditorPane("text/html", content);
text.setEditable(false);
JScrollPane pane = new JScrollPane(text);
frame.getContentPane().add(pane);
frame.setSize(300, 300);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.show();
}
================================
À̹ø¿¡´Â ¸ÞÀÏÀÌ À̹ÌÁö¸¦ Æ÷ÇÔ½ÃÄÑ Àü¼ÛÇÏ´Â ¹æ¹ý¿¡ ´ëÇÏ¿© ¾Ë¾Æº¸ÀÚ.
HTMLÀ» ÀÌ¿ëÇÏ¿© ¸Þ½ÃÁö¸¦ ¸ðµÎ º¸³Â´Ù¸é »ç½Ç ±Ã±ÝÇÑ °ÍÀÌ À̹ÌÁö¸¦ ¾î¶»°Ô ¸Þ½ÃÁö³»¿ë¾È¿¡
Æ÷ÇÔ½ÃÄÑ Àü¼ÛÇØ¾ß ÇÒ°ÍÀΰ¡¿¡ ´ëÇÑ °ÍÀε¥, ÀÌ·¯ÇÑ À̹ÌÁöÀÇ Àü¼ÛÀº cid URL, Áï image¸¦ ÷ºÎÇÒ¶§
content-ID header¸¦ ÂüÁ¶Çϴ Ưº°ÇÑ Å±׿¡ ÀÇÇؼ À̹ÌÁö¸¦ Á¶ÀÛÇÏ¿© º¸³¾¼ö ÀÖ´Â ±â´ÉÀ» Á¦°øÇÑ´Ù.
À̹ÌÁö ÷ºÎ¿¡ ´ëÇÑ Ã³¸®´Â ¸Þ½ÃÁö¿¡ ÆÄÀÏÀ» Æ÷ÇÔÇÏ´Â °Í°ú ºñ½ÁÇϸç, ´ÜÁö ´Ù¸¥ °ÍÀ̶ó°í´Â
MimeMultiPartÀÇ »ý¼ºÀڵ鿡°Ô subTypeµéÀÌ ¼¼ÆõǾîÁö°í imgű׸¦ »ç¿ëÇؼ À̹ÌÁö°¡
Content-ID header¿¡ ¼¼ÆõǾîÁø´Ù´Â°ÍÀ» ¹Ýµå½Ã ¾Ë·Á¾ß ÇÑ´Ù.
¾Æ·¡ÀÇ Äڵ尡 ¹Ù·Î ±×°ÍÀ» ±¸ÇöÇØ ³õÀº ÄÚµåÀÌ´Ù.
String file = ...;
// Create the message
Message message = new MimeMessage(session);
// Fill its headers
message.setSubject("Embedded Image");
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
// message part¸¦ »ý¼ºÇÑ´Ù.
BodyPart messageBodyPart = new MimeBodyPart();
String htmlText = "<H1>Hello</H1>" +
"<img src=\"cid:memememe\">";
messageBodyPart.setContent(htmlText, "text/html");
// Multipart¸¦ »ý¼ºÇÑ´Ù.
MimeMultipart multipart = new MimeMultipart("related");
multipart.addBodyPart(messageBodyPart);
// À̹ÌÁö¸¦ À§ÇÑ part¸¦ »ý¼ºÇÑ´Ù.
messageBodyPart = new MimeBodyPart();
// À̹ÌÁö¸¦ °¡Á®¿Í part¿¡ Æ÷ÇÔ½ÃŲ´Ù.
DataSource fds = new FileDataSource(file);
messageBodyPart.setDataHandler(new DataHandler(fds));
messageBodyPart.setHeader("Content-ID","memememe");
// multi-part¿¡ Ãß°¡½ÃŲ´Ù.
multipart.addBodyPart(messageBodyPart);
// ¸Þ½ÃÁö¿Í multipart¸¦ °áÇÕ½ÃŲ´Ù.
message.setContent(multipart);
¾Æ·¡´Â ¼Ò½ºÄÚµåÀÌ´Ù.
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class HtmlImageExample {
public static void main (String args[]) throws Exception {
new HtmlImageExample();
}
public HtmlImageExample() throws Exception {
String host = "orgio.net";
String from = "ienvyou@javapattern.info";
String to = "specular@netsgo.com";
String file = "logo.gif";
// Get system properties
Properties props = System.getProperties();
// Setup mail server
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth","true");
Authenticator auth = new MyAuthentication();
// Get session
Session session = Session.getDefaultInstance(props, auth);
session.setDebug(true);
// Create the message
Message message = new MimeMessage(session);
// Fill its headers
message.setSubject("Embedded Image");
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
// Create your new message part
BodyPart messageBodyPart = new MimeBodyPart();
// Set the HTML content, be sure it references the attachment
String htmlText = "<H1>Hello</H1>" +
"<img src=\"cid:memememe\">";
// Set the content of the body part
messageBodyPart.setContent(htmlText, "text/html");
// Create a related multi-part to combine the parts
MimeMultipart multipart = new MimeMultipart("related");
// Add body part to multipart
multipart.addBodyPart(messageBodyPart);
// Create part for the image
messageBodyPart = new MimeBodyPart();
// Fetch the image and associate to part
DataSource fds = new FileDataSource(file);
messageBodyPart.setDataHandler(new DataHandler(fds));
// Add a header to connect to the HTML
messageBodyPart.setHeader("Content-ID","memememe");
// Add part to multi-part
multipart.addBodyPart(messageBodyPart);
// Associate multi-part with message
message.setContent(multipart);
// Send message
Transport.send(message);
}
}
class MyAuthentication extends Authenticator {
PasswordAuthentication pa;
public MyAuthentication(){
pa = new PasswordAuthentication("ienvyou","0000");
}
public PasswordAuthentication getPasswordAuthentication() {
return pa;
}
}
|
|