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

📄 smtpclient.java

📁 java开源的企业总线.xmlBlaster
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/*------------------------------------------------------------------------------ Name:      SmtpClient.java Project:   xmlBlaster.org Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file Comment:   javac EmailData.java SmtpClient.java ------------------------------------------------------------------------------*/package org.xmlBlaster.util.protocol.email;import javax.activation.DataHandler;import javax.activation.DataSource;import javax.mail.Multipart;import javax.mail.Session;import javax.mail.Message;import javax.mail.Transport;import javax.mail.Authenticator;import javax.mail.PasswordAuthentication;import javax.mail.MessagingException;import javax.mail.internet.MimeBodyPart;import javax.mail.internet.MimeMessage;import javax.mail.internet.InternetAddress;import javax.mail.internet.AddressException;import javax.mail.internet.MimeMultipart;import java.net.URISyntaxException;import java.sql.Timestamp;import java.util.Date;import java.util.Properties;import java.util.logging.Logger;import java.util.logging.Level;import org.xmlBlaster.util.Global;import org.xmlBlaster.util.ReplaceVariable;import org.xmlBlaster.util.XbUri;import org.xmlBlaster.util.XmlBlasterException;import org.xmlBlaster.util.context.ContextNode;import org.xmlBlaster.util.def.Constants;import org.xmlBlaster.util.def.ErrorCode;import org.xmlBlaster.util.plugin.I_Plugin;import org.xmlBlaster.util.plugin.I_PluginConfig;import org.xmlBlaster.util.plugin.PluginInfo;/** * This class sends outgoing emails. * <p> * Developer note: Please don't use log.severe() or log.warning() to avoid * recursion for logging-notification mails. *  * @see <a *      href="http://www-106.ibm.com/developerworks/java/library/j-james1.html">James *      MTA</a> * @see <a href="http://java.sun.com/products/javamail/javadocs/index.html">Java *      Mail API</a> * @see <a href="http://java.sun.com/developer/onlineTraining/JavaMail/contents.html">Javamail tutorial</a> * @see <a href="http://www.xmlBlaster.org/xmlBlaster/doc/requirements/protocol.email.html">The protocol.email requirement</a> * @author <a href="mailto:xmlBlaster@marcelruff.info">Marcel Ruff</a> */public class SmtpClient extends Authenticator implements I_Plugin, SmtpClientMBean {   private static Logger log = Logger.getLogger(SmtpClient.class.getName());   private Global glob;      private ContextNode contextNode;      private PluginInfo pluginInfo;   // If the mail.smtp.from changes we would need a new session instance with its own properties   private Session session;   private PasswordAuthentication authentication;   private XbUri xbUri;      private boolean isInitialized;      /**     * Setting this to true we can force the messageId attachment to    * always be base64 encoded.    * <br />    * Javamail does base64 encoding automatically if need so    * the default of this variable is false.    */   private boolean messageIdForceBase64;   /**     * Setting this to true we can force the MsgUnit attachment to    * always be base64 encoded.    * <br />    * Javamail does base64 encoding automatically if need so    * the default of this variable is false.    */   private boolean contentForceBase64;      private boolean breakLongMessageIdLine;      /**    * mail.smtp.timeout    * Socket I/O timeout value in milliseconds. Default is infinite timeout.    */   private int smtpIoTimeout;   /**    * mail.smtp.connectiontimeout    * Socket connection timeout value in milliseconds. Default is infinite timeout.    */   private int smtpConnectionTimeout;   /**    * Add 'Expires:' email header.     * If the message to send has an expiry date and this    * addExpiresHeader=true we send an 'Expires:' header in the email    * (Expiry Date Indication).     * <br />    * Supported as new RFC 822 header (Expires:).  In general, no    * automatic action can be expected by MTAs.    * <br />    * Defaults to true.    * @see http://www.faqs.org/rfcs/rfc2156.html    */   private boolean addExpiresHeader;      /**    * Comma separated list of fileName extensions to send attachment as inline    */   private String inlineExtension;   /** My JMX registration */   private Object mbeanHandle;   public static final String OBJECTENTRY_KEY = SmtpClient.class.getName();      /**    * @return Returns the user.    */   public String getUser() {      return this.xbUri.getUser();   }   /**    * The SmtpClient is a singleton in the Global scope.     * Access this singleton for the given global, and if it    * doesn't exist create one instance.    * @param glob    * @param pluginInfo    * @return never null    * @throws XmlBlasterException     */   public static SmtpClient getSmtpClient(Global glob, I_PluginConfig pluginConfig)                              throws XmlBlasterException {      Global serverNode = (org.xmlBlaster.util.Global)glob.getObjectEntry(Constants.OBJECT_ENTRY_ServerScope);      if (serverNode == null) serverNode = glob;            SmtpClient smtpClient = (SmtpClient)serverNode.getObjectEntry(OBJECTENTRY_KEY);      if (smtpClient != null)         return smtpClient;            synchronized(glob.objectMapMonitor) {         smtpClient = (SmtpClient)serverNode.getObjectEntry(OBJECTENTRY_KEY);         if (smtpClient == null) {            smtpClient = new SmtpClient();            smtpClient.setSessionProperties(null, glob, pluginConfig); // adds itself as ObjectEntry         }         return smtpClient;      }   }   /**    * Called from runlevel manager on server side.     */   public SmtpClient() {   }   /**    * Access the xmlBlaster internal name of the protocol driver.    * @return The configured type in xmlBlasterPlugins.xml, defaults to "smtp"    */   public String getProtocolId() {      return (this.pluginInfo == null) ? "smtp" : this.pluginInfo.getType();   }   /**    * Enforced by I_Plugin    *     * @return The configured type in xmlBlaster.properties, defaults to "smtp"    */   public String getType() {      return getProtocolId();   }   /**    * The command line key prefix    * @return Defaults to "plugin/smtp"    */   public String getEnvPrefix() {      return "plugin/" + getType().toLowerCase();   }   /** Enforced by I_Plugin */   public String getVersion() {      return (this.pluginInfo == null) ? "1.0" : this.pluginInfo.getVersion();   }   /**    * This method is called by the PluginManager (enforced by I_Plugin). The    * SmtpClient singleton is registered in the Global object store.    *     * @see org.xmlBlaster.util.plugin.I_Plugin#init(org.xmlBlaster.util.Global,org.xmlBlaster.util.plugin.PluginInfo)    */   public void init(org.xmlBlaster.util.Global glob, PluginInfo pluginInfo)         throws XmlBlasterException {      this.glob = glob;      this.pluginInfo = pluginInfo;      setSessionProperties(null, glob, pluginInfo);      // Make this singleton available for others      // key="org.xmlBlaster.util.protocol.email.SmtpClient"      Global serverNode = (org.xmlBlaster.util.Global)this.glob.getObjectEntry(Constants.OBJECT_ENTRY_ServerScope);      if (serverNode == null) serverNode = this.glob;      serverNode.addObjectEntry(OBJECTENTRY_KEY, this);   }   /**    * Used by Authenticator to access user name and password    */   public PasswordAuthentication getPasswordAuthentication() {      if (log.isLoggable(Level.FINE))         log.fine("Entering getPasswordAuthentication: "               + this.authentication.toString());      return this.authentication;   }   /**    * Set session properties and create a session.    * <p>    * Example settings:    * </p>    *     * <pre>    * Properties props = System.getProperties();    * props.put(&quot;mail.debug&quot;, &quot;true&quot;);    * props.put(&quot;mail.smtp.url&quot;, &quot;smtp://demo:secret@localhost:2525&quot;);    * </pre>    *     * <p>    * If a property is not found <tt>System.getProperty()</tt> is consulted.    * </p>    *     * @see <a    *      href="http://java.sun.com/products/javamail/javadocs/com/sun/mail/smtp/package-summary.html">SMTP    *      API</a>    * @see <a    *      href="http://java.sun.com/products/javamail/javadocs/com/sun/mail/pop3/package-summary.html">POP3    *      API</a>    */   public synchronized void setSessionProperties(Properties props, Global glob,         I_PluginConfig pluginConfig) throws XmlBlasterException {            if (this.isInitialized) {         if (log.isLoggable(Level.FINE))            log.fine("Ignoring multiple setSessionProperties() call");         return;      }      this.glob = glob;      if (props == null)         props = new Properties();      if (props.getProperty("mail.debug") == null)         props.put("mail.debug", glob.get("mail.debug", System.getProperty(               "mail.debug", "false"), null, pluginConfig));      String uri = glob.get("mail.smtp.url", System            .getProperty("mail.smtp.url"), null, pluginConfig);      if (uri == null) {         throw new XmlBlasterException(glob,               ErrorCode.RESOURCE_CONFIGURATION_ADDRESS, "SmtpClient",               "Please configure a mail.smtp.url to access the SMTP MTA, for example 'mail.smtp.url=smtp://joe:password@smtp.xmlBlaster.org:25'");      }      try {         this.xbUri = new XbUri(uri.trim());      } catch (URISyntaxException e) {         throw new XmlBlasterException(glob,               ErrorCode.RESOURCE_CONFIGURATION_ADDRESS, "SmtpClient",               "Your URI '" + uri +               "' is illegal", e);      }      if (this.xbUri.getHost() == null) {         throw new XmlBlasterException(glob,

⌨️ 快捷键说明

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