java email

		Properties props = new Properties();
		props.put("mail.host",mailServer);
		props.put("mail.transport.protocol", "smtp");

		// create some properties and get the default Session
		Session session = Session.getDefaultInstance(props, null);
		session.setDebug(debug);

		// create a message
		Message msg = new MimeMessage(session);

		// set the from and to address
		InternetAddress addressFrom = new InternetAddress(from);
		msg.setFrom(addressFrom);

		InternetAddress[] addressTo = new InternetAddress[recipients.length];
		for (int i = 0; i < recipients.length; i++) {
			addressTo[i] = new InternetAddress(recipients[i]);
		}
		msg.setRecipients(Message.RecipientType.TO, addressTo);

		// Optional : You can also set your custom headers in the Email if you
		// Want
		msg.addHeader("MyHeaderName", "myHeaderValue");

		
		// Create the message part 
		BodyPart messageBodyPart = new MimeBodyPart();

		// Fill the message
		if((Integer)results.get("recentRun") > 0) {
			msg.setSubject(MessageFormat.format(subject, ""));	
			//MessageFormat.format(subject, "is running");//dynamic properties placeholder ??
			messageBodyPart.setContent(MessageFormat.format((String) message,"",SYD.format(results.get("latestRun"))),"text/html");
		}
		else {
			msg.setSubject(MessageFormat.format(subject, "NOT "));
			//MessageFormat.format(subject, "is not running");//dynamic properties placeholder ??
			messageBodyPart.setContent(MessageFormat.format((String) message,"<b><FONT COLOR='RED'>NOT</FONT></b> ",SYD.format(results.get("latestRun"))),"text/html");
		}
		
		Multipart multipart = new MimeMultipart();
		multipart.addBodyPart(messageBodyPart);

/*		
 * // Part two is attachment
		messageBodyPart = new MimeBodyPart();
		//DataSource source = new FileDataSource("C:\\OOLCheck\\OOLReport.pdf");
		DataSource source = new FileDataSource("OOLReport.pdf");
		messageBodyPart.setDataHandler(new DataHandler(source));
		messageBodyPart.setFileName("OOLReport.pdf");

		multipart.addBodyPart(messageBodyPart);
		
		messageBodyPart = new MimeBodyPart();
		//DataSource source2 = new FileDataSource("C:\\OOLCheck\\OOLReport_OEReport.pdf");
		DataSource source2 = new FileDataSource("OOLReport_OEReport.pdf");
		messageBodyPart.setDataHandler(new DataHandler(source2));
		messageBodyPart.setFileName("OOLReport_OEReport.pdf");

		multipart.addBodyPart(messageBodyPart);

*/		// Put parts in message
		msg.setContent(multipart,"text/html");
		
		try {
			Transport.send(msg);
			}catch(MessagingException e){
			}
Advertisements

Author: lwpro2

Java J2EE professional

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s