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

📄 htmlemail.java

📁 也是apache的
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     * <code>HtmlEmail</code> object will not be changed.     *     * @param file The <code>File</code> to embed     * @param cid the Content-ID to use for the embedded <code>File</code>     * @return A String with the Content-ID of the file.     * @throws EmailException when the supplied <code>File</code> cannot be used     *  or if the file has already been embedded;     *  also see {@link javax.mail.internet.MimeBodyPart} for definitions     * @since 1.1     */    public String embed(File file, String cid) throws EmailException    {        if (EmailUtils.isEmpty(file.getName()))        {            throw new EmailException("file name cannot be null or empty");        }        // verify that the File can provide a canonical path        String filePath = null;        try        {            filePath = file.getCanonicalPath();        }        catch (IOException ioe)        {            throw new EmailException("couldn't get canonical path for "                    + file.getName(), ioe);        }        // check if a FileDataSource for this name has already been attached;        // if so, return the cached CID value.        if (inlineEmbeds.containsKey(file.getName()))        {            InlineImage ii = (InlineImage) inlineEmbeds.get(file.getName());            FileDataSource fileDataSource = (FileDataSource) ii.getDataSource();            // make sure the supplied file has the same canonical path            // as the one already associated with this name.            String existingFilePath = null;            try            {                existingFilePath = fileDataSource.getFile().getCanonicalPath();            }            catch (IOException ioe)            {                throw new EmailException("couldn't get canonical path for file "                        + fileDataSource.getFile().getName()                        + "which has already been embedded", ioe);            }            if (filePath.equals(existingFilePath))            {                return ii.getCid();            }            else            {                throw new EmailException("embedded name '" + file.getName()                    + "' is already bound to file " + existingFilePath                    + "; existing names cannot be rebound");            }        }        // verify that the file is valid        if (!file.exists())        {            throw new EmailException("file " + filePath + " doesn't exist");        }        if (!file.isFile())        {            throw new EmailException("file " + filePath + " isn't a normal file");        }        if (!file.canRead())        {            throw new EmailException("file " + filePath + " isn't readable");        }        return embed(new FileDataSource(file), file.getName());    }    /**     * Embeds the specified <code>DataSource</code> in the HTML using a     * randomly generated Content-ID. Returns the generated Content-ID string.     *     * @param dataSource the <code>DataSource</code> to embed     * @param name the name that will be set in the filename header field     * @return the generated Content-ID for this <code>DataSource</code>     * @throws EmailException if the embedding fails or if <code>name</code> is     * null or empty     * @see #embed(DataSource, String, String)     * @since 1.1     */    public String embed(DataSource dataSource, String name) throws EmailException    {        // check if the DataSource has already been attached;        // if so, return the cached CID value.        if (inlineEmbeds.containsKey(name))        {            InlineImage ii = (InlineImage) inlineEmbeds.get(name);            // make sure the supplied URL points to the same thing            // as the one already associated with this name.            if (dataSource.equals(ii.getDataSource()))            {                return ii.getCid();            }            else            {                throw new EmailException("embedded DataSource '" + name                    + "' is already bound to name " + ii.getDataSource().toString()                    + "; existing names cannot be rebound");            }        }        String cid = EmailUtils.randomAlphabetic(HtmlEmail.CID_LENGTH).toLowerCase();        return embed(dataSource, name, cid);    }    /**     * Embeds the specified <code>DataSource</code> in the HTML using the     * specified Content-ID. Returns the specified Content-ID string.     *     * @param dataSource the <code>DataSource</code> to embed     * @param name the name that will be set in the filename header field     * @param cid the Content-ID to use for this <code>DataSource</code>     * @return the supplied Content-ID for this <code>DataSource</code>     * @throws EmailException if the embedding fails or if <code>name</code> is     * null or empty     * @since 1.1     */    public String embed(DataSource dataSource, String name, String cid)        throws EmailException    {        if (EmailUtils.isEmpty(name))        {            throw new EmailException("name cannot be null or empty");        }        MimeBodyPart mbp = new MimeBodyPart();        try        {            mbp.setDataHandler(new DataHandler(dataSource));            mbp.setFileName(name);            mbp.setDisposition("inline");            mbp.setContentID("<" + cid + ">");            InlineImage ii = new InlineImage(cid, dataSource, mbp);            this.inlineEmbeds.put(name, ii);            return cid;        }        catch (MessagingException me)        {            throw new EmailException(me);        }    }    /**     * Does the work of actually building the email.     *     * @exception EmailException if there was an error.     * @since 1.0     */    public void buildMimeMessage() throws EmailException    {        try        {            build();        }        catch (MessagingException me)        {            throw new EmailException(me);        }        super.buildMimeMessage();    }    /**     * @throws EmailException EmailException     * @throws MessagingException MessagingException     */    private void build() throws MessagingException, EmailException    {        MimeMultipart container = this.getContainer();        MimeMultipart subContainer = null;        BodyPart msgHtml = null;        BodyPart msgText = null;        container.setSubType("related");        subContainer = new MimeMultipart("alternative");        if (EmailUtils.isNotEmpty(this.text))        {            msgText = new MimeBodyPart();            if (this.inlineEmbeds.size() > 0)            {                subContainer.addBodyPart(msgText);            }            else            {                container.addBodyPart(msgText);            }            // apply default charset if one has been set            if (EmailUtils.isNotEmpty(this.charset))            {                msgText.setContent(                    this.text,                    Email.TEXT_PLAIN + "; charset=" + this.charset);            }            else            {                msgText.setContent(this.text, Email.TEXT_PLAIN);            }        }        if (EmailUtils.isNotEmpty(this.html))        {            msgHtml = new MimeBodyPart();            if (this.inlineEmbeds.size() > 0)            {                subContainer.addBodyPart(msgHtml);            }            else            {                container.addBodyPart(msgHtml);            }            // apply default charset if one has been set            if (EmailUtils.isNotEmpty(this.charset))            {                msgHtml.setContent(                    this.html,                    Email.TEXT_HTML + "; charset=" + this.charset);            }            else            {                msgHtml.setContent(this.html, Email.TEXT_HTML);            }            Iterator iter = this.inlineEmbeds.values().iterator();            while (iter.hasNext())            {                InlineImage ii = (InlineImage) iter.next();                container.addBodyPart(ii.getMbp());            }        }        if (this.inlineEmbeds.size() > 0)        {            // add sub container to message            this.addPart(subContainer, 0);        }    }    /**     * Private bean class that encapsulates data about URL contents     * that are embedded in the final email.     * @since 1.1     */    private static class InlineImage    {        /** content id */        private String cid;        /** <code>DataSource</code> for the content */        private DataSource dataSource;        /** the <code>MimeBodyPart</code> that contains the encoded data */        private MimeBodyPart mbp;        /**         * Creates an InlineImage object to represent the         * specified content ID and <code>MimeBodyPart</code>.         * @param cid the generated content ID         * @param dataSource the <code>DataSource</code> that represents the content         * @param mbp the <code>MimeBodyPart</code> that contains the encoded         * data         */        public InlineImage(String cid, DataSource dataSource, MimeBodyPart mbp)        {            this.cid = cid;            this.dataSource = dataSource;            this.mbp = mbp;        }        /**         * Returns the unique content ID of this InlineImage.         * @return the unique content ID of this InlineImage         */        public String getCid()        {            return cid;        }        /**         * Returns the <code>DataSource</code> that represents the encoded content.         * @return the <code>DataSource</code> representing the encoded content         */        public DataSource getDataSource()        {            return dataSource;        }        /**         * Returns the <code>MimeBodyPart</code> that contains the         * encoded InlineImage data.         * @return the <code>MimeBodyPart</code> containing the encoded         * InlineImage data         */        public MimeBodyPart getMbp()        {            return mbp;        }        // equals()/hashCode() implementations, since this class        // is stored as a entry in a Map.        /**         * {@inheritDoc}         * @return true if the other object is also an InlineImage with the same cid.         */        public boolean equals(Object obj)        {            if (this == obj)            {                return true;            }            if (!(obj instanceof InlineImage))            {                return false;            }            InlineImage that = (InlineImage) obj;            return this.cid.equals(that.cid);        }        /**         * {@inheritDoc}         * @return the cid hashCode.         */        public int hashCode()        {            return cid.hashCode();        }    }}

⌨️ 快捷键说明

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