1 5 package org.exoplatform.services.communication.message.impl; 6 7 import java.util.Properties ; 8 import javax.mail.*; 9 import javax.mail.Session ; 10 import javax.mail.Transport ; 11 import javax.mail.internet.InternetAddress ; 12 import javax.mail.internet.MimeMessage ; 13 import org.exoplatform.services.communication.message.*; 14 import org.exoplatform.commons.exception.ExoMessageException; 15 import org.exoplatform.container.configuration.*; 16 import org.exoplatform.container.configuration.ConfigurationManager; 17 22 public class MailServiceImpl implements MailService { 23 private Session mailSession_; 24 private String smtpServer_ ; 25 26 public MailServiceImpl(ConfigurationManager confService) throws Exception { 27 ServiceConfiguration sconf = confService.getServiceConfiguration(MailService.class) ; 28 ValueParam param = (ValueParam)sconf.getParameter("outgoing.mail.server") ; 29 smtpServer_ = param.getValue() ; 30 Properties props = System.getProperties(); 31 props.put("mail.smtp.host", smtpServer_); 32 ExoAuthenticator auth = new ExoAuthenticator("exo.platform", "exo2004") ; 33 mailSession_ = Session.getDefaultInstance(props, null); 34 } 35 36 public Session getMailSession() { return mailSession_ ; } 37 38 public String getOutgoingMailServer() { return smtpServer_ ; } 39 40 public void sendMessage(org.exoplatform.services.communication.message.Message message) throws Exception { 41 try { 42 MimeMessage mimeMessage = new MimeMessage (mailSession_); 43 InternetAddress author = new InternetAddress (message.getFrom()); 44 mimeMessage.setFrom(author); 45 46 mimeMessage.setFlags(fillFlags(message.getFlagsAsArray()), true); 47 String addr = message.getTo() ; 48 if(addr != null && addr.length() > 0) { 49 mimeMessage.addRecipients(javax.mail.Message.RecipientType.TO, addr); 50 } 51 addr = message.getCC() ; 52 if(addr != null && addr.length() > 0) { 53 mimeMessage.addRecipients(javax.mail.Message.RecipientType.CC, addr); 54 } 55 addr = message.getBCC() ; 56 if(addr != null && addr.length() > 0) { 57 mimeMessage.addRecipients(javax.mail.Message.RecipientType.BCC, addr); 58 } 59 mimeMessage.setSubject(message.getSubject()); 60 mimeMessage.setText(message.getBody()); 61 Transport.send(mimeMessage); 62 } catch (javax.mail.MessagingException ex) { 63 ex.printStackTrace(); 64 Object [] args = {ex.getMessage()} ; 65 throw new ExoMessageException("MessageService.send-message-fail", args) ; 66 } 67 } 68 69 private Flags fillFlags(String [] flagsAsArray) { 70 Flags flags = new Flags(); 71 for (int i = 0; i < flagsAsArray.length; i++) { 72 String flag = flagsAsArray[i]; 73 if(MessageHeader.ANSWERED_FLAG.equals(flag)){ 74 flags.add(Flags.Flag.ANSWERED); 75 } else if(MessageHeader.DELETED_FLAG.equals(flag)){ 76 flags.add(Flags.Flag.DELETED); 77 } else if(MessageHeader.DRAFT_FLAG.equals(flag)){ 78 flags.add(Flags.Flag.DRAFT); 79 } else if(MessageHeader.RECENT_FLAG.equals(flag)){ 80 flags.add(Flags.Flag.RECENT); 81 } else if(MessageHeader.SEEN_FLAG.equals(flag)){ 82 flags.add(Flags.Flag.SEEN); 83 } else if(MessageHeader.USER_FLAG.equals(flag)){ 84 flags.add(Flags.Flag.USER); 85 } else if(MessageHeader.FLAGGED_FLAG.equals(flag)){ 86 flags.add(Flags.Flag.FLAGGED); 87 } else { 88 flags.add(flag); 90 } 91 } 92 return flags; 93 } 94 } 95 | Popular Tags |