tnk #1153(2007/07/20 04:40 GMT) [ Java ] Rating0/0=0.00
要JavaMail。関連JarファイルがCLASSPATHに含まれている必要があります。 実行方法は, java MailSender テンプレートファイル 差込ファイル 差込ファイルの形式は,そのままだとパーサーが書きにくいので, 以下のように変更しています。 ----差し込みデータ---- from: from@example.org to: to@example.org name: どう書く fromname: 管理者 url: http://ja.doukaku.org/ ----差し込みデータ---- Javaには題意に沿うようなテンプレートフォーマッターが 標準ではないので,自作しています。 また, ・テンプレートのファイルからの読み込みを作成 ・差込データの読み込みとパースを作成 ・テンプレートの形式がJavaMail向きでないのでパース処理が必要 というような理由で,かなり長くなっています。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.Reader; import java.io.StringWriter; import java.util.HashMap; import java.util.Properties; import java.util.StringTokenizer; import javax.mail.MessagingException; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; public class MailSender { private static final String encoding = "ISO-2022-JP"; public static void main(String[] args) throws Exception { // テンプレートと差込データの読み込み String template = readFile(args[0]); HashMap vars = readVars(args[1]); // テンプレートを使用したメールの整形 TemplateFormatter formatter = new TemplateFormatter(); String mail = formatter.format(template, vars); // メールの送信 sendMail(mail); } public static String readFile(String filename) throws IOException { Reader in = new InputStreamReader(new FileInputStream(filename), "UTF-8"); StringWriter out = new StringWriter(); for (int c = in.read(); c >=0; c = in.read()) out.write(c); in.close(); out.close(); return out.toString(); } public static HashMap readVars(String filename) throws IOException { HashMap<String,String> result = new HashMap<String,String>(); BufferedReader in = new BufferedReader( new InputStreamReader( new FileInputStream(filename), "UTF-8")); for (String s = in.readLine(); s != null; s = in.readLine()) { if (s.trim().startsWith("#")) continue; int off = s.indexOf(":"); if (off <= 0) continue; result.put(s.substring(0,off).trim(), s.substring(off+1).trim()); } in.close(); return result; } public static void sendMail(String mail) throws MessagingException, IOException { Properties props = System.getProperties(); Session session = Session.getDefaultInstance(props); MimeMessage message = new MimeMessage(session); StringTokenizer st = new StringTokenizer(mail, "\n"); while (st.hasMoreTokens()) { String s = st.nextToken(); int off = s.indexOf(":"); if (off < 0) break; String header = s.substring(0,off).trim(); String value = s.substring(off+1); if (header.equalsIgnoreCase("subject")) { message.setSubject(value, encoding); } else if (header.equalsIgnoreCase("to")) { message.setRecipients(MimeMessage.RecipientType.TO, value); } else if (header.equalsIgnoreCase("from")) { message.setFrom(new InternetAddress(value)); } else { message.setHeader(header, value); } } StringBuffer body = new StringBuffer(); while (st.hasMoreTokens()) { body.append(st.nextToken()).append("\n"); } message.setText(body.toString(), encoding); // Transport.send(message); // 実際に送信する場合 message.writeTo(System.out); // 課題にあわせて標準出力に } } class TemplateFormatter { private String prefix = "[["; private String postfix = "]]"; public TemplateFormatter() {} public TemplateFormatter(String prefix, String postfix) { this.prefix = prefix; this.postfix = postfix; } public String format(String tempate, HashMap<String,String> vars) { StringBuffer result = new StringBuffer(tempate); for (String key : vars.keySet()) { String element = prefix + key + postfix; while (result.indexOf(element) >= 0) { int off = result.indexOf(element); result.replace(off, off+element.length(), vars.get(key)); } } return result.toString(); } }
Rating0/0=0.00-0+
[ reply ]
tnk
#1153()
[
Java
]
Rating0/0=0.00
要JavaMail。関連JarファイルがCLASSPATHに含まれている必要があります。 実行方法は, java MailSender テンプレートファイル 差込ファイル 差込ファイルの形式は,そのままだとパーサーが書きにくいので, 以下のように変更しています。 ----差し込みデータ---- from: from@example.org to: to@example.org name: どう書く fromname: 管理者 url: http://ja.doukaku.org/ ----差し込みデータ---- Javaには題意に沿うようなテンプレートフォーマッターが 標準ではないので,自作しています。 また, ・テンプレートのファイルからの読み込みを作成 ・差込データの読み込みとパースを作成 ・テンプレートの形式がJavaMail向きでないのでパース処理が必要 というような理由で,かなり長くなっています。Rating0/0=0.00-0+
[ reply ]