001package edu.pdx.cs410J.examples;
002
003import jakarta.activation.DataHandler;
004import jakarta.activation.DataSource;
005import jakarta.activation.FileDataSource;
006import jakarta.mail.*;
007import jakarta.mail.internet.*;
008
009import java.io.File;
010import java.io.PrintWriter;
011import java.util.Properties;
012
013/**
014 * This program uses the JavaMail API to send a file to someone as a
015 * MIME attachment to an email.  More information about the JavaMail
016 * API is available from http://java.sun.com/products/javamail.
017 *
018 * @author David Whitlock
019 */
020public class MailFile {
021
022  private static final PrintWriter err = new PrintWriter(System.err, true);
023
024  /**
025   * Prints out information about how to use this program.
026   */
027  private static void usage() {
028    err.println("usage: MailFile [options] file recipient [subject]");
029    err.println("  Where [options] are:");
030    err.println("  -server name   Name of the SMTP server");
031    err.println("  -verbose       Print out extra info");
032    System.exit(1);
033  }
034
035  /**
036   * Reads the name of the file to be sent, the recipient of the
037   * email, and the subject of the email (which may contain multiple
038   * words) from the command line.  Then uses the JavaMail API to
039   * construct an email message.
040   */
041  public static void main(String[] args) {
042    StringBuilder subject = null;
043    String fileName = null;
044    String recipient = null;
045    String serverName = "mailhost.pdx.edu";
046    boolean debug = false;
047
048    // Parse the command line
049    for (int i = 0; i < args.length; i++) {
050      if (args[i].equals("-server")) {
051        if(++i >= args.length) {
052          err.println("** Missing server name");
053          usage();
054        }
055
056        serverName = args[i];
057
058      } if (args[i].equals("-verbose")) {
059        debug = true;
060
061      } else if (fileName == null) {
062        fileName = args[i];
063
064      } else if (recipient == null) {
065        recipient = args[i];
066
067      } else {
068        // Part of the subject
069        if(subject == null) {
070          subject = new StringBuilder();
071        }
072
073        subject.append(args[i]).append(" ");
074      }
075    }
076
077    // Check to make sure everything is okay
078    if (fileName == null) {
079      err.println("** No file specified");
080      usage();
081    }
082
083    if (recipient == null) {
084      err.println("** No recipient specified");
085      usage();
086    }
087
088    if (subject == null) {
089      // Default subject
090      subject = new StringBuilder("A file for you");
091    }
092
093    // Obtain a Session for sending email
094    Properties props = new Properties();
095    props.put("mail.smtp.host", serverName);
096    Session session = Session.getDefaultInstance(props, null);
097    session.setDebug(debug);
098
099    MimeMessage message = new MimeMessage(session);
100
101    try {
102      InternetAddress[] to = {new InternetAddress(recipient)};
103      message.setRecipients(Message.RecipientType.TO, to);
104
105    } catch (AddressException ex) {
106      err.println("** Invalid address: " + recipient);
107      System.exit(1);
108
109    } catch (MessagingException ex) {
110      err.println("** MessagingException: " + ex);
111      System.exit(1);
112    }
113    
114    File file = new File(fileName);
115    if (!file.exists()) {
116      err.println("** File " + file + " does not exist!");
117      System.exit(1);
118    }
119
120    // This will be a multi-part MIME message
121    MimeBodyPart textPart = new MimeBodyPart();
122    try {
123      textPart.setContent("File " + file, "text/plain");
124
125    } catch (MessagingException ex) {
126      err.println("** Exception with text part: " + ex);
127      System.exit(1);
128    }
129
130    // Read in the file and make a MimeBodyPart out of it
131    DataSource ds = new FileDataSource(file);
132    DataHandler dh = new DataHandler(ds);
133    MimeBodyPart filePart = new MimeBodyPart();
134    try {
135      filePart.setDataHandler(dh);
136      filePart.setFileName(file.getName());
137      filePart.setDescription("The file you requested");
138
139    } catch (MessagingException ex) {
140      err.println("** Exception with file part: " + ex);
141      System.exit(1);
142    }
143
144    try {
145      Multipart mp = new MimeMultipart();
146      mp.addBodyPart(textPart);
147      mp.addBodyPart(filePart);
148
149      message.setContent(mp);
150
151      // Finally, send the message
152      Transport.send(message);
153
154    } catch (MessagingException ex) {
155      err.println("** Exception while adding parts and sending: " +
156                  ex);
157      System.exit(1);
158    }
159    
160  }
161  
162
163}