/** * Constructor. * * @param session Session object for this service * @param urlname URLName object to be used for this service */ protected Service(Session session, URLName urlname) { this.session = session; debug = session.getDebug(); url = urlname; /* * Initialize the URLName with default values. * The URLName will be updated when connect is called. */ String protocol = null; String host = null; int port = -1; String user = null; String password = null; String file = null; // get whatever information we can from the URL // XXX - url should always be non-null here, Session // passes it into the constructor if (url != null) { protocol = url.getProtocol(); host = url.getHost(); port = url.getPort(); user = url.getUsername(); password = url.getPassword(); file = url.getFile(); } // try to get protocol-specific default properties if (protocol != null) { if (host == null) host = session.getProperty("mail." + protocol + ".host"); if (user == null) user = session.getProperty("mail." + protocol + ".user"); } // try to get mail-wide default properties if (host == null) host = session.getProperty("mail.host"); if (user == null) user = session.getProperty("mail.user"); // try using the system username if (user == null) { try { user = System.getProperty("user.name"); } catch (SecurityException sex) { // XXX - it's not worth creating a MailLogger just for this // logger.log(Level.CONFIG, "Can't get user.name property", sex); } } url = new URLName(protocol, host, port, file, user, password); // create or choose the appropriate event queue String scope = session.getProperties().getProperty("mail.event.scope", "folder"); Executor executor = (Executor) session.getProperties().get("mail.event.executor"); if (scope.equalsIgnoreCase("application")) q = EventQueue.getApplicationEventQueue(executor); else if (scope.equalsIgnoreCase("session")) q = session.getEventQueue(); else // if (scope.equalsIgnoreCase("store") || // scope.equalsIgnoreCase("folder")) q = new EventQueue(executor); }
public boolean sendEmailSSL(String to, String subject, String body) { System.out.println("sendEmailSSL, to:" + to + " / subject: " + subject + "body: " + body); usernameSSL = sessionSSL.getProperty("mail.smtp.user"); passwordSSL = sessionSSL.getProperty("mail.smtp.password"); try { Properties props = new Properties(); props.put("mail.smtp.host", sessionSSL.getProperty("mail.smtp.host")); props.put( "mail.smtp.socketFactory.port", sessionSSL.getProperty("mail.smtp.socketFactory.port")); props.put( "mail.smtp.socketFactory.class", sessionSSL.getProperty("mail.smtp.socketFactory.class")); props.put("mail.smtp.auth", sessionSSL.getProperty("mail.smtp.auth")); props.put("mail.smtp.port", sessionSSL.getProperty("mail.smtp.port")); props.put("debug", sessionSSL.getProperty("debug")); String fromAddress = sessionSSL.getProperty("mail.smtp.from"); System.out.println("user/password: "******"/" + passwordSSL); sessionSSL = Session.getDefaultInstance( props, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(usernameSSL, passwordSSL); } }); sessionSSL.setDebug(props.getProperty("debug") == "true"); Message message = new MimeMessage(sessionSSL); message.setFrom(new InternetAddress(fromAddress)); message.setRecipient(RecipientType.TO, new InternetAddress(to)); message.setSubject(subject); message.setContent(body, "text/plain"); System.out.println("Send email"); Transport.send(message); System.out.println("email sent"); return true; } catch (MessagingException ex) { ex.printStackTrace(); return false; } }
/** * Convenience method to create a message associated with the session ses. If success is true, * String reasonFailed is ignored. If success is false and reasonFailed is null, it will be set to * "Unknown". * * @param ses * @param time * @param attempt * @param result * @return * @throws MessagingException * @throws MessagingException * @throws MessagingException * @throws AddressException */ private SMTPMessage createMessage( Session ses, Date time, boolean success, String result, String reasonFailed) throws MessagingException { SMTPMessage message = new SMTPMessage(ses); String tag; if (success) { tag = "[Success]"; } else { tag = "[Failed]"; } try { InternetAddress from = new InternetAddress(); from.setAddress(ses.getProperty("mail.smtp.from")); from.setPersonal("Hermes2 Admin"); message.setFrom(from); message.setSubject(tag + "Hermes2 housecleaning"); String theContent = "======== Report for Hermes2 Housecleaning ======== " + "\n" + "\n Hermes2 housecleaning has recently been invoked" + "\n Date: " + time + "\n Result: " + result; if (success) { message.setText(theContent); } else { if (reasonFailed == null) { reasonFailed = "Unknown"; } message.setText( theContent + "\n Reason for failure: " + reasonFailed + "\n\n" + " Please refer to the log files for more details."); } } catch (MessagingException e) { AdminError("Unable to create the message. Messaging Exception thrown."); stackTraceToLog(e); throw e; } catch (UnsupportedEncodingException e) { AdminError("Unsupported encoding in email 'from' name."); } return message; }
/** * @param replyTo email address (if null, then no reply-to will be set) * @param charset Charset, e.g. utf-8 * @param mimeSubType Mime sub-type, e.g. "html" or "plain" * @param fromEmailAddress E-Mail address of sender, e.g. [email protected] * @param fromName Name of sender, e.g. Wyona */ public static void send( String smtpHost, int smtpPort, String fromEmailAddress, String fromName, String replyTo, String to, String subject, String content, String charset, String mimeSubType) throws AddressException, MessagingException { // Create a mail session Session session = null; if (smtpHost != null && smtpPort >= 0) { java.util.Properties props = new java.util.Properties(); props.put("mail.smtp.host", smtpHost); props.put("mail.smtp.port", "" + smtpPort); session = Session.getInstance(props, null); log.warn( "Use specific mail session: " + session.getProperty("mail.smtp.host") + ":" + session.getProperty("mail.smtp.port")); } else { // INFO: Use the global configuration (default instance) of Yanel java.util.Properties props = new java.util.Properties(); props.put("mail.smtp.host", "mail.foo.bar"); // Dummy value props.put("mail.smtp.port", "37"); // Dummy value boolean smtpAuthEnabled = false; try { smtpAuthEnabled = org.wyona.yanel.core.Yanel.getInstance().isSMTPAuthSet(); } catch (Exception e) { log.error(e, e); } if (smtpAuthEnabled) { log.info("SMTP authentication enabled."); session = Session.getDefaultInstance( props, new DummyAuthenticator()); // INFO: The dummy values will be ignored, because Yanel // (org.wyona.yanel.core.Yanel) sets during // initialization the default session! } else { session = Session.getDefaultInstance( props, null); // INFO: The dummy values will be ignored, because Yanel // (org.wyona.yanel.core.Yanel) sets during initialization the default // session! } log.info( "Use default mail session: " + session.getProperty("mail.smtp.host") + ":" + session.getProperty("mail.smtp.port")); } // Construct the message MimeMessage msg = new MimeMessage(session); if (fromName != null && fromName.length() > 0) { try { msg.setFrom(new InternetAddress(fromEmailAddress, fromName)); } catch (java.io.UnsupportedEncodingException e) { log.error(e, e); msg.setFrom(new InternetAddress(fromEmailAddress)); } } else { msg.setFrom(new InternetAddress(fromEmailAddress)); } if (replyTo != null) { InternetAddress[] replyToAddresses = new InternetAddress[1]; replyToAddresses[0] = new InternetAddress(replyTo); msg.setReplyTo(replyToAddresses); } msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to)); msg.setSubject(subject); msg.setText(content, charset, mimeSubType); // Send the message Transport.send(msg); }
/** Description of the Method */ public boolean sendMessage() { MimeMultipart mp = new MimeMultipart(); try { // if there is a text and an html section if (sendingText.getCount() > 1) { sendingText.setSubType("alternative"); } // if we are sending attachments if (sendingAttachments.getCount() > 0) { MimeBodyPart bp = new MimeBodyPart(); bp.setContent(sendingAttachments); mp.addBodyPart(bp, 0); bp = new MimeBodyPart(); bp.setContent(sendingText); mp.addBodyPart(bp, 0); } else { mp = sendingText; } Logger.debug(this, "Getting the MailContext."); /* * * Get the mail session from * * the container Context */ Session session = null; Context ctx = null; try { ctx = (Context) new InitialContext().lookup("java:comp/env"); session = (javax.mail.Session) ctx.lookup("mail/MailSession"); } catch (Exception e1) { try { Logger.debug(this, "Using the jndi intitialContext()."); ctx = new InitialContext(); session = (javax.mail.Session) ctx.lookup("mail/MailSession"); } catch (Exception e) { Logger.error(this, "Exception occured finding a mailSession in JNDI context."); Logger.error(this, e1.getMessage(), e1); return false; } } if (session == null) { Logger.debug(this, "No Mail Session Available."); return false; } Logger.debug( this, "Delivering mail using: " + session.getProperty("mail.smtp.host") + " as server."); MimeMessage message = new MimeMessage(session); message.addHeader("X-RecipientId", String.valueOf(getRecipientId())); if ((fromEmail != null) && (fromName != null) && (0 < fromEmail.trim().length())) { message.setFrom(new InternetAddress(fromEmail, fromName)); } else if ((fromEmail != null) && (0 < fromEmail.trim().length())) { message.setFrom(new InternetAddress(fromEmail)); } if (toName != null) { String[] recipients = toEmail.split("[;,]"); for (String recipient : recipients) { message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient, toName)); } } else { String[] recipients = toEmail.split("[;,]"); for (String recipient : recipients) { message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient)); } } if (UtilMethods.isSet(cc)) { String[] recipients = cc.split("[;,]"); for (String recipient : recipients) { message.addRecipient(Message.RecipientType.CC, new InternetAddress(recipient)); } } if (UtilMethods.isSet(bcc)) { String[] recipients = bcc.split("[;,]"); for (String recipient : recipients) { message.addRecipient(Message.RecipientType.BCC, new InternetAddress(recipient)); } } message.setSubject(subject, encoding); message.setContent(mp); Transport.send(message); result = "Send Ok"; return true; } catch (javax.mail.SendFailedException f) { String error = String.valueOf(f); errorMessage = error.substring( error.lastIndexOf("javax.mail.SendFailedException:") + "javax.mail.SendFailedException:".length(), (error.length() - 1)); result = "Failed:" + error; Logger.error(Mailer.class, f.toString(), f); return false; } catch (MessagingException f) { String error = String.valueOf(f); errorMessage = error.substring( error.lastIndexOf("javax.mail.MessagingException:") + "javax.mail.MessagingException:".length(), (error.length() - 1)); result = "Failed:" + error; Logger.error(Mailer.class, f.toString(), f); return false; } catch (UnsupportedEncodingException f) { String error = String.valueOf(f); errorMessage = error.substring( error.lastIndexOf("java.io.UnsupportedEncodingException:") + "java.io.UnsupportedEncodingException:".length(), (error.length() - 1)); result = "Failed:" + error; Logger.error(Mailer.class, f.toString(), f); return false; } }
/** * Similar to connect(host, user, password) except a specific port can be specified. * * @param host the host to connect to * @param port the port to connect to (-1 means the default port) * @param user the user name * @param password this user's password * @exception AuthenticationFailedException for authentication failures * @exception MessagingException for other failures * @exception IllegalStateException if the service is already connected * @see #connect(java.lang.String, java.lang.String, java.lang.String) * @see javax.mail.event.ConnectionEvent */ public synchronized void connect(String host, int port, String user, String password) throws MessagingException { // see if the service is already connected if (isConnected()) throw new IllegalStateException("already connected"); PasswordAuthentication pw; boolean connected = false; boolean save = false; String protocol = null; String file = null; // get whatever information we can from the URL // XXX - url should always be non-null here, Session // passes it into the constructor if (url != null) { protocol = url.getProtocol(); if (host == null) host = url.getHost(); if (port == -1) port = url.getPort(); if (user == null) { user = url.getUsername(); if (password == null) // get password too if we need it password = url.getPassword(); } else { if (password == null && user.equals(url.getUsername())) // only get the password if it matches the username password = url.getPassword(); } file = url.getFile(); } // try to get protocol-specific default properties if (protocol != null) { if (host == null) host = session.getProperty("mail." + protocol + ".host"); if (user == null) user = session.getProperty("mail." + protocol + ".user"); } // try to get mail-wide default properties if (host == null) host = session.getProperty("mail.host"); if (user == null) user = session.getProperty("mail.user"); // try using the system username if (user == null) { try { user = System.getProperty("user.name"); } catch (SecurityException sex) { // XXX - it's not worth creating a MailLogger just for this // logger.log(Level.CONFIG, "Can't get user.name property", sex); } } // if we don't have a password, look for saved authentication info if (password == null && url != null) { // canonicalize the URLName setURLName(new URLName(protocol, host, port, file, user, null)); pw = session.getPasswordAuthentication(getURLName()); if (pw != null) { if (user == null) { user = pw.getUserName(); password = pw.getPassword(); } else if (user.equals(pw.getUserName())) { password = pw.getPassword(); } } else save = true; } // try connecting, if the protocol needs some missing // information (user, password) it will not connect. // if it tries to connect and fails, remember why for later. AuthenticationFailedException authEx = null; try { connected = protocolConnect(host, port, user, password); } catch (AuthenticationFailedException ex) { authEx = ex; } // if not connected, ask the user and try again if (!connected) { InetAddress addr; try { addr = InetAddress.getByName(host); } catch (UnknownHostException e) { addr = null; } pw = session.requestPasswordAuthentication(addr, port, protocol, null, user); if (pw != null) { user = pw.getUserName(); password = pw.getPassword(); // have the service connect again connected = protocolConnect(host, port, user, password); } } // if we're not connected by now, we give up if (!connected) { if (authEx != null) throw authEx; else if (user == null) throw new AuthenticationFailedException("failed to connect, no user name specified?"); else if (password == null) throw new AuthenticationFailedException("failed to connect, no password specified?"); else throw new AuthenticationFailedException("failed to connect"); } setURLName(new URLName(protocol, host, port, file, user, password)); if (save) session.setPasswordAuthentication(getURLName(), new PasswordAuthentication(user, password)); // set our connected state setConnected(true); // finally, deliver the connection event notifyConnectionListeners(ConnectionEvent.OPENED); }
public boolean sendEmailSSL_HTML(String to, String subject, String body) { System.out.println("sendEmailSSL_HTML, to:" + to + " / subject: " + subject + "body: " + body); usernameSSL = sessionSSL.getProperty("mail.smtp.user"); passwordSSL = sessionSSL.getProperty("mail.smtp.password"); try { Properties props = new Properties(); props.put("mail.smtp.host", sessionSSL.getProperty("mail.smtp.host")); props.put( "mail.smtp.socketFactory.port", sessionSSL.getProperty("mail.smtp.socketFactory.port")); props.put( "mail.smtp.socketFactory.class", sessionSSL.getProperty("mail.smtp.socketFactory.class")); props.put("mail.smtp.auth", sessionSSL.getProperty("mail.smtp.auth")); props.put("mail.smtp.port", sessionSSL.getProperty("mail.smtp.port")); props.put("debug", sessionSSL.getProperty("debug")); String fromAddress = sessionSSL.getProperty("mail.smtp.from"); System.out.println("user/password: "******"/" + passwordSSL); sessionSSL = Session.getDefaultInstance( props, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(usernameSSL, passwordSSL); } }); sessionSSL.setDebug(props.getProperty("debug") == "true"); Message message = new MimeMessage(sessionSSL); message.setFrom(new InternetAddress(fromAddress)); message.setRecipient(RecipientType.TO, new InternetAddress(to)); message.setSubject(subject); // Create a MimeMultipart instance with an alternative sub-type. // A multi-part message consists of multiple parts, in this case an HTML and a text message // (other possible parts are file attachments). // The alternative sub-type indicates that the multiple message parts are alternative versions // of the same content. Multipart multipart = new MimeMultipart("alternative"); // Create a MimeBodyPart instance to contain the text body part MimeBodyPart textPart = new MimeBodyPart(); textPart.setText(body + "(textcontent)"); // Create a MimeBodyPart instance to contain the HTML body part. // Order is important, the preferred format of an alternative multi-part message should be // added last. MimeBodyPart htmlPart = new MimeBodyPart(); String htmlContent = "<html><h1>Hi</h1><p>" + body + "(htmlcontent)" + "</p></html>"; htmlPart.setContent(htmlContent, "text/html"); // Add both MimeBodyPart instances to the MimeMultipart instance and set the MimeMultipart // instance as the MimeMessage. multipart.addBodyPart(textPart); multipart.addBodyPart(htmlPart); message.setContent(multipart); System.out.println("Send email"); Transport.send(message); System.out.println("email sent"); return true; } catch (MessagingException ex) { ex.printStackTrace(); return false; } }