blob: 358c9ae671a9ac107abf84d4dd123449e27a5339 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2000, 2003 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.wtp.releng.monitor;
/**
* A Class that sends build related email messages. host, sender, recipient and
* build related information set in monitor.properties
*/
import java.util.Properties;
import java.util.StringTokenizer;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class WtpMailer {
// flag that determines whether or not to send mail
boolean sendMail = true;
// the mail host
String host = "127.0.0.1";
// the mail from
String from = "";
// the mail recipeints
String recipientsList = "";
// the email subject prefix
String prefix = "";
//build label
String buildLabel = "";
//convert the comma separated list of email addressed into an array of Address objects
private Address[] getAddresses() {
int i = 0;
StringTokenizer recipients = new StringTokenizer(recipientsList, ",");
Address[] addresses = new Address[recipients.countTokens()];
while (recipients.hasMoreTokens()) {
try {
addresses[i++] = new InternetAddress(recipients.nextToken());
} catch (AddressException e) {
System.out.println("Unable to create address");
}
}
return addresses;
}
public WtpMailer(String host, String sender, String recpst, String prefix, String buildLabel ){
if(host == null || host.length() == 0 || host.indexOf(" ") >= 0)
sendMail=false;
this.host = host;
this.from = sender;
this.recipientsList = recpst;
this.prefix = prefix;
this.buildLabel = buildLabel;
}
public void sendMessage(String aSubject, String aMessage) {
if (aSubject == null || aMessage == null || sendMail == false){
printEmailFailureNotice(aSubject,aMessage);
}
// Get system properties
Properties props = System.getProperties();
// Setup mail server
props.put("mail.smtp.host", host);
// Get session
Session session = Session.getDefaultInstance(props);
MimeMessage message = new MimeMessage(session);
try {
// Set the from address
message.setFrom(new InternetAddress(from));
// Set the to address
message.addRecipients(Message.RecipientType.TO, getAddresses());
// Set the subject
message.setSubject(prefix + "[" + buildLabel +"] - "+ aSubject);
// Set the content
message.setContent(
"<a href=\"http://download.eclipse.org/webtools/downloads/drops/"+buildLabel+"\">Build is ready at: "
+ buildLabel +"</a>\n\n"
+ aMessage, "text/html");
// Send message
Transport.send(message);
} catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
}
private void printEmailFailureNotice(String aSubject, String aMessage){
System.out.println("Email failed. Settings:");
System.out.println("\nhost="+host+"\nsender="+from+"\nrecipients="+recipientsList);
System.out.println("\nSubject="+aSubject+"\nMessage="+aMessage);
return;
}
}