001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software GmbH & Co. KG, please see the 018 * company website: http://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: http://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.workplace.tools.workplace.broadcast; 029 030import org.opencms.file.CmsObject; 031import org.opencms.mail.CmsSimpleMail; 032import org.opencms.main.CmsIllegalArgumentException; 033import org.opencms.main.OpenCms; 034import org.opencms.util.CmsStringUtil; 035 036import java.util.ArrayList; 037import java.util.Collections; 038import java.util.List; 039import java.util.StringTokenizer; 040 041import javax.mail.internet.AddressException; 042import javax.mail.internet.InternetAddress; 043 044/** 045 * Bean class for message information.<p> 046 * 047 * @since 6.0.0 048 */ 049public class CmsMessageInfo { 050 051 /** Cc header string. */ 052 private String m_cc = ""; 053 054 /** From header string. */ 055 private String m_from = ""; 056 057 /** Message string. */ 058 private String m_msg = ""; 059 060 /** Subject header string. */ 061 private String m_subject = ""; 062 063 /** To header string. */ 064 private String m_to = ""; 065 066 /** 067 * Default Constructor.<p> 068 */ 069 public CmsMessageInfo() { 070 071 // noop 072 } 073 074 /** 075 * Returns the cc string.<p> 076 * 077 * @return the cc string 078 */ 079 public String getCc() { 080 081 return m_cc; 082 } 083 084 /** 085 * Returns the from string.<p> 086 * 087 * @return the from string 088 */ 089 public String getFrom() { 090 091 return m_from; 092 } 093 094 /** 095 * Returns the message string.<p> 096 * 097 * @return the message string 098 */ 099 public String getMsg() { 100 101 return m_msg; 102 } 103 104 /** 105 * Returns the subject string.<p> 106 * 107 * @return the subject string 108 */ 109 public String getSubject() { 110 111 return m_subject; 112 } 113 114 /** 115 * Returns the to string.<p> 116 * 117 * @return the to string 118 */ 119 public String getTo() { 120 121 return m_to; 122 } 123 124 /** 125 * Sends the given message to the given addresses.<p> 126 * 127 * @param cms the cms context 128 * 129 * @throws Exception if something goes wrong 130 */ 131 public void sendEmail(CmsObject cms) throws Exception { 132 133 // create a plain text email 134 CmsSimpleMail theMail = new CmsSimpleMail(); 135 theMail.setCharset(cms.getRequestContext().getEncoding()); 136 theMail.setFrom(cms.getRequestContext().getCurrentUser().getEmail(), getFrom()); 137 theMail.setTo(createInternetAddresses(getTo())); 138 if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(getCc())) { 139 theMail.setCc(createInternetAddresses(getCc())); 140 } 141 theMail.setSubject("[" + OpenCms.getSystemInfo().getServerName() + "] " + getSubject()); 142 theMail.setMsg(getMsg()); 143 // send the mail 144 theMail.send(); 145 } 146 147 /** 148 * Sets the cc string.<p> 149 * 150 * @param cc the cc string 151 */ 152 public void setCc(String cc) { 153 154 if (!CmsStringUtil.isEmptyOrWhitespaceOnly(cc)) { 155 m_cc = cc; 156 } 157 } 158 159 /** 160 * Sets the from string.<p> 161 * 162 * @param from the from string 163 */ 164 public void setFrom(String from) { 165 166 checkString(from); 167 m_from = from; 168 } 169 170 /** 171 * Sets the message string.<p> 172 * 173 * @param msg the message string 174 */ 175 public void setMsg(String msg) { 176 177 checkString(msg); 178 m_msg = msg; 179 } 180 181 /** 182 * Sets the subject string.<p> 183 * 184 * @param subject the subject string 185 */ 186 public void setSubject(String subject) { 187 188 checkString(subject); 189 m_subject = subject; 190 } 191 192 /** 193 * Sets the to string.<p> 194 * 195 * This has to be a ';' separated string of email-addresses.<p> 196 * 197 * 198 * @param to the to string 199 */ 200 public void setTo(String to) { 201 202 m_to = to; 203 } 204 205 /** 206 * Throws a runtime exception if the string is null, empty or contains JavaScript.<p> 207 * 208 * @param string the string to check 209 */ 210 private void checkString(String string) { 211 212 if (CmsStringUtil.isEmptyOrWhitespaceOnly(string)) { 213 throw new CmsIllegalArgumentException(Messages.get().container(Messages.ERR_EMPTY_STRING_0)); 214 } 215 } 216 217 /** 218 * Creates a list of internet addresses (email) from a semicolon separated String.<p> 219 * 220 * @param mailAddresses a semicolon separated String with email addresses 221 * @return list of internet addresses (email) 222 * @throws AddressException if an email address is not correct 223 */ 224 private List<InternetAddress> createInternetAddresses(String mailAddresses) throws AddressException { 225 226 if (CmsStringUtil.isNotEmpty(mailAddresses)) { 227 // at least one email address is present, generate list 228 StringTokenizer T = new StringTokenizer(mailAddresses, ";"); 229 List<InternetAddress> addresses = new ArrayList<InternetAddress>(T.countTokens()); 230 while (T.hasMoreTokens()) { 231 InternetAddress address = new InternetAddress(T.nextToken().trim()); 232 addresses.add(address); 233 } 234 return addresses; 235 } else { 236 // no address given, return empty list 237 return Collections.emptyList(); 238 } 239 } 240 241}