1 19 20 package za.org.coefficient.util.common; 21 22 import za.org.coefficient.core.Constants; 23 24 import java.util.Properties ; 25 26 import javax.mail.*; 27 import javax.mail.internet.*; 28 29 33 public class MailUtil { 34 36 private static Session session = null; 37 38 static { 39 Properties props = new Properties (); 40 props.put("mail.smtp.host", Constants.MAIL_SMTP_HOST); 41 props.put("mail.from", Constants.MAIL_SENDER_ADDRESS); 42 props.put("mail.transport.protocol", "smtp"); 43 session = Session.getDefaultInstance(props, null); 44 } 45 46 48 public static boolean sendEmail(String message, String subject, 49 String recipient, String sender) { 50 if (session == null) { 51 return false; 52 } 53 boolean retVal = true; 54 try { 55 MimeMessage msg = new MimeMessage(session); 56 msg.setContent(message, "text/html"); 57 Address recp = new InternetAddress(recipient); 58 if (sender != null) { 59 Address snd = new InternetAddress(sender); 60 msg.setFrom(snd); 61 } 62 msg.addRecipient(Message.RecipientType.TO, recp); 63 msg.setSubject(subject); 64 Transport.send(msg); 65 } catch (MessagingException e) { 66 e.printStackTrace(); 67 retVal = false; 68 } 69 70 return retVal; 71 } 72 } 73 | Popular Tags |