1 37 38 package com.sun.j2ee.blueprints.opc.mailer; 39 40 import java.util.*; 41 42 import javax.naming.InitialContext ; 43 import javax.activation.DataHandler ; 44 import javax.mail.*; 45 import javax.mail.internet.*; 46 47 48 51 public class MailHelper { 52 53 58 public void createAndSendMail(String emailAddress, String subject, String mailContent, Locale locale) throws MailerException { 59 try { 60 InitialContext ic = new InitialContext (); 61 Session session = (Session) ic.lookup(JNDINames.MAIL_SESSION); 62 Message msg = new MimeMessage(session); 63 msg.setFrom(); 64 msg.setRecipients(Message.RecipientType.TO, 65 InternetAddress.parse(emailAddress, false)); 66 msg.setSubject(subject); 67 String contentType = "text/html"; 68 StringBuffer sb = new StringBuffer (mailContent); 69 msg.setDataHandler(new DataHandler ( 70 new ByteArrayDataSource(sb.toString(), contentType))); 71 msg.setHeader("X-Mailer", "JavaMailer"); 72 msg.setSentDate(new Date()); 73 Transport.send(msg); 74 } catch (Exception e) { 75 System.err.println("MailHelper caught: " + e); 76 e.printStackTrace(); 77 throw new MailerException("Failure while sending mail:" + e); 78 } 79 } 80 } 81 82 | Popular Tags |