📄 james.java
字号:
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 ServiceException 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. * * 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 { if (mail.getSender() == null) { if (getLogger().isInfoEnabled()) getLogger().info("Mail to be bounced contains a null (<>) reverse path. No bounce will be sent."); return; } else { // Bounce message goes to the reverse path, not to the Reply-To address if (getLogger().isInfoEnabled()) getLogger().info("Processing a bounce request for a message with a reverse path of " + mail.getSender().toString()); } MailImpl reply = rawBounce(mail,message); //Change the sender... reply.getMessage().setFrom(bouncer.toInternetAddress()); reply.getMessage().saveChanges(); //Send it off ... with null reverse-path reply.setSender(null); sendMail(reply); ContainerUtil.dispose(reply); } /** * Generates a bounce mail that is a bounce of the original message. * * @param bounceText the text to be prepended to the message to describe the bounce condition * * @return the bounce mail * * @throws MessagingException if the bounce mail could not be created */ private MailImpl rawBounce(Mail mail, String bounceText) throws MessagingException { //This sends a message to the james component that is a bounce of the sent message MimeMessage original = mail.getMessage(); MimeMessage reply = (MimeMessage) original.reply(false); reply.setSubject("Re: " + original.getSubject()); reply.setSentDate(new Date()); Collection recipients = new HashSet(); recipients.add(mail.getSender()); InternetAddress addr[] = { new InternetAddress(mail.getSender().toString())}; reply.setRecipients(Message.RecipientType.TO, addr); reply.setFrom(new InternetAddress(mail.getRecipients().iterator().next().toString())); reply.setText(bounceText); reply.setHeader(RFC2822Headers.MESSAGE_ID, "replyTo-" + mail.getName()); return new MailImpl( "replyTo-" + mail.getName(), new MailAddress(mail.getRecipients().iterator().next().toString()), 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; } /** * 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 3; } /** * 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. * * @deprecated we deprecated this in the MailServer interface and this is an implementation * this component depends already depends on a UsersRepository: clients could directly * use the addUser of the usersRepository. */ 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 ServiceException cme ) { getLogger().error("Fatal configuration error - DNS Servers lost!", cme ); throw new RuntimeException("Fatal configuration error - DNS Servers lost!"); } return dnsServer.getSMTPHostAddresses(domainName); } /** * This method has been moved to LocalDelivery (the only client of the method). * Now we can safely remove it from the Mailet API and from this implementation of MailetContext. * * The local field localDeliveryMailet will be removed when we remove the storeMail method. * * @deprecated since 2.2.0 look at the LocalDelivery code to find out how to do the local delivery. * @see org.apache.mailet.MailetContext#storeMail(org.apache.mailet.MailAddress, org.apache.mailet.MailAddress, javax.mail.internet.MimeMessage) */ public void storeMail(MailAddress sender, MailAddress recipient, MimeMessage msg) throws MessagingException { if (recipient == null) { throw new IllegalArgumentException("Recipient for mail to be spooled cannot be null."); } if (msg == null) { throw new IllegalArgumentException("Mail message to be spooled cannot be null."); } Collection recipients = new HashSet(); recipients.add(recipient); MailImpl m = new MailImpl(getId(),sender,recipients,msg); localDeliveryMailet.service(m); ContainerUtil.dispose(m); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -