1 31 32 package org.opencms.workplace.tools.workplace.broadcast; 33 34 import org.opencms.file.CmsObject; 35 import org.opencms.mail.CmsSimpleMail; 36 import org.opencms.main.CmsIllegalArgumentException; 37 import org.opencms.main.OpenCms; 38 import org.opencms.util.CmsStringUtil; 39 40 import java.util.ArrayList ; 41 import java.util.Collections ; 42 import java.util.List ; 43 import java.util.StringTokenizer ; 44 45 import javax.mail.Address ; 46 import javax.mail.SendFailedException ; 47 import javax.mail.internet.AddressException ; 48 import javax.mail.internet.InternetAddress ; 49 50 59 public class CmsMessageInfo { 60 61 62 private String m_cc = ""; 63 64 65 private String m_from = ""; 66 67 68 private String m_msg = ""; 69 70 71 private String m_subject = ""; 72 73 74 private String m_to = ""; 75 76 79 public CmsMessageInfo() { 80 81 } 83 84 89 public String getCc() { 90 91 return m_cc; 92 } 93 94 99 public String getFrom() { 100 101 return m_from; 102 } 103 104 109 public String getMsg() { 110 111 return m_msg; 112 } 113 114 119 public String getSubject() { 120 121 return m_subject; 122 } 123 124 129 public String getTo() { 130 131 return m_to; 132 } 133 134 141 public void sendEmail(CmsObject cms) throws Exception { 142 143 CmsSimpleMail theMail = new CmsSimpleMail(); 145 theMail.setCharset(cms.getRequestContext().getEncoding()); 146 theMail.setFrom(cms.getRequestContext().currentUser().getEmail(), getFrom()); 147 theMail.setTo(createInternetAddresses(getTo())); 148 if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(getCc())) { 149 theMail.setCc(createInternetAddresses(getCc())); 150 } 151 theMail.setSubject("[" + OpenCms.getSystemInfo().getServerName() + "] " + getSubject()); 152 theMail.setMsg(getMsg()); 153 try { 155 theMail.send(); 156 } catch (SendFailedException sf) { 157 StringBuffer newTo = new StringBuffer (); 159 Address [] unsent = sf.getValidUnsentAddresses(); 160 if (unsent != null) { 161 for (int i = unsent.length - 1; i >= 0; i--) { 162 newTo.append(unsent[i].toString()).append(';'); 163 } 164 } 165 if (unsent != null) { 166 unsent = sf.getInvalidAddresses(); 167 for (int i = unsent.length - 1; i >= 0; i--) { 168 newTo.append(unsent[i].toString()).append(';'); 169 } 170 } 171 172 setTo(newTo.toString()); 173 throw (Exception )sf.getCause(); 175 176 } 177 } 178 179 184 public void setCc(String cc) { 185 186 if (!CmsStringUtil.isEmptyOrWhitespaceOnly(cc)) { 187 m_cc = cc; 188 } 189 } 190 191 196 public void setFrom(String from) { 197 198 checkString(from); 199 m_from = from; 200 } 201 202 207 public void setMsg(String msg) { 208 209 checkString(msg); 210 m_msg = msg; 211 } 212 213 218 public void setSubject(String subject) { 219 220 checkString(subject); 221 m_subject = subject; 222 } 223 224 232 public void setTo(String to) { 233 234 m_to = to; 235 } 236 237 242 private void checkString(String string) { 243 244 if (CmsStringUtil.isEmptyOrWhitespaceOnly(string)) { 245 throw new CmsIllegalArgumentException(Messages.get().container(Messages.ERR_EMPTY_STRING_0)); 246 } 247 } 248 249 256 private List createInternetAddresses(String mailAddresses) throws AddressException { 257 258 if (CmsStringUtil.isNotEmpty(mailAddresses)) { 259 StringTokenizer T = new StringTokenizer (mailAddresses, ";"); 261 List addresses = new ArrayList (T.countTokens()); 262 while (T.hasMoreTokens()) { 263 InternetAddress address = new InternetAddress (T.nextToken().trim()); 264 addresses.add(address); 265 } 266 return addresses; 267 } else { 268 return Collections.EMPTY_LIST; 270 } 271 } 272 273 } 274 | Popular Tags |