Subversion Repositories javautils

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Copyright 1996-2007 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  *
  8.  *   - Redistributions of source code must retain the above copyright
  9.  *     notice, this list of conditions and the following disclaimer.
  10.  *
  11.  *   - Redistributions in binary form must reproduce the above copyright
  12.  *     notice, this list of conditions and the following disclaimer in the
  13.  *     documentation and/or other materials provided with the distribution.
  14.  *
  15.  *   - Neither the name of Sun Microsystems nor the names of its
  16.  *     contributors may be used to endorse or promote products derived
  17.  *     from this software without specific prior written permission.
  18.  *
  19.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  20.  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  21.  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  22.  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  23.  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  25.  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  26.  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  27.  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28.  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30.  */
  31.  
  32. import java.util.*;
  33. import java.io.*;
  34. import javax.mail.*;
  35. import javax.mail.internet.*;
  36. import javax.activation.*;
  37.  
  38. /**
  39.  * sendfile will create a multipart message with the second
  40.  * block of the message being the given file.<p>
  41.  *
  42.  * This demonstrates how to use the FileDataSource to send
  43.  * a file via mail.<p>
  44.  *
  45.  * usage: <code>java sendfile <i>to from smtp file true|false</i></code>
  46.  * where <i>to</i> and <i>from</i> are the destination and
  47.  * origin email addresses, respectively, and <i>smtp</i>
  48.  * is the hostname of the machine that has smtp server
  49.  * running.  <i>file</i> is the file to send. The next parameter
  50.  * either turns on or turns off debugging during sending.
  51.  *
  52.  * @author      Christopher Cotton
  53.  */
  54. public class sendfile {
  55.  
  56.     public static void main(String[] args) {
  57.         if (args.length != 5) {
  58.             System.out.println("usage: java sendfile <to> <from> <smtp> <file> true|false");
  59.             System.exit(1);
  60.         }
  61.  
  62.         String to = args[0];
  63.         String from = args[1];
  64.         String host = args[2];
  65.         String filename = args[3];
  66.         boolean debug = Boolean.valueOf(args[4]).booleanValue();
  67.         String msgText1 = "Sending a file.\n";
  68.         String subject = "Sending a file";
  69.        
  70.         // create some properties and get the default Session
  71.         Properties props = System.getProperties();
  72.         props.put("mail.smtp.host", host);
  73.        
  74.         Session session = Session.getInstance(props, null);
  75.         session.setDebug(debug);
  76.        
  77.         try {
  78.             // create a message
  79.             MimeMessage msg = new MimeMessage(session);
  80.             msg.setFrom(new InternetAddress(from));
  81.             InternetAddress[] address = {new InternetAddress(to)};
  82.             msg.setRecipients(Message.RecipientType.TO, address);
  83.             msg.setSubject(subject);
  84.  
  85.             // create and fill the first message part
  86.             MimeBodyPart mbp1 = new MimeBodyPart();
  87.             mbp1.setText(msgText1);
  88.  
  89.             // create the second message part
  90.             MimeBodyPart mbp2 = new MimeBodyPart();
  91.  
  92.             // attach the file to the message
  93.             mbp2.attachFile(filename);
  94.  
  95.             /*
  96.              * Use the following approach instead of the above line if
  97.              * you want to control the MIME type of the attached file.
  98.              * Normally you should never need to do this.
  99.              *
  100.             FileDataSource fds = new FileDataSource(filename) {
  101.                 public String getContentType() {
  102.                     return "application/octet-stream";
  103.                 }
  104.             };
  105.             mbp2.setDataHandler(new DataHandler(fds));
  106.             mbp2.setFileName(fds.getName());
  107.              */
  108.  
  109.             // create the Multipart and add its parts to it
  110.             Multipart mp = new MimeMultipart();
  111.             mp.addBodyPart(mbp1);
  112.             mp.addBodyPart(mbp2);
  113.  
  114.             // add the Multipart to the message
  115.             msg.setContent(mp);
  116.  
  117.             // set the Date: header
  118.             msg.setSentDate(new Date());
  119.  
  120.             /*
  121.              * If you want to control the Content-Transfer-Encoding
  122.              * of the attached file, do the following.  Normally you
  123.              * should never need to do this.
  124.              *
  125.             msg.saveChanges();
  126.             mbp2.setHeader("Content-Transfer-Encoding", "base64");
  127.              */
  128.  
  129.             // send the message
  130.             Transport.send(msg);
  131.            
  132.         } catch (MessagingException mex) {
  133.             mex.printStackTrace();
  134.             Exception ex = null;
  135.             if ((ex = mex.getNextException()) != null) {
  136.                 ex.printStackTrace();
  137.             }
  138.         } catch (IOException ioex) {
  139.             ioex.printStackTrace();
  140.         }
  141.     }
  142. }
  143.