📄 emailservice.java
字号:
} } /** * Sends a collection of email messages. This method differs from * {@link #sendMessages(Collection)} in that messages are sent * before this method returns rather than queueing the messages to be sent later. * * @param messages * @throws MessagingException */ public void sendMessagesImmediately(Collection<MimeMessage> messages) throws MessagingException { EmailTask task = new EmailTask(messages); task.sendMessages(); } /** * Returns the SMTP host (e.g. mail.example.com). The default value is "localhost". * * @return the SMTP host. */ public String getHost() { return host; } /** * Sets the SMTP host (e.g. mail.example.com). The default value is "localhost". * * @param host the SMTP host. */ public void setHost(String host) { this.host = host; JiveGlobals.setProperty("mail.smtp.host", host); session = null; } /** * Returns the port number used when connecting to the SMTP server. The default * port is 25. * * @return the SMTP port. */ public int getPort() { return port; } /** * Sets the port number that will be used when connecting to the SMTP * server. The default is 25, the standard SMTP port number. * * @param port the SMTP port number. */ public void setPort(int port) { if (port < 0) { throw new IllegalArgumentException("Invalid port value: " + port); } this.port = port; JiveGlobals.setProperty("mail.smtp.port", Integer.toString(port)); session = null; } /** * Returns the username used to connect to the SMTP server. If the username * is <tt>null</tt>, no username will be used when connecting to the server. * * @return the username used to connect to the SMTP server, or <tt>null</tt> if * there is no username. */ public String getUsername() { return username; } /** * Sets the username that will be used when connecting to the SMTP * server. The default is <tt>null</tt>, or no username. * * @param username the SMTP username. */ public void setUsername(String username) { this.username = username; if (username == null) { JiveGlobals.deleteProperty("mail.smtp.username"); } else { JiveGlobals.setProperty("mail.smtp.username", username); } session = null; } /** * Returns the password used to connect to the SMTP server. If the password * is <tt>null</tt>, no password will be used when connecting to the server. * * @return the password used to connect to the SMTP server, or <tt>null</tt> if * there is no password. */ public String getPassword() { return password; } /** * Sets the password that will be used when connecting to the SMTP * server. The default is <tt>null</tt>, or no password. * * @param password the SMTP password. */ public void setPassword(String password) { this.password = password; if (password == null) { JiveGlobals.deleteProperty("mail.smtp.password"); } else { JiveGlobals.setProperty("mail.smtp.password", password); } session = null; } /** * Returns true if SMTP debugging is enabled. Debug information is * written to <tt>System.out</tt> by the underlying JavaMail provider. * * @return true if SMTP debugging is enabled. */ public boolean isDebugEnabled() { return debugEnabled; } /** * Enables or disables SMTP transport layer debugging. Debug information is * written to <tt>System.out</tt> by the underlying JavaMail provider. * * @param debugEnabled true if SMTP debugging should be enabled. */ public void setDebugEnabled(boolean debugEnabled) { this.debugEnabled = debugEnabled; JiveGlobals.setProperty("mail.debug", Boolean.toString(debugEnabled)); session = null; } /** * Returns true if SSL is enabled for SMTP connections. * * @return true if SSL is enabled. */ public boolean isSSLEnabled() { return sslEnabled; } /** * Sets whether the SMTP connection is configured to use SSL or not. * Typically, the port should be 465 when using SSL with SMTP. * * @param sslEnabled true if ssl should be enabled, false otherwise. */ public void setSSLEnabled(boolean sslEnabled) { this.sslEnabled = sslEnabled; JiveGlobals.setProperty("mail.smtp.ssl", Boolean.toString(sslEnabled)); session = null; } /** * Creates a Javamail session. */ private synchronized void createSession() { if (host == null) { throw new IllegalArgumentException("Host cannot be null."); } Properties mailProps = new Properties(); mailProps.setProperty("mail.smtp.host", host); mailProps.setProperty("mail.smtp.port", String.valueOf(port)); // Allow messages with a mix of valid and invalid recipients to still be sent. mailProps.setProperty("mail.smtp.sendpartial", "true"); mailProps.setProperty("mail.debug", String.valueOf(debugEnabled)); // Methology from an article on www.javaworld.com (Java Tip 115) // We will attempt to failback to an insecure connection // if the secure one cannot be made if (sslEnabled) { // Register with security provider. Security.setProperty("ssl.SocketFactory.provider", SSL_FACTORY); mailProps.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY); mailProps.setProperty("mail.smtp.socketFactory.fallback", "true"); } // If a username is defined, use SMTP authentication. if (username != null) { mailProps.put("mail.smtp.auth", "true"); } session = Session.getInstance(mailProps, null); } /** * Task to send one or more emails via the SMTP server. */ private class EmailTask implements Runnable { private Collection<MimeMessage> messages; public EmailTask(Collection<MimeMessage> messages) { this.messages = messages; } public void run() { try { sendMessages(); } catch (MessagingException me) { Log.error(me); } } public void sendMessages() throws MessagingException { Transport transport = null; try { URLName url = new URLName("smtp", host, port, "", username, password); if (session == null) { createSession(); } transport = new com.sun.mail.smtp.SMTPTransport(session, url); transport.connect(host, port, username, password); for (MimeMessage message : messages) { // Attempt to send message, but catch exceptions caused by invalid // addresses so that other messages can continue to be sent. try { transport.sendMessage(message, message.getRecipients(MimeMessage.RecipientType.TO)); } catch (AddressException ae) { Log.error(ae); } catch (SendFailedException sfe) { Log.error(sfe); } } } finally { if (transport != null) { try { transport.close(); } catch (MessagingException e) { /* ignore */ } } } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -