Home | History | Annotate | Download | only in releng
      1 /*******************************************************************************
      2  * Copyright (c) 2000, 2006 IBM Corporation and others.
      3  * All rights reserved. This program and the accompanying materials
      4  * are made available under the terms of the Eclipse Public License v1.0
      5  * which accompanies this distribution, and is available at
      6  * http://www.eclipse.org/legal/epl-v10.html
      7  *
      8  * Contributors:
      9  *     IBM Corporation - initial API and implementation
     10  *******************************************************************************/
     11 package org.eclipse.releng;
     12 
     13 /**
     14  * A Class that sends build related email messages.  host, sender, recipient and
     15  * build related information set in monitor.properties
     16  */
     17 
     18 import javax.activation.DataHandler;
     19 import javax.activation.FileDataSource;
     20 import javax.mail.*;
     21 import javax.mail.internet.*;
     22 
     23 import java.io.File;
     24 import java.util.StringTokenizer;
     25 import java.util.Properties;
     26 
     27 public class Mailer {
     28 
     29 	// flag that determines whether or not to send mail
     30 	boolean sendMail = true;
     31 
     32 	// the Object that holds the key value pairs in monitor.properties
     33 	private BuildProperties buildProperties;
     34 
     35 	//convert the comma separated list of email addressed into an array of Address objects
     36 	private Address[] getAddresses(String recipientList) {
     37 		int i = 0;
     38 		StringTokenizer recipients = new StringTokenizer(recipientList, ",");
     39 		Address[] addresses = new Address[recipients.countTokens()];
     40 
     41 		while (recipients.hasMoreTokens()) {
     42 			try {
     43 				addresses[i++] = new InternetAddress(recipients.nextToken());
     44 			} catch (AddressException e) {
     45 				System.out.println("Unable to create address");
     46 			}
     47 		}
     48 		return addresses;
     49 	}
     50 
     51 	public Mailer(){
     52 		this("monitor.properties");
     53 	}
     54 	public Mailer(String buildPropertiesPath){
     55 		buildProperties = new BuildProperties(buildPropertiesPath);
     56 		if (buildProperties.getHost().equals("")||buildProperties.getSender().equals("")||buildProperties.getToRecipientList().equals("")){
     57 			sendMail=false;
     58 		}
     59 	}
     60 
     61 
     62 	public static void main(String args[]) {
     63 		Mailer mailer=new Mailer(args[0]);
     64 		mailer.sendTextMessage(args[1],args[2]);
     65 	}
     66 
     67 	public void sendMessage(String aSubject, String aMessage) {
     68 		if (aSubject == null || aMessage == null || sendMail == false){
     69 			printEmailFailureNotice(aSubject,aMessage);
     70 		}
     71 
     72 		// Get system properties
     73 		Properties props = System.getProperties();
     74 
     75 		// Setup mail server
     76 		props.put("mail.smtp.host", buildProperties.getHost());
     77 
     78 		// Get session
     79 		Session session = Session.getDefaultInstance(props, null);
     80 
     81 		// Define message
     82 		MimeMessage message = new MimeMessage(session);
     83 
     84 		try {
     85 
     86 			// Set the from address
     87 			message.setFrom(new InternetAddress(buildProperties.getSender()));
     88 
     89 			// Set the to address
     90 			message.addRecipients(Message.RecipientType.TO, getAddresses(buildProperties.getToRecipientList()));
     91 
     92 			// Set the subject
     93 			message.setSubject(buildProperties.getBuildSubjectPrefix()+
     94 						"Build "
     95 							+ buildProperties.getBuildid()
     96 							+ " (Timestamp:  "
     97 							+ buildProperties.getTimestamp()
     98 							+ "):"
     99 							+ aSubject);
    100 
    101 			// Set the content
    102 			message.setText(
    103 				"Build "
    104 					+ buildProperties.getBuildid()
    105 					+ " (Timestamp: "
    106 					+ buildProperties.getTimestamp()
    107 					+ "):  "
    108 					+ aMessage);
    109 
    110 			// Send message
    111 			Transport.send(message);
    112 
    113 		} catch (AddressException e) {
    114 			e.printStackTrace();
    115 		} catch (MessagingException e) {
    116 			e.printStackTrace();
    117 		}
    118 	}
    119 
    120 	public void sendTextMessage(String aSubject, String aMessage) {
    121 		if (aSubject == null || aMessage == null || sendMail == false){
    122 			printEmailFailureNotice(aSubject,aMessage);
    123 		}
    124 
    125 		// Get system properties
    126 		Properties props = System.getProperties();
    127 
    128 		// Setup mail server
    129 		props.put("mail.smtp.host", buildProperties.getHost());
    130 
    131 		// Get session
    132 		Session session = Session.getDefaultInstance(props, null);
    133 
    134 		// Define message
    135 		MimeMessage message = new MimeMessage(session);
    136 
    137 		try {
    138 
    139 			// Set the from address
    140 			message.setFrom(new InternetAddress(buildProperties.getSender()));
    141 
    142 			// Set the to address
    143 			message.addRecipients(Message.RecipientType.BCC, getAddresses(buildProperties.getTextRecipientList()));
    144 
    145 			// Set the subject
    146 			message.setSubject(buildProperties.getBuildSubjectPrefix()+
    147 						"Build "
    148 							+ buildProperties.getBuildid()
    149 							+ ":"
    150 							+ aSubject);
    151 
    152 			// Set the content
    153 			message.setText(aMessage);
    154 
    155 			// Send message
    156 			Transport.send(message);
    157 
    158 		} catch (AddressException e) {
    159 			e.printStackTrace();
    160 		} catch (MessagingException e) {
    161 			e.printStackTrace();
    162 		}
    163 	}
    164 
    165 	public void sendMultiPartMessage(
    166 	// a method for sending mail with attachments
    167 	String aSubject, String aMessage, String[] attachments) {
    168 		if (aSubject == null || aMessage == null || sendMail == false){
    169 			printEmailFailureNotice(aSubject,aMessage);
    170 		}
    171 
    172 		// Get system properties
    173 		Properties props = System.getProperties();
    174 
    175 		// Setup mail server
    176 		props.put("mail.smtp.host", buildProperties.getHost());
    177 
    178 		// Get session
    179 		Session session = Session.getDefaultInstance(props, null);
    180 
    181 		// Define message
    182 		MimeMessage message = new MimeMessage(session);
    183 
    184 		Multipart mp = new MimeMultipart();
    185 
    186 		try {
    187 			// Set the from address
    188 			message.setFrom(new InternetAddress(buildProperties.getSender()));
    189 
    190 			// Set the to address
    191 			message.addRecipients(Message.RecipientType.TO, getAddresses(buildProperties.getToRecipientList()));
    192 
    193 			// Set the subject
    194 			message.setSubject(buildProperties.getBuildSubjectPrefix()+
    195 			"Build "
    196 				+ buildProperties.getBuildid()
    197 				+ " (Timestamp:  "
    198 				+ buildProperties.getTimestamp()
    199 				+ "):"
    200 				+ aSubject);
    201 
    202 			// create and fill the first message part
    203 			MimeBodyPart part = new MimeBodyPart();
    204 			part.setText(
    205 				"Build "
    206 					+ buildProperties.getBuildid()
    207 					+ " (Timestamp: "
    208 					+ buildProperties.getTimestamp()
    209 					+ "):  "
    210 					+ aMessage);
    211 			mp.addBodyPart(part);
    212 
    213 			//for each attachment create new message part
    214 			for (int i = 0; i < attachments.length; i++) {
    215 				MimeBodyPart attachmentPart = new MimeBodyPart();
    216 				// attach the file to the message
    217 				FileDataSource attachment = new FileDataSource(attachments[i]);
    218 				attachmentPart.setDataHandler(new DataHandler(attachment));
    219 				File attachmentFile=new File(attachments[i]);
    220 				attachmentPart.setFileName(attachmentFile.getParent()+"-"+attachmentFile.getName());
    221 				mp.addBodyPart(attachmentPart);
    222 			}
    223 
    224 			// add the Multipart to the message
    225 			message.setContent(mp);
    226 
    227 			Transport.send(message);
    228 
    229 		} catch (AddressException e) {
    230 		} catch (MessagingException e) {
    231 		}
    232 	}
    233 
    234 	private void printEmailFailureNotice(String aSubject, String aMessage){
    235 		System.out.println("Email failed.  Settings:");
    236 		System.out.println("\nhost="+buildProperties.getHost()+"\nsender="+buildProperties.getSender()+"\nrecipients="+buildProperties.getToRecipientList());
    237 		System.out.println("\nSubject="+aSubject+"\nMessage="+aMessage);
    238 		return;
    239 	}
    240 
    241 	/**
    242 	 * Returns the buildProperties.
    243 	 * @return BuildProperties
    244 	 */
    245 	public BuildProperties getBuildProperties() {
    246 		return buildProperties;
    247 	}
    248 
    249 	/**
    250 	 * Sets the buildProperties.
    251 	 * @param buildProperties The buildProperties to set
    252 	 */
    253 	public void setBuildProperties(BuildProperties buildProperties) {
    254 		this.buildProperties = buildProperties;
    255 	}
    256 
    257 }
    258