1 25 26 package org.snipsnap.util.mail; 27 28 import org.radeox.util.logging.Logger; 29 import org.snipsnap.app.Application; 30 import org.snipsnap.config.Configuration; 31 32 import javax.mail.*; 33 import javax.mail.internet.InternetAddress ; 34 import javax.mail.internet.MimeBodyPart ; 35 import javax.mail.internet.MimeMessage ; 36 import javax.mail.internet.MimeMultipart ; 37 import java.net.InetAddress ; 38 import java.net.MalformedURLException ; 39 import java.net.URL ; 40 import java.net.UnknownHostException ; 41 import java.util.ArrayList ; 42 import java.util.Collection ; 43 import java.util.Iterator ; 44 import java.util.Properties ; 45 46 52 53 public class Mail { 54 public static Mail instance; 55 56 public static synchronized Mail getInstance() { 57 if (null == instance) { 58 instance = new Mail(); 59 } 60 return instance; 61 } 62 63 private Session session; 64 65 public Mail() { 66 Properties props = new Properties (); 67 Configuration config = Application.get().getConfiguration(); 68 String mailhost = config.getMailHost(); 69 InetAddress addr = null; 70 try { 71 addr = InetAddress.getByName(mailhost); 72 } catch (UnknownHostException e) { 73 Logger.debug("Mail: '" + mailhost + "': unknown host address"); 74 try { 75 addr = InetAddress.getByName("mailhost"); 76 } catch (UnknownHostException e1) { 77 try { 78 addr = InetAddress.getByName("mail"); 79 } catch (UnknownHostException e2) { 80 Logger.debug("Mail: unable to detect mail host automatically, please configure by hand"); 81 } 82 } 83 } 84 85 if (null != mailhost) { 86 props.put("mail.smtp.host", addr.getHostName()); 87 session = Session.getDefaultInstance(props, null); 88 } 90 } 91 92 93 public void sendMail(String sender, String recipient, String subject, String content) { 94 Collection recientList = new ArrayList (); 95 recientList.add(recipient); 96 sendMail(sender, recientList, subject, content); 97 } 98 99 public void sendMail(String recipient, String subject, String content) { 100 Collection recipientList = new ArrayList (); 101 recipientList.add(recipient); 102 Configuration configuration = Application.get().getConfiguration(); 104 String sender = configuration.getMailDomain(); 105 if (null == sender) { 106 sender = configuration.getUrl(); 107 try { 108 sender = new URL (sender).getHost(); 109 } catch (MalformedURLException e) { 110 sender = "this-is-a-bug.org"; 111 } 112 } 113 sendMail("do-not-reply@" + sender, recipientList, subject, content); 114 } 115 116 public void sendMail(String sender, Collection recipientList, String subject, String content) { 117 try { 118 Message mesg = new MimeMessage (session); 119 mesg.setFrom(new InternetAddress (sender)); 120 Iterator iterator = recipientList.iterator(); 122 while (iterator.hasNext()) { 123 String recpt = (String ) iterator.next(); 124 mesg.addRecipient(Message.RecipientType.TO, new InternetAddress (recpt)); 125 } 126 127 mesg.setSubject(subject); 131 132 Multipart mp = new MimeMultipart (); 134 135 138 BodyPart htmlPart = new MimeBodyPart (); 139 htmlPart.setContent(content, "text/html"); 140 mp.addBodyPart(htmlPart); 141 mesg.setContent(mp); 142 143 Transport.send(mesg); 144 145 } catch (MessagingException ex) { 146 Exception e; 147 if ((e = ex.getNextException()) != null) { 148 Logger.warn(ex.getMessage(), e); 149 } 150 } 151 return; 152 } 153 } 154 | Popular Tags |