mailprocessor.java

来自「jGossip是一个简单而功能强大的Java论坛软件(消息板)」· Java 代码 · 共 112 行

JAVA
112
字号
/*
 * $$Id: MailProcessor.java,v 1.9 2004/05/05 14:02:12 bel70 Exp $$
 *
 * ***** BEGIN LICENSE BLOCK *****
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License
 * at http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 * the License for the specific language governing rights and
 * limitations under the License.
 *
 * The Original Code is JGossip forum code.
 *
 * The Initial Developer of the Original Code is the JResearch, Org.
 * Portions created by the Initial Developer are Copyright (C) 2004
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *              Dmitry Belov <bel@jresearch.org>
 *
 * ***** END LICENSE BLOCK ***** */
/*
 * Created on Oct 28, 2003
 *
  */
package org.jresearch.gossip.mail;

import java.util.Properties;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.jresearch.gossip.IConst;
import org.jresearch.gossip.configuration.Configurator;
import org.jresearch.gossip.log.ForumLog;


/**
 * DOCUMENT ME!
 *
 * @author dbelov
 */
public class MailProcessor
    implements com.evelopers.common.concurrent.QueueMessageProcessor {
    public static Session _mailSession;
    private static SmtpAuthenticator _authenticator;

    /* (non-Javadoc)
     * @see com.evelopers.common.concurrent.QueueMessageProcessor#processMessage(java.lang.Object)
     */
    public void processMessage(Object mess) {
        MailMessage mailmess = (MailMessage) mess;

        // Get (or create and cache) mail session
        Session s = null;
		Transport trans=null;
        try {
            s = getMailSession();

            // Create new message
            MimeMessage msg = new MimeMessage(s);

            // Put data from request into message
            msg.setText(mailmess.getMessagetext());
            msg.setSubject(mailmess.getSubject());

            Address fromAddr = new InternetAddress(mailmess.getAddrfrom(),
                    mailmess.getNamefrom());
            msg.setFrom(fromAddr);

            Address toAddr = new InternetAddress(mailmess.getAddrto(),
                    mailmess.getNameto());
            msg.addRecipient(Message.RecipientType.TO, toAddr);
            msg.addHeaderLine("Content-Type: text/html;");
			msg.saveChanges();
            // Send the message
			trans=s.getTransport("smtp");
			trans.connect();
			trans.sendMessage(msg,new Address[]{toAddr});
			trans.close();
        } catch (Exception e) {
            ForumLog.getInstance().getForumLogger().error(e.getMessage());
        }
    }

    //	Open the mail session if it isn't already open.
    protected Session getMailSession() throws Exception {
        // Create mail session if it doesn't exist
        if (_mailSession == null) {
            _authenticator = new SmtpAuthenticator();

            Configurator config = Configurator.getInstance();
            Properties props = new Properties();
            props.put("mail.smtp.user", config.get(IConst.CONFIG.MAILUSER));
            props.put("mail.smtp.host", config.get(IConst.CONFIG.MAILHOST));
			props.put("mail.smtp.port", config.get(IConst.CONFIG.SMTP_SERVER_PORT));
			props.put("mail.mime.charset", config.get(IConst.CONFIG.CHARSET));
			props.put("mail.smtp.auth","true");
            _mailSession = Session.getInstance(props, _authenticator);
        }

        return _mailSession;
    }
}

⌨️ 快捷键说明

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