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

📄 james.java

📁 java 开发的邮件服务器平台。支持以下协议。 协议可以修改为自己的专门标识
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
     *     * MIME layout of the bounce message:     *     * multipart (mixed)/     *     contentPartRoot (body) = mpContent (alternative)/     *           part (body) = message     *     part (body) = original     *     */    public void bounce(Mail mail, String message, MailAddress bouncer) throws MessagingException {        MimeMessage orig = mail.getMessage();        //Create the reply message        MimeMessage reply = (MimeMessage) orig.reply(false);        //If there is a Return-Path header,        String[] returnPathHeaders = orig.getHeader(RFC2822Headers.RETURN_PATH);        String returnPathHeader = null;        if (returnPathHeaders != null) {            // TODO: Take a look at the JavaMail spec to see if the originating header            //       is guaranteed to be at position 0            returnPathHeader = returnPathHeaders[0];            if (returnPathHeader != null) {                returnPathHeader = returnPathHeader.trim();                if (returnPathHeader.equals("<>")) {                    if (getLogger().isInfoEnabled())                        getLogger().info("Processing a bounce request for a message with an empty return path.  No bounce will be sent.");                    return;                } else {                    if (getLogger().isInfoEnabled())                        getLogger().info("Processing a bounce request for a message with a return path header.  The bounce will be sent to " + returnPathHeader);                    //Return the message to that address, not to the Reply-To address                    reply.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(returnPathHeader));                }            }        } else {            getLogger().warn("Mail to be bounced does not contain a Return-Path header.");        }        reply.setSentDate(new Date());        reply.setHeader(RFC2822Headers.RETURN_PATH,"<>");        //Create the list of recipients in our MailAddress format        Collection recipients = new HashSet();        Address addresses[] = reply.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]));                }            }        }        //Change the sender...        reply.setFrom(bouncer.toInternetAddress());        try {            //Create the message body            MimeMultipart multipart = new MimeMultipart("mixed");            // Create the message            MimeMultipart mpContent = new MimeMultipart("alternative");            MimeBodyPart contentPartRoot = new MimeBodyPart();            contentPartRoot.setContent(mpContent);            multipart.addBodyPart(contentPartRoot);            MimeBodyPart part = new MimeBodyPart();            part.setText(message);            mpContent.addBodyPart(part);            //Add the original message as the second mime body part            part = new MimeBodyPart();            part.setContent(orig, "message/rfc822");            if ((orig.getSubject() != null) && (orig.getSubject().trim().length() > 0)) {                part.setFileName(orig.getSubject().trim());            } else {                part.setFileName("No Subject");            }            part.setDisposition(javax.mail.Part.ATTACHMENT);            multipart.addBodyPart(part);            reply.setContent(multipart);        } catch (Exception ioe) {            throw new MessagingException("Unable to create multipart body", ioe);        }        reply.saveChanges();        //Send it off ... with null reverse-path        sendMail(null, recipients, reply);    }    /**     * Returns whether that account has a local inbox on this server     *     * @param name the name to be checked     *     * @return whether the account has a local inbox     */    public boolean isLocalUser(String name) {        if (ignoreCase) {            return localusers.containsCaseInsensitive(name);        } else {            return localusers.contains(name);        }    }    /**     * Returns the address of the postmaster for this server.     *     * @return the <code>MailAddress</code> for the postmaster     */    public MailAddress getPostmaster() {        return postmaster;    }    public void storeMail(MailAddress sender, MailAddress recipient, MimeMessage message)        throws MessagingException {        String username;        if (recipient == null) {            throw new IllegalArgumentException("Recipient for mail to be spooled cannot be null.");        }        if (message == null) {            throw new IllegalArgumentException("Mail message to be spooled cannot be null.");        }        if (ignoreCase) {            String originalUsername = recipient.getUser();            username = localusers.getRealName(originalUsername);            if (username == null) {                StringBuffer errorBuffer =                    new StringBuffer(128)                        .append("The inbox for user ")                        .append(originalUsername)                        .append(" was not found on this server.");                throw new MessagingException(errorBuffer.toString());            }        } else {            username = recipient.getUser();        }        JamesUser user;        if (enableAliases || enableForwarding) {            user = (JamesUser) localusers.getUserByName(username);            if (enableAliases && user.getAliasing()) {                username = user.getAlias();            }            // Forwarding takes precedence over local aliases            if (enableForwarding && user.getForwarding()) {                MailAddress forwardTo = user.getForwardingDestination();                if (forwardTo == null) {                    StringBuffer errorBuffer =                        new StringBuffer(128)                            .append("Forwarding was enabled for ")                            .append(username)                            .append(" but no forwarding address was set for this account.");                    throw new MessagingException(errorBuffer.toString());                }                Collection recipients = new HashSet();                recipients.add(forwardTo);                try {                    sendMail(sender, recipients, message);                    if (getLogger().isInfoEnabled()) {                        StringBuffer logBuffer =                            new StringBuffer(128)                                    .append("Mail for ")                                    .append(username)                                    .append(" forwarded to ")                                    .append(forwardTo.toString());                        getLogger().info(logBuffer.toString());                    }                    return;                } catch (MessagingException me) {                    if (getLogger().isErrorEnabled()) {                        StringBuffer logBuffer =                            new StringBuffer(128)                                    .append("Error forwarding mail to ")                                    .append(forwardTo.toString())                                    .append("attempting local delivery");                        getLogger().error(logBuffer.toString());                    }                    throw me;                }            }        }        Collection recipients = new HashSet();        recipients.add(recipient);        MailImpl mailImpl = new MailImpl(getId(), sender, recipients, message);        MailRepository userInbox = getUserInbox(username);        if (userInbox == null) {            StringBuffer errorBuffer =                new StringBuffer(128)                    .append("The inbox for user ")                    .append(username)                    .append(" was not found on this server.");            throw new MessagingException(errorBuffer.toString());        }        userInbox.store(mailImpl);    }    /**     * Return the major version number for the server     *     * @return the major vesion number for the server     */    public int getMajorVersion() {        return 2;    }    /**     * Return the minor version number for the server     *     * @return the minor vesion number for the server     */    public int getMinorVersion() {        return 1;    }    /**     * Check whether the mail domain in question is to be     * handled by this server.     *     * @param serverName the name of the server to check     * @return whether the server is local     */    public boolean isLocalServer( final String serverName ) {        return serverNames.contains(serverName.toLowerCase(Locale.US));    }    /**     * Return the type of the server     *     * @return the type of the server     */    public String getServerInfo() {        return "Apache JAMES";    }    /**     * Return the logger for the Mailet API     *     * @return the logger for the Mailet API     */    private Logger getMailetLogger() {        if (mailetLogger == null) {            mailetLogger = getLogger().getChildLogger("Mailet");        }        return mailetLogger;    }    /**     * Log a message to the Mailet logger     *     * @param message the message to pass to the Mailet logger     */    public void log(String message) {        getMailetLogger().info(message);    }    /**     * Log a message and a Throwable to the Mailet logger     *     * @param message the message to pass to the Mailet logger     * @param t the <code>Throwable</code> to be logged     */    public void log(String message, Throwable t) {        getMailetLogger().info(message,t);    }    /**     * Adds a user to this mail server. Currently just adds user to a     * UsersRepository.     *     * @param userName String representing user name, that is the portion of     * an email address before the '@<domain>'.     * @param password String plaintext password     * @return boolean true if user added succesfully, else false.     */    public boolean addUser(String userName, String password) {        boolean success;        DefaultJamesUser user = new DefaultJamesUser(userName, "SHA");        user.setPassword(password);        user.initialize();        success = localusers.addUser(user);        return success;    }    /**     * Performs DNS lookups as needed to find servers which should or might     * support SMTP.     * Returns an Iterator over HostAddress, a specialized subclass of     * javax.mail.URLName, which provides location information for     * servers that are specified as mail handlers for the given     * hostname.  This is done using MX records, and the HostAddress     * instances are returned sorted by MX priority.  If no host is     * found for domainName, the Iterator returned will be empty and the     * first call to hasNext() will return false.     *     * @see org.apache.james.DNSServer#getSMTPHostAddresses(String)     * @since Mailet API v2.2.0a16-unstable     * @param domainName - the domain for which to find mail servers     * @return an Iterator over HostAddress instances, sorted by priority     */    public Iterator getSMTPHostAddresses(String domainName) {        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.getSMTPHostAddresses(domainName);    }}

⌨️ 快捷键说明

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