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

📄 james.java

📁 java 开发的邮件服务器平台。支持以下协议。 协议可以修改为自己的专门标识
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
        context.put( Constants.POSTMASTER, postmaster );        if (!isLocalServer(postmaster.getHost())) {            StringBuffer warnBuffer                = new StringBuffer(320)                        .append("The specified postmaster address ( ")                        .append(postmaster)                        .append(" ) is not a local address.  This is not necessarily a problem, but it does mean that emails addressed to the postmaster will be routed to another server.  For some configurations this may cause problems.");            getLogger().warn(warnBuffer.toString());        }        Configuration userNamesConf = conf.getChild("usernames");        ignoreCase = userNamesConf.getAttributeAsBoolean("ignoreCase", false);        enableAliases = userNamesConf.getAttributeAsBoolean("enableAliases", false);        enableForwarding = userNamesConf.getAttributeAsBoolean("enableForwarding", false);        //Get localusers        try {            localusers = (UsersRepository) usersStore.getRepository("LocalUsers");        } catch (Exception e) {            getLogger().error("Cannot open private UserRepository");            throw e;        }        //}        compMgr.put( UsersRepository.ROLE, (Component)localusers);        getLogger().info("Local users repository opened");        Configuration inboxConf = conf.getChild("inboxRepository");        Configuration inboxRepConf = inboxConf.getChild("repository");        try {            localInbox = (MailRepository) mailstore.select(inboxRepConf);        } catch (Exception e) {            getLogger().error("Cannot open private MailRepository");            throw e;        }        inboxRootURL = inboxRepConf.getAttribute("destinationURL");        getLogger().info("Private Repository LocalInbox opened");        // Add this to comp        compMgr.put( MailServer.ROLE, this);        spool = mailstore.getInboundSpool();        if (getLogger().isDebugEnabled()) {            getLogger().debug("Got spool");        }        // For mailet engine provide MailetContext        //compMgr.put("org.apache.mailet.MailetContext", this);        // For AVALON aware mailets and matchers, we put the Component object as        // an attribute        attributes.put(Constants.AVALON_COMPONENT_MANAGER, compMgr);        System.out.println(SOFTWARE_NAME_VERSION);        getLogger().info("JAMES ...init end");    }    /**     * Place a mail on the spool for processing     *     * @param message the message to send     *     * @throws MessagingException if an exception is caught while placing the mail     *                            on the spool     */    public void sendMail(MimeMessage message) throws MessagingException {        MailAddress sender = new MailAddress((InternetAddress)message.getFrom()[0]);        Collection recipients = new HashSet();        Address addresses[] = message.getAllRecipients();        if (addresses != null) {            for (int i = 0; i < addresses.length; i++) {                // Javamail treats the "newsgroups:" header field as a                // recipient, so we want to filter those out.                if ( addresses[i] instanceof InternetAddress ) {                    recipients.add(new MailAddress((InternetAddress)addresses[i]));                }            }        }        sendMail(sender, recipients, message);    }    /**     * Place a mail on the spool for processing     *     * @param sender the sender of the mail     * @param recipients the recipients of the mail     * @param message the message to send     *     * @throws MessagingException if an exception is caught while placing the mail     *                            on the spool     */    public void sendMail(MailAddress sender, Collection recipients, MimeMessage message)            throws MessagingException {        sendMail(sender, recipients, message, Mail.DEFAULT);    }    /**     * Place a mail on the spool for processing     *     * @param sender the sender of the mail     * @param recipients the recipients of the mail     * @param message the message to send     * @param state the state of the message     *     * @throws MessagingException if an exception is caught while placing the mail     *                            on the spool     */    public void sendMail(MailAddress sender, Collection recipients, MimeMessage message, String state)            throws MessagingException {        MailImpl mail = new MailImpl(getId(), sender, recipients, message);        mail.setState(state);        sendMail(mail);    }    /**     * Place a mail on the spool for processing     *     * @param sender the sender of the mail     * @param recipients the recipients of the mail     * @param msg an <code>InputStream</code> containing the message     *     * @throws MessagingException if an exception is caught while placing the mail     *                            on the spool     */    public void sendMail(MailAddress sender, Collection recipients, InputStream msg)            throws MessagingException {        // parse headers        MailHeaders headers = new MailHeaders(msg);        // if headers do not contains minimum REQUIRED headers fields throw Exception        if (!headers.isValid()) {            throw new MessagingException("Some REQURED header field is missing. Invalid Message");        }        ByteArrayInputStream headersIn = new ByteArrayInputStream(headers.toByteArray());        sendMail(new MailImpl(getId(), sender, recipients, new SequenceInputStream(headersIn, msg)));    }    /**     * Place a mail on the spool for processing     *     * @param mail the mail to place on the spool     *     * @throws MessagingException if an exception is caught while placing the mail     *                            on the spool     */    public void sendMail(Mail mail) throws MessagingException {        MailImpl mailimpl = (MailImpl)mail;        try {            spool.store(mailimpl);        } catch (Exception e) {            try {                spool.remove(mailimpl);            } catch (Exception ignored) {            }            throw new MessagingException("Exception spooling message: " + e.getMessage(), e);        }        if (getLogger().isDebugEnabled()) {            StringBuffer logBuffer =                new StringBuffer(64)                        .append("Mail ")                        .append(mailimpl.getName())                        .append(" pushed in spool");            getLogger().debug(logBuffer.toString());        }    }    /**     * <p>Retrieve the mail repository for a user</p>     *     * <p>For POP3 server only - at the moment.</p>     *     * @param userName the name of the user whose inbox is to be retrieved     *     * @return the POP3 inbox for the user     */    public synchronized MailRepository getUserInbox(String userName) {        MailRepository userInbox = (MailRepository) null;        userInbox = (MailRepository) mailboxes.get(userName);        if (userInbox != null) {            return userInbox;        } else if (mailboxes.containsKey(userName)) {            // we have a problem            getLogger().error("Null mailbox for non-null key");            throw new RuntimeException("Error in getUserInbox.");        } else {            // need mailbox object            if (getLogger().isDebugEnabled()) {                getLogger().debug("Retrieving and caching inbox for " + userName );            }            StringBuffer destinationBuffer =                new StringBuffer(192)                        .append(inboxRootURL)                        .append(userName)                        .append("/");            String destination = destinationBuffer.toString();            DefaultConfiguration mboxConf                = new DefaultConfiguration("repository", "generated:AvalonFileRepository.compose()");            mboxConf.setAttribute("destinationURL", destination);            mboxConf.setAttribute("type", "MAIL");            try {                userInbox = (MailRepository) mailstore.select(mboxConf);                mailboxes.put(userName, userInbox);            } catch (Exception e) {                if (getLogger().isErrorEnabled())                {                    getLogger().error("Cannot open user Mailbox" + e);                }                throw new RuntimeException("Error in getUserInbox." + e);            }            return userInbox;        }    }    /**     * Return a new mail id.     *     * @return a new mail id     */    public String getId() {        long localCount = -1;        synchronized (James.class) {            localCount = count++;        }        StringBuffer idBuffer =            new StringBuffer(64)                    .append("Mail")                    .append(System.currentTimeMillis())                    .append("-")                    .append(localCount);        return idBuffer.toString();    }    /**     * The main method.  Should never be invoked, as James must be called     * from within an Avalon framework container.     *     * @param args the command line arguments     */    public static void main(String[] args) {        System.out.println("ERROR!");        System.out.println("Cannot execute James as a stand alone application.");        System.out.println("To run James, you need to have the Avalon framework installed.");        System.out.println("Please refer to the Readme file to know how to run James.");    }    //Methods for MailetContext    /**     * <p>Get the prioritized list of mail servers for a given host.</p>     *     * <p>TODO: This needs to be made a more specific ordered subtype of Collection.</p>     *     * @param host     */    public Collection getMailServers(String host) {        DNSServer dnsServer = null;        try {            dnsServer = (DNSServer) compMgr.lookup( DNSServer.ROLE );        } catch ( final ComponentException cme ) {            getLogger().error("Fatal configuration error - DNS Servers lost!", cme );            throw new RuntimeException("Fatal configuration error - DNS Servers lost!");        }        return dnsServer.findMXRecords(host);    }    public Object getAttribute(String key) {        return attributes.get(key);    }    public void setAttribute(String key, Object object) {        attributes.put(key, object);    }    public void removeAttribute(String key) {        attributes.remove(key);    }    public Iterator getAttributeNames() {        Vector names = new Vector();        for (Enumeration e = attributes.keys(); e.hasMoreElements(); ) {            names.add(e.nextElement());        }        return names.iterator();    }    /**     * This generates a response to the Return-Path address, or the address of     * the message's sender if the Return-Path is not available.  Note that     * this is different than a mail-client's reply, which would use the     * Reply-To or From header. This will send the bounce with the server's     * postmaster as the sender.     */    public void bounce(Mail mail, String message) throws MessagingException {        bounce(mail, message, getPostmaster());    }    /**     * This generates a response to the Return-Path address, or the     * address of the message's sender if the Return-Path is not     * available.  Note that this is different than a mail-client's     * reply, which would use the Reply-To or From header.     *     * Bounced messages are attached in their entirety (headers and     * content) and the resulting MIME part type is "message/rfc822".     *     * The attachment to the subject of the original message (or "No     * Subject" if there is no subject in the original message)     *     * There are outstanding issues with this implementation revolving     * around handling of the return-path header.

⌨️ 快捷键说明

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