Changes between Initial Version and Version 1 of waue/2011/0804


Ignore:
Timestamp:
Aug 4, 2011, 3:12:11 PM (13 years ago)
Author:
waue
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • waue/2011/0804

    v1 v1  
     1
     2 * 系統要裝能寄信的工具才可
     3
     4{{{
     5sudo apt-get install sendmail sendmail-bin
     6}}}
     7
     8 * 下載 javamail
     9[http://www.oracle.com/technetwork/java/javamail/index.html]
     10
     11 * 其實不用  JavaBeans Activation Framework (JAF)
     12
     13{{{
     14#!java
     15package tw.org.nchc.icasIII;
     16
     17import java.io.BufferedReader;
     18import java.io.File;
     19import java.io.FileReader;
     20import java.io.IOException;
     21import java.util.Date;
     22import java.util.Properties;
     23
     24import javax.activation.DataHandler;
     25import javax.mail.Folder;
     26import javax.mail.Message;
     27import javax.mail.MessagingException;
     28import javax.mail.Session;
     29import javax.mail.Store;
     30import javax.mail.Transport;
     31import javax.mail.URLName;
     32import javax.mail.internet.InternetAddress;
     33import javax.mail.internet.MimeMessage;
     34import javax.mail.util.ByteArrayDataSource;
     35
     36public class sendhtml {
     37
     38        public static void main(String[] argv) {
     39                new sendhtml(argv);
     40        }
     41
     42        public sendhtml(String[] argv) {
     43
     44                String to, subject = null, from = null;
     45                String cc = null, bcc = null, url = null;
     46                String mailhost = null;
     47                String mailer = "sendhtml";
     48                String protocol = null, host = null, user = null, password = null;
     49                String record = null; // name of folder in which to record mail
     50                boolean debug = false;
     51               
     52                int optind;
     53                /*
     54
     55
     56                for (optind = 0; optind < argv.length; optind++) {
     57                        if (argv[optind].equals("-T")) {
     58                                protocol = argv[++optind];
     59                        } else if (argv[optind].equals("-H")) {
     60                                host = argv[++optind];
     61                        } else if (argv[optind].equals("-U")) {
     62                                user = argv[++optind];
     63                        } else if (argv[optind].equals("-P")) {
     64                                password = argv[++optind];
     65                        } else if (argv[optind].equals("-M")) {
     66                                mailhost = argv[++optind];
     67                        } else if (argv[optind].equals("-f")) {
     68                                record = argv[++optind];
     69                        } else if (argv[optind].equals("-s")) {
     70                                subject = argv[++optind];
     71                        } else if (argv[optind].equals("-o")) { // originator
     72                                from = argv[++optind];
     73                        } else if (argv[optind].equals("-c")) {
     74                                cc = argv[++optind];
     75                        } else if (argv[optind].equals("-b")) {
     76                                bcc = argv[++optind];
     77                        } else if (argv[optind].equals("-L")) {
     78                                url = argv[++optind];
     79                        } else if (argv[optind].equals("-d")) {
     80                                debug = true;
     81                        } else if (argv[optind].equals("--")) {
     82                                optind++;
     83                                break;
     84                        } else if (argv[optind].startsWith("-")) {
     85                                System.out
     86                                                .println("Usage: sendhtml [[-L store-url] | [-T prot] [-H host] [-U user] [-P passwd]]");
     87                                System.out
     88                                                .println("\t[-s subject] [-o from-address] [-c cc-addresses] [-b bcc-addresses]");
     89                                System.out
     90                                                .println("\t[-f record-mailbox] [-M transport-host] [-d] [address]");
     91                                System.exit(1);
     92                        } else {
     93                                break;
     94                        }
     95                }
     96                */
     97               
     98                to = "waue0920@gmail.com";
     99                subject = "test, javamail";
     100                from = "waue0920@nchc.org.tw";
     101                cc = null;
     102                bcc = null;
     103                url = null;
     104                mailhost = "localhost";
     105                mailer = "sendhtml";
     106                protocol = null;
     107                host = null;
     108                user = null;
     109                password = null;
     110                record = null; // name of folder in which to record mail
     111               
     112                try {
     113                        BufferedReader in = new BufferedReader(new FileReader(new File("/home/waue/Desktop/index.html")));
     114                        if (subject == null) {
     115                                System.out.print("Subject: ");
     116                                System.out.flush();
     117                                subject = in.readLine();
     118                        } else {
     119                                System.out.println("Subject: " + subject);
     120                        }
     121
     122                        Properties props = System.getProperties();
     123
     124                        if (mailhost != null)
     125                                props.put("mail.smtp.host", mailhost);
     126
     127                        Session session = Session.getInstance(props, null);
     128                        if (debug)
     129                                session.setDebug(true);
     130
     131                        // construct the message
     132                        Message msg = new MimeMessage(session);
     133                        if (from != null)
     134                                msg.setFrom(new InternetAddress(from));
     135                        else
     136                                msg.setFrom();
     137
     138                        msg.setRecipients(Message.RecipientType.TO,
     139                                        InternetAddress.parse(to, false));
     140                        if (cc != null)
     141                                msg.setRecipients(Message.RecipientType.CC,
     142                                                InternetAddress.parse(cc, false));
     143                        if (bcc != null)
     144                                msg.setRecipients(Message.RecipientType.BCC,
     145                                                InternetAddress.parse(bcc, false));
     146
     147                        msg.setSubject(subject);
     148
     149                        collect(in, msg);
     150
     151                        msg.setHeader("X-Mailer", mailer);
     152                        msg.setSentDate(new Date());
     153
     154                        // send the thing off
     155                        Transport.send(msg);
     156
     157                        System.out.println("\nMail was sent successfully.");
     158
     159                        // Keep a copy, if requested.
     160
     161                        if (record != null) {
     162                                // Get a Store object
     163                                Store store = null;
     164                                if (url != null) {
     165                                        URLName urln = new URLName(url);
     166                                        store = session.getStore(urln);
     167                                        store.connect();
     168                                } else {
     169                                        if (protocol != null)
     170                                                store = session.getStore(protocol);
     171                                        else
     172                                                store = session.getStore();
     173
     174                                        // Connect
     175                                        if (host != null || user != null || password != null)
     176                                                store.connect(host, user, password);
     177                                        else
     178                                                store.connect();
     179                                }
     180
     181                                // Get record Folder. Create if it does not exist.
     182                                Folder folder = store.getFolder(record);
     183                                if (folder == null) {
     184                                        System.err.println("Can't get record folder.");
     185                                        System.exit(1);
     186                                }
     187                                if (!folder.exists())
     188                                        folder.create(Folder.HOLDS_MESSAGES);
     189
     190                                Message[] msgs = new Message[1];
     191                                msgs[0] = msg;
     192                                folder.appendMessages(msgs);
     193
     194                                System.out.println("Mail was recorded successfully.");
     195                        }
     196
     197                } catch (Exception e) {
     198                        e.printStackTrace();
     199                }
     200        }
     201
     202        public void collect(BufferedReader in, Message msg)
     203                        throws MessagingException, IOException {
     204                String line;
     205                String subject = msg.getSubject();
     206                StringBuffer sb = new StringBuffer();
     207                sb.append("<HTML>\n");
     208                sb.append("<HEAD>\n");
     209                sb.append("<TITLE>\n");
     210                sb.append(subject + "\n");
     211                sb.append("</TITLE>\n");
     212                sb.append("</HEAD>\n");
     213
     214                sb.append("<BODY>\n");
     215                sb.append("<H1>" + subject + "</H1>" + "\n");
     216
     217                while ((line = in.readLine()) != null) {
     218                        sb.append(line);
     219                        sb.append("\n");
     220                }
     221
     222                sb.append("</BODY>\n");
     223                sb.append("</HTML>\n");
     224
     225                msg.setDataHandler(new DataHandler(new ByteArrayDataSource(sb
     226                                .toString(), "text/html")));
     227        }
     228}
     229
     230}}}