smtpexporter.java
来自「Jive是基于JSP/JAVA技术构架的一个大型BBS论坛系统,这是Jive论坛」· Java 代码 · 共 765 行 · 第 1/2 页
JAVA
765 行
/** * $RCSfile: SmtpExporter.java,v $ * $Revision: 1.7 $ * $Date: 2002/07/16 15:35:18 $ * * Copyright (C) 1999-2002 CoolServlets, Inc. All Rights Reserved. * * This software is the proprietary information of CoolServlets, Inc. * Use is subject to license terms. * * @author Bruce Ritchie */package com.jivesoftware.forum.gateway;import com.jivesoftware.forum.*;import javax.activation.DataHandler;import javax.mail.*;import javax.mail.internet.*;import java.io.IOException;import java.io.UnsupportedEncodingException;import java.security.Security;import java.util.Iterator;import java.util.Properties;/** * A gateway that performs exports through SMTP.<p> */public class SmtpExporter implements GatewayExporter { private Session session = null; private String host = null; private int port = 25; private String username = null; private String password = null; private String toAddress = null; private String organization = null; private String defaultFromAddress = null; private String replyToAddress = null; private String gatewayMessageId = "Email-Message-ID"; private String dummyParentHeader = JavaMailGateway.DUMMY_PARENT_HEADER; private boolean SSLEnabled = false; private boolean emailPrefEnabled = true; private boolean fromAddressOnly = false; private boolean debugEnabled = false; private boolean updateMessageID = true; private boolean exportIgnoreMessageID = false; private boolean attachmentEnabled = false; private boolean stopFlag = false; private ForumFactory factory = null; private long forumID = -1; /** * Specify the factory that javamail should use for SSL connections * to the server. */ private static final String SSL_FACTORY = "com.jivesoftware.forum.gateway.ssl.DummySSLSocketFactory"; /** * Create a new SmtpExporter instance. */ public SmtpExporter(ForumFactory factory, Forum forum) { this.factory = factory; this.forumID = forum.getID(); } //FROM THE GATEWAYEXPORTER INTERFACE// /** * Export a forum message to a SMTP server.<p> * * NOTE: It is imperative that parent messages get exported prior to * children messages so that the proper message headers can be set. Failure * to do this will result in messages that may not be threaded properly * when viewed using a mailreader. * * @param forumMessage the message to export. * @throws GatewayException if a connection was unable to be established. */ public synchronized void exportData(ForumMessage forumMessage) throws GatewayException { if (host == null || toAddress == null || defaultFromAddress == null) { throw new GatewayException("Required properties are not all set."); } // Refuse to re-export a message which has previously been exported // or imported or which was automatically created for threading // purposes by an import gateway. // // This can be overridden so that messages can be re-exported // however dummy messages will never be exported. if ((forumMessage.getProperty(gatewayMessageId) != null && !exportIgnoreMessageID) || forumMessage.getProperty(dummyParentHeader) != null) { // ignore return; } try { retrieveSession(); MimeMessage message = createMessage(forumMessage); Transport transport = connectToSmtpServer(); transport.sendMessage(message, message.getRecipients(MimeMessage.RecipientType.TO)); if (!updateMessageID && exportIgnoreMessageID) { // don't set only if we have been told not to, one time // gateway export task may choose to override } else { // set the message ID so that we don't inadvertently cause a // loop to happen forumMessage.setProperty(gatewayMessageId, message.getMessageID()); } disconnectFromSmtpServer(transport); session = null; } catch (Exception e) { throw new GatewayException(e); } } /** * Export an array of forum message to a SMTP server.<p> * * NOTE: It is imperative that parent messages get exported prior to * children messages so that the proper message headers can be set. Failure * to do this will result in messages that may not be threaded properly * when viewed using a mailreader. Therefore, parent messages should be * first in the list. * * @param forumMessage array of messages to export, sorted parents first. * @throws GatewayException if a connection was unable to be established. */ public synchronized void exportData(ForumMessage[] forumMessage) throws GatewayException { retrieveSession(); Transport transport = null; try { transport = connectToSmtpServer(); for (int x=0; x < forumMessage.length; x++) { // stop if we've been told to if (stopFlag) { break; } MimeMessage message = createMessage(forumMessage[x]); transport.sendMessage(message, message.getRecipients(MimeMessage.RecipientType.TO)); if (!updateMessageID && exportIgnoreMessageID) { // don't set only if we have been told not to, one time // gateway export task may choose to override } else { // set the message ID so that we don't inadvertently cause a // loop to happen forumMessage[x].setProperty(gatewayMessageId, message.getMessageID()); } } } catch (Exception e) { throw new GatewayException(e); } finally { if (transport != null) { try { disconnectFromSmtpServer(transport); } catch (MessagingException e) { /* ignore */ } } session = null; } } public void stop() throws GatewayException { stopFlag = true; } /** * Returns the SMTP host (eg mail.example.com). The host is null by * default, but must be set before gateway exports can execute. * * @return the SMTP host. */ public String getHost() { return host; } /** * Sets the SMTP host (eg mail.example.com). The host is null by * default, but must be set before gateway exports can execute. * * @param host the SMTP host. */ public void setHost(String host) { this.host = host; // JavaMail Session no longer valid so set to null; it will get // recreated as needed. session = null; } /** * Returns the port number that will be used when connecting to the SMTP * server. The default is 25, the standard SMTP port number. * * @return the SMTP port number. */ 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) { this.port = port; // JavaMail Session no longer valid so set to null; it will get // recreated as needed. session = null; } /** * Returns the username that will be used when connecting to the SMTP * server. The default is null, or no username. * * @return the SMTP username. */ public String getUsername() { return username; } /** * Sets the username that will be used when connecting to the SMTP * server. The default is null, or no username. * * @param username the SMTP username. */ public void setUsername(String username) { this.username = username; // JavaMail Session no longer valid so set to null; it will get // recreated as needed. session = null; } /** * Returns the password that will be used when connecting to the SMTP * server. The default is null, or no password. * * @return the SMTP password. */ public String getPassword() { return password; } /** * Sets the username that will be used when connecting to the SMTP * server. The default is null, or no username. * * @param password the SMTP password. */ public void setPassword(String password) { this.password = password; // JavaMail Session no longer valid so set to null; it will get // recreated as needed. session = null; } /** * Returns true if debugging is turned on for the email transport layers. * Debug information is written to <tt>System.out</tt> by the underlying * JavaMail provider. * * @return true if debugging is turned on. */ public boolean isDebugEnabled() { return debugEnabled; } /** * Toggles SMTP transport layer debugging on or off. 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; // JavaMail SMTP Session no longer valid so set to null; it will get // recreated as needed. session = null; } /** * Returns true if a user's privacy setting on their email address * should be obeyed when doing an export. * * @return true if a user's privacy settings are respected during exports. */ public boolean isEmailPrefEnabled() { return emailPrefEnabled; } /** * Toggles whether a user's privacy setting on their email address * should be obeyed when doing an export. * * @param enabled true if a user's privacy settings are respected during * exports. */ public void setEmailPrefEnabled(boolean enabled) { this.emailPrefEnabled = enabled; } /** * Returns true if the default from address should be used exclusively * when sending email, ignoring the user's preferences. Default is false. * * @return true if the default from address should be used exclusively * when sending email, ignoring the user's preferences. Default is false. */ public boolean isFromAddressOnly() { return fromAddressOnly; } /** * Toggles whether to exclusively use the default from address when * sending email and ignore the users's preferences. Default is false. * * @param enabled true if the default from address should be used exclusively * when sending email, ignoring the user's preferences. False otherwise. */ public void setFromAddressOnly(boolean enabled) { this.fromAddressOnly = enabled; } /** * Returns the email address that messages will be sent to during exports. * If the gateway is for a mailing list, this would be the mailing list * address (e.g., <tt>your-list@example.com</tt>). * * @return the email address that will receive exported messages. */ public String getToAddress() { return toAddress; } /** * Sets the email address that messages will be sent to during exports. * If the gateway is for a mailing list, this would be the mailing list * address (e.g., <tt>your-list@example.com</tt>). * * @param address the email address that will receive exported messages. */ public void setToAddress(String address) { if ("".equals(address)) { address = null; } this.toAddress = address; } /** * From email address to send mail from for export. Used in the case * of anonymous users or users who hide their information. * * @return the current sender. */ public String getDefaultFromAddress() { return defaultFromAddress; }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?