root / branches / 0.8 / linshare / src / main / java / org / linagora / linShare / core / service / impl / MailNotifierServiceImpl.java @ 213
History | View | Annotate | Download (7.6 KB)
| 1 | /*
|
|---|---|
| 2 | * This file is part of Linshare. |
| 3 | * |
| 4 | * Linshare is free software: you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU Affero General Public License as |
| 6 | * published by the Free Software Foundation, either version 3 of |
| 7 | * the License, or (at your option) any later version. |
| 8 | * |
| 9 | * Linshare is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU Affero General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Affero General Public |
| 15 | * License along with Foobar. If not, see |
| 16 | * <http://www.gnu.org/licenses/>. |
| 17 | * |
| 18 | * (c) 2008 Groupe Linagora - http://linagora.org |
| 19 | * |
| 20 | */ |
| 21 | package org.linagora.linShare.core.service.impl;
|
| 22 | |
| 23 | import java.util.Properties; |
| 24 | |
| 25 | import javax.activation.DataHandler; |
| 26 | import javax.mail.BodyPart; |
| 27 | import javax.mail.MessagingException; |
| 28 | import javax.mail.Multipart; |
| 29 | import javax.mail.Session; |
| 30 | import javax.mail.Transport; |
| 31 | import javax.mail.internet.InternetAddress; |
| 32 | import javax.mail.internet.MimeBodyPart; |
| 33 | import javax.mail.internet.MimeMessage; |
| 34 | import javax.mail.internet.MimeMultipart; |
| 35 | import javax.mail.util.ByteArrayDataSource; |
| 36 | |
| 37 | import org.apache.commons.logging.Log; |
| 38 | import org.apache.commons.logging.LogFactory; |
| 39 | import org.linagora.linShare.core.exception.TechnicalErrorCode; |
| 40 | import org.linagora.linShare.core.exception.TechnicalException; |
| 41 | import org.linagora.linShare.core.service.NotifierService; |
| 42 | |
| 43 | |
| 44 | /**
|
| 45 | * |
| 46 | */ |
| 47 | public class MailNotifierServiceImpl implements NotifierService { |
| 48 | |
| 49 | /** The smtpServer that will send the email. */
|
| 50 | private final String smtpServer; |
| 51 | |
| 52 | /** The smtp user. */
|
| 53 | private final String smtpUser; |
| 54 | |
| 55 | /** The sender identity. */
|
| 56 | private final String smtpSender; |
| 57 | |
| 58 | /** The smtp password. */
|
| 59 | private final String smtpPassword; |
| 60 | |
| 61 | /** The smtp port. */
|
| 62 | private final int smtpPort; |
| 63 | |
| 64 | /** Is the server needs authentification. */
|
| 65 | private final boolean needsAuth; |
| 66 | |
| 67 | /** Mail charset. */
|
| 68 | private final String charset; |
| 69 | |
| 70 | |
| 71 | private final static Log logger = LogFactory.getLog(MailNotifierServiceImpl.class); |
| 72 | |
| 73 | |
| 74 | /**
|
| 75 | * see http://java.sun.com/developer/EJTechTips/2004/tt0625.html for multipart/alternative |
| 76 | * @param smtpServer |
| 77 | * @param smtpSender |
| 78 | * @param smtpUser |
| 79 | * @param smtpPassword |
| 80 | * @param needsAuth |
| 81 | * @param charset |
| 82 | */ |
| 83 | |
| 84 | public MailNotifierServiceImpl(String smtpServer,int smtpPort, String smtpSender, String smtpUser, String smtpPassword, |
| 85 | boolean needsAuth, String charset) { |
| 86 | this.smtpServer = smtpServer;
|
| 87 | this.smtpPort = smtpPort;
|
| 88 | this.smtpSender = smtpSender;
|
| 89 | this.smtpUser = smtpUser;
|
| 90 | this.smtpPassword = smtpPassword;
|
| 91 | this.needsAuth = needsAuth;
|
| 92 | this.charset = charset;
|
| 93 | } |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | /** Send notification to a recipient.
|
| 101 | * @param recipient notification recipient. |
| 102 | * @param subject subject. |
| 103 | * @param content content. |
| 104 | */ |
| 105 | public void sendNotification(String fromUser, String recipient, String subject, String htmlContent, String textContent) { |
| 106 | |
| 107 | // get the mail session
|
| 108 | Session session = getMailSession(); |
| 109 | |
| 110 | // Define message
|
| 111 | MimeMessage messageMim = new MimeMessage(session);
|
| 112 | try {
|
| 113 | |
| 114 | if(fromUser==null) |
| 115 | messageMim.setFrom(new InternetAddress(smtpSender));
|
| 116 | else
|
| 117 | messageMim.setFrom(new InternetAddress(fromUser));
|
| 118 | |
| 119 | messageMim.addRecipient(javax.mail.Message.RecipientType.TO, |
| 120 | new InternetAddress(recipient));
|
| 121 | |
| 122 | messageMim.setSubject(subject,charset); |
| 123 | |
| 124 | |
| 125 | |
| 126 | // Create a "related" Multipart message
|
| 127 | //content type is multipart/alternative
|
| 128 | //it will contain two part BodyPart 1 and 2
|
| 129 | Multipart mp = new MimeMultipart("alternative"); |
| 130 | |
| 131 | //BodyPart 1 is text file
|
| 132 | BodyPart alt_bp1 = new MimeBodyPart();
|
| 133 | alt_bp1.setDataHandler(new DataHandler(new ByteArrayDataSource(textContent,"text/plain"))); |
| 134 | mp.addBodyPart(alt_bp1); |
| 135 | |
| 136 | |
| 137 | //BodyPart 2
|
| 138 | //content type is multipart/related
|
| 139 | //A multipart/related is used to indicate that message parts should not be considered individually but rather
|
| 140 | //as parts of an aggregate whole. The message consists of a root part (by default, the first) which reference other parts inline,
|
| 141 | //which may in turn reference other parts.
|
| 142 | Multipart html_mp = new MimeMultipart("related"); |
| 143 | |
| 144 | //Include an HTML message with images.
|
| 145 | //BodyParts: the HTML file and an image
|
| 146 | |
| 147 | // Get the HTML file
|
| 148 | BodyPart rel_bph = new MimeBodyPart();
|
| 149 | rel_bph.setDataHandler(new DataHandler(new ByteArrayDataSource(htmlContent,"text/html; charset="+charset))); |
| 150 | html_mp.addBodyPart(rel_bph); |
| 151 | |
| 152 | |
| 153 | //inline image ?
|
| 154 | String cid = "image.part.1@linshare.org"; |
| 155 | MimeBodyPart rel_bpi = new MimeBodyPart();
|
| 156 | // Initialize and add the image file to the html body part
|
| 157 | rel_bpi.setFileName("mail_logo.png");
|
| 158 | rel_bpi.setText("linshare");
|
| 159 | |
| 160 | |
| 161 | rel_bpi.setDataHandler(new DataHandler(getClass().getResource("/org/linagora/linShare/core/service/mail_logo.png"))); |
| 162 | rel_bpi.setHeader("Content-ID", "<" + cid + ">"); |
| 163 | rel_bpi.setDisposition("inline");
|
| 164 | html_mp.addBodyPart(rel_bpi); |
| 165 | |
| 166 | // Create the second BodyPart of the multipart/alternative,
|
| 167 | // set its content to the html multipart, and add the
|
| 168 | // second bodypart to the main multipart.
|
| 169 | BodyPart alt_bp2 = new MimeBodyPart();
|
| 170 | alt_bp2.setContent(html_mp); |
| 171 | mp.addBodyPart(alt_bp2); |
| 172 | |
| 173 | messageMim.setContent(mp); |
| 174 | // Since we used html tags, the content must be marker as text/html
|
| 175 | //messageMim.setContent(content,"text/html; charset="+charset);
|
| 176 | |
| 177 | |
| 178 | Transport tr = session.getTransport("smtp");
|
| 179 | |
| 180 | // Connect to smtp server, if needed
|
| 181 | if (needsAuth) {
|
| 182 | tr.connect(smtpServer, smtpPort ,smtpUser, smtpPassword); |
| 183 | messageMim.saveChanges(); |
| 184 | tr.sendMessage(messageMim, messageMim.getAllRecipients()); |
| 185 | tr.close(); |
| 186 | } else {
|
| 187 | // Send message
|
| 188 | Transport.send(messageMim); |
| 189 | } |
| 190 | } catch (MessagingException e) {
|
| 191 | logger.error("Error sending notification on "+smtpServer+" port "+smtpPort,e); |
| 192 | throw new TechnicalException(TechnicalErrorCode.MAIL_EXCEPTION, "Error sending notification",e); |
| 193 | } catch (Exception e) { |
| 194 | logger.error("Error sending notification on "+smtpServer+" port "+smtpPort,e); |
| 195 | throw new TechnicalException(TechnicalErrorCode.MAIL_EXCEPTION, "Error sending notification",e); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | |
| 200 | private Session getMailSession(){
|
| 201 | |
| 202 | // create some properties and get the default Session
|
| 203 | |
| 204 | // Set the host smtp address
|
| 205 | Properties props = new Properties(); |
| 206 | props.put("mail.smtp.host", smtpServer);
|
| 207 | props.put("mail.smtp.port", smtpPort+""); |
| 208 | |
| 209 | if (needsAuth){
|
| 210 | props.put("mail.smtp.auth", "true"); |
| 211 | }else{
|
| 212 | props.put("mail.smtp.auth", "false"); |
| 213 | } |
| 214 | // create some properties and get the default Session
|
| 215 | Session session = Session.getInstance(props, null);
|
| 216 | if(logger.isDebugEnabled()){
|
| 217 | session.setDebug(true);
|
| 218 | } else {
|
| 219 | session.setDebug(false);
|
| 220 | } |
| 221 | |
| 222 | return session;
|
| 223 | } |
| 224 | |
| 225 | } |