{{{ #!html
使用 java api 寄信
javamail 1.4.4 + java 6 + sendmail
}}} [[PageOutline]] * 系統要裝能寄信的工具 {{{ sudo apt-get install sendmail sendmail-bin }}} * 下載 javamail ; 雖然網站上有寫,但此次不需要 JavaBeans Activation Framework (JAF) [http://www.oracle.com/technetwork/java/javamail/index.html] * 解壓縮後,將 javamail.jar 放到 classpath 內 * 測試將 /home/waue/Desktop/index.html 寄到 gmail * 程式碼 {{{ #!java package tw.org.nchc.icasIII; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.util.Date; import java.util.Properties; import javax.activation.DataHandler; import javax.mail.Folder; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Session; import javax.mail.Store; import javax.mail.Transport; import javax.mail.URLName; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import javax.mail.util.ByteArrayDataSource; public class sendhtml { public static void main(String[] argv) { new sendhtml(argv); } public sendhtml(String[] argv) { String to, subject = null, from = null; String cc = null, bcc = null, url = null; String mailhost = null; String mailer = "sendhtml"; String protocol = null, host = null, user = null, password = null; String record = null; // name of folder in which to record mail boolean debug = false; int optind; to = "waue0920@gmail.com"; subject = "test, javamail"; from = "waue0920@nchc.org.tw"; cc = null; bcc = null; url = null; mailhost = "localhost"; mailer = "sendhtml"; protocol = null; host = null; user = null; password = null; record = null; // name of folder in which to record mail try { BufferedReader in = new BufferedReader(new FileReader(new File("/home/waue/Desktop/index.html"))); if (subject == null) { System.out.print("Subject: "); System.out.flush(); subject = in.readLine(); } else { System.out.println("Subject: " + subject); } Properties props = System.getProperties(); if (mailhost != null) props.put("mail.smtp.host", mailhost); Session session = Session.getInstance(props, null); if (debug) session.setDebug(true); // construct the message Message msg = new MimeMessage(session); if (from != null) msg.setFrom(new InternetAddress(from)); else msg.setFrom(); msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false)); if (cc != null) msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc, false)); if (bcc != null) msg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(bcc, false)); msg.setSubject(subject); collect(in, msg); msg.setHeader("X-Mailer", mailer); msg.setSentDate(new Date()); // send the thing off Transport.send(msg); System.out.println("\nMail was sent successfully."); // Keep a copy, if requested. if (record != null) { // Get a Store object Store store = null; if (url != null) { URLName urln = new URLName(url); store = session.getStore(urln); store.connect(); } else { if (protocol != null) store = session.getStore(protocol); else store = session.getStore(); // Connect if (host != null || user != null || password != null) store.connect(host, user, password); else store.connect(); } // Get record Folder. Create if it does not exist. Folder folder = store.getFolder(record); if (folder == null) { System.err.println("Can't get record folder."); System.exit(1); } if (!folder.exists()) folder.create(Folder.HOLDS_MESSAGES); Message[] msgs = new Message[1]; msgs[0] = msg; folder.appendMessages(msgs); System.out.println("Mail was recorded successfully."); } } catch (Exception e) { e.printStackTrace(); } } public void collect(BufferedReader in, Message msg) throws MessagingException, IOException { String line; String subject = msg.getSubject(); StringBuffer sb = new StringBuffer(); sb.append("\n"); sb.append("\n"); sb.append("\n"); sb.append(subject + "\n"); sb.append("\n"); sb.append("\n"); sb.append("\n"); sb.append("

" + subject + "

" + "\n"); while ((line = in.readLine()) != null) { sb.append(line); sb.append("\n"); } sb.append("\n"); sb.append("\n"); msg.setDataHandler(new DataHandler(new ByteArrayDataSource(sb .toString(), "text/html"))); } } }}} * 可以參考 demo 內的範例寫 java 寄信的 code