package org.crosswire.utils; import java.io.File; import java.io.PrintWriter; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.Properties; import java.util.Vector; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Part; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.AddressException; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; import javax.mail.PasswordAuthentication; import org.apache.log4j.Logger; public class SMTPMail { protected static Message prepareHeader(String smtp_host, String from, String to, String subject) throws IOException, AddressException, MessagingException { Properties props = new Properties(); props.put("mail.smtp.host", smtp_host); Session session = Session.getDefaultInstance(props, null); Message msg = new MimeMessage(session); InternetAddress addr = new InternetAddress(to); msg.addRecipients(Message.RecipientType.TO, new InternetAddress[] { addr }); InternetAddress from_addr = new InternetAddress(from); msg.setFrom(from_addr); msg.setSubject(subject); return msg; } public static void sendMail(String smtp_host, String from, String to, String subject, String message) throws IOException, AddressException, MessagingException { Message msg = prepareHeader(smtp_host, from, to, subject); msg.setContent(message, "text/plain"); Transport.send(msg); } public static void sendWithAttachments(String smtp_host, String from, String to, String subject, String message, Vector attach) throws IOException, AddressException, MessagingException { Message msg = prepareHeader(smtp_host, from, to, subject); MimeMultipart mp = new MimeMultipart(); MimeBodyPart text = new MimeBodyPart(); text.setDisposition(Part.INLINE); text.setContent(message, "text/plain"); mp.addBodyPart(text); for (File file : attach) { MimeBodyPart file_part = new MimeBodyPart(); FileDataSource fds = new FileDataSource(file); DataHandler dh = new DataHandler(fds); file_part.setFileName(file.getName()); file_part.setDisposition(Part.ATTACHMENT); file_part.setDescription("Attached file: " + file.getName()); file_part.setDataHandler(dh); mp.addBodyPart(file_part); } msg.setContent(mp); Transport.send(msg); } // Main method public static void main(String[] args) { try { SMTPMail.sendMail("localhost", "webmaster@crosswire.org", "scribe777@gmail.com", "SMTPMailer test", "Some, hopefully you, requested to create an account XXXX at community.crosswire.org using this email. To activate this account click the link below:\n\nhttp://community.crosswire.org?activate.jsp?UUID=bla\n\nIf you did not request this, do nothing and this account will not be created."); } catch (Exception e) { e.printStackTrace(); } } static Logger logger = Logger.getLogger(SMTPMail.class); static Transport transport = null; static public void closeMailConnection() { try { if (transport != null) transport.close(); } catch (Exception e) { logger.error("Problem closing outgoing mail connection", e); } transport = null; } static public void sendEmail(java.util.Properties sysConfig, String from, String to, String subject, String body) { logger.error("Sending email: [" + to + "] ["+from+"] ["+subject+"]"); sendEmail(sysConfig, from, to, subject, body, false); } static public void sendEmail(javax.servlet.http.HttpSession httpSession, String from, String to, String subject, String body) { sendEmail(httpSession, from, to, subject, body, false); } static public void sendEmail(javax.servlet.http.HttpSession httpSession, String from, String to, String subject, String body, boolean leaveConnectionOpen) { sendEmail(httpSession, from, to, subject, body, null, leaveConnectionOpen); } static public void sendEmail(javax.servlet.http.HttpSession httpSession, String from, String to, String subject, String body, String bodyMimeType, boolean leaveConnectionOpen) { Properties sysConfig = Utils.getSysConfig(httpSession); sendEmail(sysConfig, from, to, subject, body, bodyMimeType, false); } static public void sendEmail(java.util.Properties sysConfig, String from, String to, String subject, String body, boolean leaveConnectionOpen) { sendEmail(sysConfig, from, to, subject, body, null, false); } static public void sendEmail(java.util.Properties sysConfig, String from, String to, String subject, String body, String bodyMimeType, boolean leaveConnectionOpen) { sendEmail(sysConfig, from, to, subject, body, bodyMimeType, null, null, null, leaveConnectionOpen); } static public void sendEmail(java.util.Properties sysConfig, String from, String to, String subject, String body, String attachmentName, String attachmentBody, String attachmentMimeType, boolean leaveConnectionOpen) { sendEmail(sysConfig, from, to, subject, body, null, attachmentName, attachmentBody, attachmentMimeType, leaveConnectionOpen); } static synchronized public void sendEmail(java.util.Properties sysConfig, String from, String to, String subject, String body, String bodyMimeType, String attachmentName, String attachmentBody, String attachmentMimeType, boolean leaveConnectionOpen) { if (attachmentName == null) attachmentName = "attachment.txt"; if (attachmentMimeType == null) attachmentMimeType = "text/plain"; if (bodyMimeType == null) bodyMimeType = "text/plain"; final String server = sysConfig.getProperty("SendMailServer", "localhost"); final String userID = sysConfig.getProperty("SendMailUser", ""); final String passwd = sysConfig.getProperty("SendMailPasswd", ""); String port = sysConfig.getProperty("SendMailPort", ""); String auth = sysConfig.getProperty("SendMailAuth", ""); final String startTLS = sysConfig.getProperty("SendMailStartTLS", "false"); final String ssl = sysConfig.getProperty("SendMailSSL", "false"); Properties props = new Properties(); try { if ("true".equals(startTLS)) { if (port.isEmpty()) port = "587"; if (auth.isEmpty()) auth = "true"; props.put("mail.smtp.starttls.enable", "true"); // added this line } else if ("true".equals(ssl)) { if (port.isEmpty()) port = "465"; if (auth.isEmpty()) auth = "true"; props.put("mail.smtp.socketFactory.port", port); props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); props.put("mail.smtp.socketFactory.fallback", "false"); } props.put("mail.smtp.host", server); if (auth != null && auth.length() > 0) props.put("mail.smtp.auth", auth); if (port != null && port.length() > 0) props.put("mail.smtp.port", port); javax.mail.Authenticator authenticator = "true".equals(auth) ? new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(userID, passwd); } } : null; Session session = Session.getInstance(props, authenticator); if (transport == null) { if (userID != null && passwd != null && userID.length() > 0 && passwd.length() > 0) { String protocol = "true".equals(startTLS) || !"true".equals(ssl) ? "smtp" : "smtps"; logger.debug("Email protocol: " + protocol); transport = session.getTransport(protocol); transport.connect(server, userID, passwd); } } try { Message msg = new MimeMessage(session); InternetAddress addr = new InternetAddress(to); msg.addRecipients(Message.RecipientType.TO, new InternetAddress[] { addr }); InternetAddress from_addr = new InternetAddress(from); msg.setFrom(from_addr); msg.setSubject(subject); MimeMultipart mp = new MimeMultipart(); MimeBodyPart text = new MimeBodyPart(); text.setDisposition(Part.INLINE); text.setContent(body, bodyMimeType); mp.addBodyPart(text); if (attachmentBody != null) { MimeBodyPart file_part = new MimeBodyPart(); // FileDataSource fds = new FileDataSource(file); // DataHandler dh = new DataHandler(fds); file_part.setFileName(attachmentName); file_part.setDisposition(Part.ATTACHMENT); file_part.setDescription("Attached file: " + attachmentName); file_part.setDataHandler(new DataHandler(attachmentBody, attachmentMimeType)); mp.addBodyPart(file_part); } msg.setContent(mp); if (transport != null) transport.sendMessage(msg, msg.getAllRecipients()); else Transport.send(msg); } catch (Exception e) { ByteArrayOutputStream propsList = new ByteArrayOutputStream(); PrintWriter writer = new PrintWriter(propsList); props.list(writer); writer.flush(); logger.error("Problem sending email [" + subject + "] to: " + to + "; props.list: " + propsList.toString(), e); } } catch (Exception e) { ByteArrayOutputStream propsList = new ByteArrayOutputStream(); PrintWriter writer = new PrintWriter(propsList); props.list(writer); writer.flush(); logger.error("Problem sending email [" + subject + "] to: " + to + "; props.list: " + propsList.toString(), e); } finally { if (!leaveConnectionOpen) { closeMailConnection(); } } } }