/** * Send mail using JavaMail. */ public static bool mail(Env env, string to, string subject, StringValue message, @Optional string additionalHeaders, @Optional string additionalParameters) { Transport smtp = null; try { HashMap <String, String> headers = splitHeaders(additionalHeaders); if (to == null || to.equals("")) { to = headers.get("to"); } Properties props = new Properties(); StringValue host = env.getIni("SMTP"); if (host != null && !host.ToString().equals("")) { props.put("mail.smtp.host", host.ToString()); } else if (System.getProperty("mail.smtp.host") != null) { props.put("mail.smtp.host", System.getProperty("mail.smtp.host")); } StringValue port = env.getIni("smtp_port"); if (port != null && !port.ToString().equals("")) { props.put("mail.smtp.port", port.ToString()); } else if (System.getProperty("mail.smtp.port") != null) { props.put("mail.smtp.port", System.getProperty("mail.smtp.port")); } if (System.getProperty("mail.smtp.class") != null) { props.put("mail.smtp.class", System.getProperty("mail.smtp.class")); } StringValue user = null; if (headers.get("from") != null) { user = env.createString(headers.get("from")); } if (user == null) { user = env.getIni("sendmail_from"); } if (user != null && !user.ToString().equals("")) { string userString = user.ToString(); props.put("mail.from", userString); } else if (System.getProperty("mail.from") != null) { props.put("mail.from", System.getProperty("mail.from")); } else { try { InetAddress addr = InetAddress.getLocalHost(); string email = (System.getProperty("user.name") + "@" + addr.getHostName()); int index = email.indexOf('@'); // for certain windows smtp servers if (email.indexOf('.', index) < 0) { email += ".com"; } props.put("mail.from", email); } catch (Exception e) { log.log(Level.FINER, e.ToString(), e); } } string username = env.getIniString("smtp_username"); string password = env.getIniString("smtp_password"); if (password != null && !"".equals(password)) { props.put("mail.smtp.auth", "true"); } Session mailSession = Session.getInstance(props, null); smtp = mailSession.getTransport("smtp"); QuercusMimeMessage msg = new QuercusMimeMessage(mailSession); if (subject == null) { subject = ""; } msg.setSubject(subject); msg.setContent(message.ToString(), "text/plain"); ArrayList <Address> addrList = new ArrayList <Address>(); if (to != null && to.length() > 0) { addRecipients(msg, Message.RecipientType.TO, to, addrList); } if (headers != null) { addHeaders(msg, headers, addrList); } Address [] from = msg.getFrom(); if (from == null || from.length == 0) { log.fine(L.l( "mail 'From' not set, setting to Java System property 'user.name'")); msg.setFrom(); } msg.saveChanges(); from = msg.getFrom(); log.fine(L.l("sending mail, From: {0}, To: {1}", from[0], addrList)); if (password != null && !"".equals(password)) { smtp.connect(username, password); } else { smtp.connect(); } Address[] addr; addr = new Address[addrList.size()]; addrList.toArray(addr); smtp.sendMessage(msg, addr); log.fine("quercus-mail: sent mail to " + to); return(true); } catch (AuthenticationFailedException e) { log.warning(L.l("Quercus[] mail could not send mail to '{0}' because authentication failed\n{1}", to, e.getMessage())); log.log(Level.FINE, e.ToString(), e); env.warning(e.ToString()); return(false); } catch (MessagingException e) { Throwable cause = e; log.warning(L.l("Quercus[] mail could not send mail to '{0}'\n{1}", to, cause.getMessage())); log.log(Level.FINE, cause.ToString(), cause); env.warning(cause.getMessage()); return(false); } catch (Exception e) { Throwable cause = e; log.warning(L.l("Quercus[] mail could not send mail to '{0}'\n{1}", to, e)); log.log(Level.FINE, cause.ToString(), cause); env.warning(cause.ToString()); return(false); } finally { try { if (smtp != null) { smtp.close(); } } catch (Exception e) { log.log(Level.FINER, e.ToString(), e); } } }