1 38 39 import java.util.*; 40 import java.io.*; 41 import javax.mail.*; 42 import javax.mail.internet.*; 43 import javax.activation.*; 44 45 61 public class sendfile { 62 63 public static void main(String [] args) { 64 if (args.length != 5) { 65 System.out.println("usage: java sendfile <to> <from> <smtp> <file> true|false"); 66 System.exit(1); 67 } 68 69 String to = args[0]; 70 String from = args[1]; 71 String host = args[2]; 72 String filename = args[3]; 73 boolean debug = Boolean.valueOf(args[4]).booleanValue(); 74 String msgText1 = "Sending a file.\n"; 75 String subject = "Sending a file"; 76 77 Properties props = System.getProperties(); 79 props.put("mail.smtp.host", host); 80 81 Session session = Session.getInstance(props, null); 82 session.setDebug(debug); 83 84 try { 85 MimeMessage msg = new MimeMessage(session); 87 msg.setFrom(new InternetAddress(from)); 88 InternetAddress[] address = {new InternetAddress(to)}; 89 msg.setRecipients(Message.RecipientType.TO, address); 90 msg.setSubject(subject); 91 92 MimeBodyPart mbp1 = new MimeBodyPart(); 94 mbp1.setText(msgText1); 95 96 MimeBodyPart mbp2 = new MimeBodyPart(); 98 99 FileDataSource fds = new FileDataSource(filename); 101 mbp2.setDataHandler(new DataHandler(fds)); 102 mbp2.setFileName(fds.getName()); 103 104 Multipart mp = new MimeMultipart(); 106 mp.addBodyPart(mbp1); 107 mp.addBodyPart(mbp2); 108 109 msg.setContent(mp); 111 112 msg.setSentDate(new Date()); 114 115 Transport.send(msg); 117 118 } catch (MessagingException mex) { 119 mex.printStackTrace(); 120 Exception ex = null; 121 if ((ex = mex.getNextException()) != null) { 122 ex.printStackTrace(); 123 } 124 } 125 } 126 } 127
| Popular Tags
|