⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 emailgateway.java

📁 jive 2.1.1 的源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/**
 * $RCSfile: EmailGateway.java,v $
 * $Revision: 1.19 $
 * $Date: 2001/09/09 15:42:40 $
 *
 * Copyright (C) 1999-2001 CoolServlets Inc. All rights reserved.
 * ===================================================================
 * The Jive Software License (based on Apache Software License, Version 1.1)
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution,
 *    if any, must include the following acknowledgment:
 *       "This product includes software developed by
 *        Jive Software (http://www.jivesoftware.com)."
 *    Alternately, this acknowledgment may appear in the software itself,
 *    if and wherever such third-party acknowledgments normally appear.
 *
 * 4. The names "Jive" and "CoolServlets" must not be used to
 *    endorse or promote products derived from this software without
 *    prior written permission. For written permission, please
 *    contact webmaster@coolservlets.com.
 *
 * 5. Products derived from this software may not be called "Jive",
 *    nor may "Jive" appear in their name, without prior written
 *    permission of CoolServlets.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL COOLSERVLETS INC OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
 * This software consists of voluntary contributions made by many
 * individuals on behalf of Jive Software. For more information
 * on Jive Software please visit http://www.jivesoftware.com.
 */

package com.jivesoftware.forum.gateway;

import java.io.*;
import java.util.*;
import java.text.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import com.jivesoftware.forum.*;
import com.jivesoftware.util.StringUtils;

/**
 * A gateway to synchronize a forum with the contents of an email account or
 * a mailing list. For example, say you want to sychronize with the mailing
 * list <tt>mail-list@example.com</tt>. To do so, you would set the "to address"
 * to be <tt>mail-list@example.com</tt>. For importing, you'd need to create and
 * use a POP3 account that was subscribed to the list, for example
 * <tt>mail-list-robot@example.com</tt>.<p>
 *
 * The following properties must be set with valid values before importing and
 * exporting can work: <ul>
 *      <li> <tt>POP3Host</tt>
 *      <li> <tt>POP3Username</tt>
 *      <li> <tt>POP3Password</tt>
 *      <li> <tt>SMTPHost</tt>
 *      <li> <tt>defaultFromAddress</tt>
 *      <li> <tt>toAddress</tt> </ul>
 *
 * All other properties start with reasonable default values, but can be
 * changed as necessary.
 *
 * @author Bruce Ritchie
 */
public class EmailGateway implements Gateway {

    private POP3Gateway gateway;
    private Session SMTPSession = null;

    private String SMTPHost = null;
    private int SMTPPort = 25;
    private String SMTPUsername = null;
    private String SMTPPassword = null;

    private String toAddress = null;
    private String defaultFromAddress = null;

    private boolean emailPrefEnabled = true;
    private boolean debugEnabled = false;

    /**
     * Used to flag messages in the forum with a message id
     * Specific to this gateway
     */
    public static final String GATEWAY_MESSAGE_ID = "Email-Message-ID";

    /**
     * Used to flag messages in the forum with a parent id
     * Specific to this gateway
     */
    public static final String GATEWAY_PARENT_ID = "Email-Parent-ID";

    /**
     * Create a new EmailGateway instance.
     */
    public EmailGateway(ForumFactory factory, Forum forum) {
        // Configure POP Gateway
        gateway = new POP3Gateway(factory, forum);
    }

    //FROM THE GATEWAY INTERFACE//

    public synchronized void importData(Date afterDate) throws GatewayException
    {
        gateway.importData(afterDate);
    }

    public synchronized void exportData(ForumMessage forumMessage)
            throws GatewayException
    {
        if (SMTPHost == 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.
        if (forumMessage.getProperty(GATEWAY_MESSAGE_ID) != null ||
                forumMessage.getProperty(gateway.DUMMY_PARENT_HEADER) != null) {
            return;
        }

        try {
            // Create the mail session if necessary.
            if (SMTPSession == null) {
                Properties mailProps = new Properties();
                mailProps.setProperty("mail.smtp.host", SMTPHost);
                mailProps.setProperty("mail.smtp.port", String.valueOf(SMTPPort));
                mailProps.setProperty("mail.debug", String.valueOf(debugEnabled));
                SMTPSession = Session.getInstance(mailProps, null);
            }

            // Next, determine the "from" address, which will normally be
            // the message author's name and email address. However, we have to
            // use a default address in some circumstances when that info isn't
            // available.
            InternetAddress fromAddress = null;
            if (!forumMessage.isAnonymous()) {
                User user = forumMessage.getUser();
                String name, email = null;
                // Get the name, or use their username if the name is hidden.
                if (user.isNameVisible()) {
                    name = user.getName();
                }
                else {
                    name = user.getUsername();
                }
                // Get their email address, or use the dummy address if their
                // email is hidden and the admin hasn't overriden that.
                if (!user.isEmailVisible() && emailPrefEnabled) {
                    email = defaultFromAddress;
                }
                else {
                    email = user.getEmail();
                }
                fromAddress = new InternetAddress(email, name);
            }
            // Message was posted anonymously. However, a name and email might
            // be defined through extended properties.
            else if (forumMessage.getProperty("name") != null &&
                     forumMessage.getProperty("email") != null)
            {
                String name  = forumMessage.getProperty("name");
                String email = forumMessage.getProperty("email");
                fromAddress = new InternetAddress(email, name);
            }
            // Getting "from" address failed. Use the default address.
            else {
                fromAddress = new InternetAddress(defaultFromAddress);
            }

            // Create a new JavaMail message and set basic values on it.
            MimeMessage message = new MimeMessage(SMTPSession);
            message.setRecipient(Message.RecipientType.TO,
                    new InternetAddress(toAddress));
            message.setFrom(fromAddress);
            // The body is the message + the footer.
            StringBuffer body = new StringBuffer(forumMessage.getUnfilteredBody());
            Forum forum = gateway.factory.getForum(gateway.forumID);
            GatewayManager manager = forum.getGatewayManager();
            body.append(manager.getTranslatedFooter(forumMessage));
            message.setContent(body.toString(),"text/plain");

            message.setSubject(forumMessage.getSubject());

            // Get a parent message ID if one exists so we can set
            // the references header properly for threading to work.
            try {
                ForumThread thread = forumMessage.getForumThread();
                TreeWalker tree = thread.treeWalker();
                ForumMessage parent = tree.getParent(forumMessage);
                String parentId = parent.getProperty(GATEWAY_MESSAGE_ID);

                if (parentId != null && !parentId.equals("")) {
                    message.setHeader("In-Reply-To", parentId);
                }
            }
            catch (ForumMessageNotFoundException fmnfe) { /* ignore */ }

            // Send the message
            URLName url =
                new URLName("smtp", SMTPHost, SMTPPort, "", SMTPUsername, SMTPPassword);
            Transport trans =
                (Transport) new com.sun.mail.smtp.SMTPTransport(SMTPSession, url);
            trans.connect(SMTPHost, SMTPPort, SMTPUsername, SMTPPassword);
            trans.sendMessage(message, message.getRecipients(MimeMessage.RecipientType.TO));

            // Set the message ID so that we don't inadvertently cause a mail
            // loop to happen.
            forumMessage.setProperty(GATEWAY_MESSAGE_ID, message.getMessageID());
        }
        catch (Exception e) {
            throw new GatewayException(e);
        }
    }

    //GATEWAY PROPERTIES//

    /**
     * Returns the POP3 host (e.g. mail.example.com). The host is null by
     * default, but must be set before gateway imports can execute.
     *
     * @return the POP3 host.
     */
    public String getPOP3Host() {
        return gateway.getHost();
    }

    /**
     * Sets the POP3 host (e.g. mail.example.com). The host is null by
     * default, but must be set before gateway imports can execute.
     *
     * @param host the POP3 host.
     */
    public void setPOP3Host(String host) {
        gateway.setHost(host);
    }

    /**
     * Returns the port number that will be used when connecting to the POP3
     * server. The default is 110, the standard POP3 port number.
     *
     * @return the POP3 port number.
     */
    public int getPOP3Port() {
        return gateway.getPort();
    }

    /**
     * Sets the port number that will be used when connecting to the POP3
     * server. The default is 110, the standard POP3 port number.
     *
     * @param port the POP3 port number.
     */
    public synchronized void setPOP3Port(int port) {
        gateway.setPort(port);
    }

    /**
     * Returns the username that will be used when connection to the POP3 server.
     * By default the value is null, which means that no username will be used.
     *
     * @return the POP3 username.
     */
    public String getPOP3Username() {
        return gateway.getUsername();
    }

    /**
     * Sets the username that will be used when connecting to the POP3 server.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -