1 21 package uk.org.primrose.web; 22 23 import javax.mail.*; 24 import javax.mail.internet.*; 25 import java.util.*; 26 import java.io.*; 27 import javax.activation.*; 28 29 34 public class SendMail { 35 static String mxServer; 36 String toAddress, fromAddress; 37 String subject, text; 38 MimeBodyPart mbp = null; 39 Multipart mp = new MimeMultipart(); 40 41 42 52 public SendMail(String mxServer, String toAddress, String fromAddress, String subject, String text) { 53 this.mxServer = mxServer; 54 this.toAddress = toAddress; 55 this.fromAddress = fromAddress; 56 this.subject = subject; 57 this.text = text; 58 59 } 60 61 65 public void attach(File file) { 66 try { 67 mbp = new MimeBodyPart(); 68 mbp.setFileName(file.getName()); 69 mbp.setDataHandler(new DataHandler(new FileDataSource(file))); 70 mp.addBodyPart(mbp); 71 } catch (MessagingException me) { 72 me.printStackTrace(); 73 } 74 } 75 76 80 public void send() throws IOException { 81 Properties props = System.getProperties(); 82 props.put("mail.smtp.host", mxServer); 83 Session session = Session.getDefaultInstance(props, null); 84 85 try { 86 Message msg = new MimeMessage(session); 87 msg.setFrom(new InternetAddress(fromAddress)); 88 msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toAddress, false)); 89 msg.setSubject(subject); 90 msg.setHeader("X-Mailer", "JavaMail"); 91 92 if (mbp != null) { 96 MimeBodyPart mbp2 = new MimeBodyPart(); 97 mbp2.setText(text); 98 mp.addBodyPart(mbp2, 0); 99 msg.setContent(mp); 100 } else { 101 msg.setContent(text, "text/html"); 102 } 103 104 Transport.send(msg); 105 } catch (AddressException ae) { 106 ae.printStackTrace(); 107 throw new IOException("[SendMail] Address is invalid for mail " +ae.toString()); 108 } catch (MessagingException me) { 109 me.printStackTrace(); 110 throw new IOException("[SendMail] Message send exception " +me.toString()); 111 } 112 } 113 114 122 public static String nslookup(String domain) { 123 String mailserver = null; 124 try { 125 Process p = Runtime.getRuntime().exec("nslookup -type=mx " +domain); 126 BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream())); 127 128 boolean gotMxLine = false; 129 String line = null; 130 String token = null; 131 132 while ((line = br.readLine()) != null) { 133 gotMxLine = false; 134 StringTokenizer st = new StringTokenizer(line); 136 while (st.hasMoreTokens()) { 137 token = st.nextToken(); 138 if (token.equals("exchanger")) { 139 gotMxLine = true; 140 } 141 if (gotMxLine) { 142 mailserver = token; 143 } 144 } 145 146 } 147 } catch (IOException ioe) { 148 ioe.printStackTrace(); 149 return null; 150 } 151 152 System.out.println("Mail Server to use is :: " +mailserver); 153 return mailserver; 154 } 155 156 161 public static void main(String args[]) throws Exception { 162 if (args.length < 5) { 163 System.out.println("Usuage :: <mxServer> <toAddress> <fromAddress> <subject> <text>"); 164 System.exit(1); 165 } 166 171 String text = ""; 172 BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(args[4]))); 174 String line = ""; 175 while ((line = br.readLine()) != null) { 177 text += line +"\n"; 178 } 179 180 185 new SendMail(args[0], args[1], args[2], args[3], text).send(); 186 188 189 221 } 222 } 223 | Popular Tags |