Java Mail 을 이용한 MIME 변환

public static byte[] getBytes(InputStream is) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[10240]; // 10kb int read; while ((read = is.read(buffer)) > 0) { baos.write(buffer, 0, read); } return baos.toByteArray(); } public static byte[] makeMime(String charset, String mailFrom, String mailFromDisplay, String[] cc, String[] bcc, String title, String body) throws Exception { Properties props = new Properties(); Session session = Session.getDefaultInstance(props, null); MimeMessage mimeMessage = new MimeMessage(session); Address buffer = null; Address[] addressCC = new Address[cc.length]; for (int i=0; i<cc.length; i++) { buffer = new InternetAddress(cc[i], cc[i], charset); addressCC[i] = buffer; } Address[] addressBCC = new Address[bcc.length]; for (int i=0; i<bcc.length; i++) { buffer = new InternetAddress(bcc[i], bcc[i], charset); addressBCC[i] = buffer; } mimeMessage.setFrom(new InternetAddress(mailFrom, mailFromDisplay, charset)); mimeMessage.setRecipients(Message.RecipientType.CC, addressCC); mimeMessage.setRecipients(Message.RecipientType.BCC, addressBCC); mimeMessage.setSubject(title, charset); mimeMessage.setText(body, charset); mimeMessage.setHeader(“Content-Type”, “text/plain; charset=\”” + charset + “\””); mimeMessage.setHeader(“Content-Transfer-Encoding”, “7bit”); mimeMessage.setSentDate(new Date()); ByteArrayOutputStream out = new ByteArrayOutputStream(); mimeMessage.writeTo(out); return out.toByteArray(); // 실제 송신은 아래처럼 합니다. // Transport transport = session.getTransport(“smtp”); // transport.connect(); // transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients()); // transport.close(); } 참고사이트 http://d.hatena.ne.jp/ttshrk/20110405/1301998708 http://d.hatena.ne.jp/ttshrk/20110407/1302152957]]>

Related Posts