1 17 18 package org.efs.openreports.providers; 19 20 import java.util.Properties ; 21 import java.util.Vector ; 22 23 import javax.activation.DataHandler ; 24 import javax.activation.DataSource ; 25 import javax.activation.FileDataSource ; 26 import javax.mail.*; 27 import javax.mail.internet.*; 28 29 import com.opensymphony.xwork.ActionContext; 30 import com.opensymphony.xwork.interceptor.component.ComponentManager; 31 32 import org.apache.log4j.Logger; 33 34 import org.efs.openreports.objects.MailMessage; 35 import org.efs.openreports.objects.ORProperty; 36 import org.efs.openreports.util.SMTPAuthenticator; 37 38 public class MailProvider implements PropertiesProviderAware 39 { 40 protected static Logger log = Logger.getLogger(MailProvider.class.getName()); 41 42 private PropertiesProvider propertiesProvider; 43 44 private String mailHost; 45 private boolean useMailAuthenticator; 46 private String userName; 47 private String password; 48 49 public MailProvider(PropertiesProvider propertiesProvider) throws ProviderException 51 { 52 this.propertiesProvider = propertiesProvider; 53 init(); 54 } 55 56 public MailProvider() throws ProviderException 58 { 59 ComponentManager container = (ComponentManager) ActionContext.getContext().get( 60 "com.opensymphony.xwork.interceptor.component.ComponentManager"); 61 62 container.initializeObject(this); 63 64 init(); 65 } 66 67 protected void init() throws ProviderException 68 { 69 mailHost = null; 70 useMailAuthenticator = false; 71 userName = null; 72 password = null; 73 74 ORProperty property = propertiesProvider.getProperty(ORProperty.MAIL_SMTP_HOST); 75 if (property != null) mailHost = property.getValue(); 76 77 property = propertiesProvider.getProperty(ORProperty.MAIL_AUTH_USER); 78 if (property != null) useMailAuthenticator = Boolean.getBoolean(property.getValue()); 79 80 property = propertiesProvider.getProperty(ORProperty.MAIL_AUTH_USER); 81 if (property != null) userName = property.getValue(); 82 83 property = propertiesProvider.getProperty(ORProperty.MAIL_AUTH_PASSWORD); 84 if (property != null) password = property.getValue(); 85 86 log.info("Created"); 87 } 88 89 public void sendMail(MailMessage mail) throws ProviderException 90 { 91 try 92 { 93 Properties mailProps = new Properties (); 95 mailProps.put("mail.smtp.host", mailHost); 96 97 Session mailSession = null; 98 if (useMailAuthenticator) 99 { 100 mailSession = Session.getInstance(mailProps, new SMTPAuthenticator(userName, password)); 101 } 102 else 103 { 104 mailSession = Session.getInstance(mailProps, null); 105 } 106 107 InternetAddress from = new InternetAddress(mail.getSender()); 109 110 Vector recipients = mail.getRecipients(); 111 InternetAddress[] to = new InternetAddress[recipients.size()]; 112 for (int i = 0; i < recipients.size(); i++) 113 { 114 to[i] = new InternetAddress((String ) recipients.get(i)); 115 } 116 117 Multipart multipart = new MimeMultipart(); 119 120 if (mail.getText() != null) 122 { 123 MimeBodyPart mbpText = new MimeBodyPart(); 124 mbpText.setText(mail.getText()); 125 multipart.addBodyPart(mbpText); 126 } 127 128 Vector attachments = mail.getAttachments(); 130 for (int i = 0; i < attachments.size(); i++) 131 { 132 String fileAttachment = (String ) attachments.get(i); 133 FileDataSource source = new FileDataSource (fileAttachment); 134 135 MimeBodyPart mbpAttachment = new MimeBodyPart(); 136 mbpAttachment.setDataHandler(new DataHandler (source)); 137 mbpAttachment.setFileName(fileAttachment); 138 139 multipart.addBodyPart(mbpAttachment); 140 } 141 142 if (mail.getByteArrayDataSource().getContentType().equals("text/html")) 144 { 145 Multipart htmlMP = new MimeMultipart("related"); 146 MimeBodyPart htmlBP = new MimeBodyPart(); 147 htmlBP.setDataHandler(new DataHandler (mail.getByteArrayDataSource())); 148 htmlMP.addBodyPart(htmlBP); 149 150 Vector images = mail.getHtmlImageDataSources(); 152 for (int i = 0; i < images.size(); i++) 153 { 154 DataSource imageDS = (DataSource ) images.get(i); 155 156 MimeBodyPart imageBodyPart = new MimeBodyPart(); 157 imageBodyPart.setFileName(imageDS.getName()); 158 imageBodyPart.setText(imageDS.getName()); 159 imageBodyPart.setDataHandler(new DataHandler (imageDS)); 160 imageBodyPart.setHeader("Content-ID", "<" + imageDS.getName() + ">"); 161 imageBodyPart.setDisposition(javax.mail.Part.INLINE); 162 163 htmlMP.addBodyPart(imageBodyPart); 164 } 165 166 BodyPart completeHtmlBP = new MimeBodyPart(); 167 completeHtmlBP.setContent(htmlMP); 168 multipart.addBodyPart(completeHtmlBP); 169 } 170 else 171 { 172 MimeBodyPart mbpAttachment = new MimeBodyPart(); 173 mbpAttachment.setDataHandler(new DataHandler (mail.getByteArrayDataSource())); 174 mbpAttachment.setFileName(mail.getByteArrayDataSource().getName()); 175 176 multipart.addBodyPart(mbpAttachment); 177 } 178 179 Message msg = new MimeMessage(mailSession); 181 msg.setFrom(from); 182 msg.setRecipients(Message.RecipientType.TO, to); 183 msg.setSubject(mail.getSubject()); 184 msg.setContent(multipart); 185 186 Transport.send(msg); 187 } 188 catch (Exception e) 189 { 190 log.error(e.toString()); 191 throw new ProviderException(e.getMessage()); 192 } 193 } 194 195 public void setMailHost(String mailHost) 196 { 197 this.mailHost = mailHost; 198 } 199 200 public void setPassword(String password) 201 { 202 this.password = password; 203 } 204 205 public void setUseMailAuthenticator(boolean useMailAuthenticator) 206 { 207 this.useMailAuthenticator = useMailAuthenticator; 208 } 209 210 public void setUserName(String userName) 211 { 212 this.userName = userName; 213 } 214 215 public void setPropertiesProvider(PropertiesProvider propertiesProvider) 216 { 217 this.propertiesProvider = propertiesProvider; 218 } 219 220 }
| Popular Tags
|