Search in sources :

Example 11 with URLDataSource

use of javax.activation.URLDataSource in project camel by apache.

the class MimeMessageConsumeTest method populateMimeMessageBody.

/**
     * Lets encode a multipart mime message
     */
protected void populateMimeMessageBody(MimeMessage message) throws MessagingException {
    MimeBodyPart plainPart = new MimeBodyPart();
    plainPart.setText(body);
    MimeBodyPart htmlPart = new MimeBodyPart();
    htmlPart.setText("<html><body>" + body + "</body></html>");
    Multipart alt = new MimeMultipart("alternative");
    alt.addBodyPart(plainPart);
    alt.addBodyPart(htmlPart);
    Multipart mixed = new MimeMultipart("mixed");
    MimeBodyPart wrap = new MimeBodyPart();
    wrap.setContent(alt);
    mixed.addBodyPart(wrap);
    mixed.addBodyPart(plainPart);
    mixed.addBodyPart(htmlPart);
    DataSource ds;
    try {
        File f = new File(getClass().getResource("/log4j2.properties").toURI());
        ds = new FileDataSource(f);
    } catch (URISyntaxException ex) {
        ds = new URLDataSource(getClass().getResource("/log4j2.properties"));
    }
    DataHandler dh = new DataHandler(ds);
    BodyPart attachmentBodyPart;
    // Create another body part
    attachmentBodyPart = new MimeBodyPart();
    // Set the data handler to the attachment
    attachmentBodyPart.setDataHandler(dh);
    // Set the filename
    attachmentBodyPart.setFileName(dh.getName());
    // Set Disposition
    attachmentBodyPart.setDisposition(Part.ATTACHMENT);
    mixed.addBodyPart(plainPart);
    mixed.addBodyPart(htmlPart);
    // Add attachmentBodyPart to multipart
    mixed.addBodyPart(attachmentBodyPart);
    message.setContent(mixed);
}
Also used : MimeBodyPart(javax.mail.internet.MimeBodyPart) BodyPart(javax.mail.BodyPart) MimeMultipart(javax.mail.internet.MimeMultipart) Multipart(javax.mail.Multipart) URLDataSource(javax.activation.URLDataSource) MimeMultipart(javax.mail.internet.MimeMultipart) FileDataSource(javax.activation.FileDataSource) URISyntaxException(java.net.URISyntaxException) DataHandler(javax.activation.DataHandler) MimeBodyPart(javax.mail.internet.MimeBodyPart) File(java.io.File) FileDataSource(javax.activation.FileDataSource) URLDataSource(javax.activation.URLDataSource) DataSource(javax.activation.DataSource)

Example 12 with URLDataSource

use of javax.activation.URLDataSource in project adempiere by adempiere.

the class EMail method setContent.

//	addAttachment
/**
	 *	Set the message content
	 * 	@throws MessagingException
	 * 	@throws IOException
	 */
private void setContent() throws MessagingException, IOException {
    //	Local Character Set
    String charSetName = Ini.getCharset().name();
    if (charSetName == null || charSetName.length() == 0)
        // WebEnv.ENCODING - alternative iso-8859-1
        charSetName = "iso-8859-1";
    //
    m_msg.setSubject(getSubject(), charSetName);
    //	Simple Message
    if (m_attachments == null || m_attachments.size() == 0) {
        if (m_messageHTML == null || m_messageHTML.length() == 0)
            m_msg.setText(getMessageCRLF(), charSetName);
        else
            m_msg.setDataHandler(new DataHandler(new ByteArrayDataSource(m_messageHTML, charSetName, "text/html")));
        //
        log.fine("(simple) " + getSubject());
    } else //	Multi part message	***************************************
    {
        //	First Part - Message
        MimeBodyPart mbp_1 = new MimeBodyPart();
        mbp_1.setText("");
        if (m_messageHTML == null || m_messageHTML.length() == 0)
            mbp_1.setText(getMessageCRLF(), charSetName);
        else
            mbp_1.setDataHandler(new DataHandler(new ByteArrayDataSource(m_messageHTML, charSetName, "text/html")));
        // Create Multipart and its parts to it
        Multipart mp = new MimeMultipart();
        mp.addBodyPart(mbp_1);
        log.fine("(multi) " + getSubject() + " - " + mbp_1);
        //	for all attachments
        for (int i = 0; i < m_attachments.size(); i++) {
            Object attachment = m_attachments.get(i);
            DataSource ds = null;
            if (attachment instanceof File) {
                File file = (File) attachment;
                if (file.exists())
                    ds = new FileDataSource(file);
                else {
                    log.log(Level.WARNING, "File does not exist: " + file);
                    continue;
                }
            } else if (attachment instanceof URI) {
                URI url = (URI) attachment;
                ds = new URLDataSource(url.toURL());
            } else if (attachment instanceof DataSource)
                ds = (DataSource) attachment;
            else {
                log.log(Level.WARNING, "Attachement type unknown: " + attachment);
                continue;
            }
            //	Attachment Part
            MimeBodyPart mbp_2 = new MimeBodyPart();
            mbp_2.setDataHandler(new DataHandler(ds));
            mbp_2.setFileName(ds.getName());
            log.fine("Added Attachment " + ds.getName() + " - " + mbp_2);
            mp.addBodyPart(mbp_2);
        }
        //	Add to Message
        m_msg.setContent(mp);
    }
//	multi=part
}
Also used : Multipart(javax.mail.Multipart) MimeMultipart(javax.mail.internet.MimeMultipart) URLDataSource(javax.activation.URLDataSource) DataHandler(javax.activation.DataHandler) URI(java.net.URI) FileDataSource(javax.activation.FileDataSource) URLDataSource(javax.activation.URLDataSource) DataSource(javax.activation.DataSource) MimeMultipart(javax.mail.internet.MimeMultipart) FileDataSource(javax.activation.FileDataSource) MimeBodyPart(javax.mail.internet.MimeBodyPart) File(java.io.File)

Example 13 with URLDataSource

use of javax.activation.URLDataSource in project webservices-axiom by apache.

the class TestParseURI method runTest.

protected void runTest() throws Throwable {
    InstrumentedDataSource ds = new InstrumentedDataSource(new URLDataSource(XMLSample.SIMPLE.getUrl()));
    DataSourceRegistration registration = DataSourceRegistry.registerDataSource(ds);
    try {
        DocumentBuilder builder = dbf.newDocumentBuilder();
        Document document = builder.parse(registration.getURL().toExternalForm());
        assertThat(document.getDocumentElement().getLocalName()).isEqualTo("root");
        assertThat(ds.getOpenStreamCount()).isEqualTo(0);
    } finally {
        registration.unregister();
    }
}
Also used : InstrumentedDataSource(org.apache.axiom.testutils.activation.InstrumentedDataSource) URLDataSource(javax.activation.URLDataSource) DocumentBuilder(javax.xml.parsers.DocumentBuilder) DataSourceRegistration(org.apache.axiom.testutils.net.protocol.mem.DataSourceRegistration) Document(org.w3c.dom.Document)

Example 14 with URLDataSource

use of javax.activation.URLDataSource in project Lucee by lucee.

the class HtmlEmailImpl method embed.

/**
 * Embeds an URL in the HTML.
 *
 * <p>This method allows to embed a file located by an URL into
 * the mail body.  It allows, for instance, to add inline images
 * to the email.  Inline files may be referenced with a
 * <code>cid:xxxxxx</code> URL, where xxxxxx is the Content-ID
 * returned by the embed function.
 *
 * <p>Example of use:<br><code><pre>
 * HtmlEmail he = new HtmlEmail();
 * he.setHtmlMsg("&lt;html&gt;&lt;img src=cid:" +
 *  embed("file:/my/image.gif","image.gif") +
 *  "&gt;&lt;/html&gt;");
 * // code to set the others email fields (not shown)
 * </pre></code>
 *
 * @param url The URL of the file.
 * @param cid A String with the Content-ID of the file.
 * @param name The name that will be set in the filename header
 * field.
 * @throws EmailException when URL supplied is invalid
 *  also see javax.mail.internet.MimeBodyPart for definitions
 */
public void embed(URL url, String cid, String name) throws EmailException {
    // verify that the URL is valid
    try {
        InputStream is = url.openStream();
        is.close();
    } catch (IOException e) {
        throw new EmailException("Invalid URL");
    }
    MimeBodyPart mbp = new MimeBodyPart();
    try {
        mbp.setDataHandler(new DataHandler(new URLDataSource(url)));
        mbp.setFileName(name);
        mbp.setDisposition("inline");
        mbp.addHeader("Content-ID", "<" + cid + ">");
        this.inlineImages.add(mbp);
    } catch (MessagingException me) {
        throw new EmailException(me);
    }
}
Also used : URLDataSource(javax.activation.URLDataSource) MessagingException(javax.mail.MessagingException) InputStream(java.io.InputStream) EmailException(org.apache.commons.mail.EmailException) IOException(java.io.IOException) DataHandler(javax.activation.DataHandler) MimeBodyPart(javax.mail.internet.MimeBodyPart)

Example 15 with URLDataSource

use of javax.activation.URLDataSource in project weicoder by wdcode.

the class EmailJava method sendEmail.

/**
 * 发送Email
 * @param to 发送地址
 * @param subject 邮件标题
 * @param msg 邮件内容
 * @param attach 附件
 * @param flag 是否html
 */
private void sendEmail(String[] to, String subject, String msg, String attach, boolean flag) {
    try {
        // 参数设置
        Properties props = new Properties();
        // 指定SMTP服务器
        props.put("mail.smtp.host", getHost());
        // 是否需要SMTP验证
        props.put("mail.smtp.auth", isAuth());
        // 获得Session
        Session mailSession = Session.getDefaultInstance(props);
        // 创建细信息类
        Message message = new MimeMessage(mailSession);
        // 设置邮件服务器
        message.setFrom(new InternetAddress(getFrom()));
        // 收件人
        for (int i = 0; i < to.length; i++) {
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to[i]));
        }
        // 邮件主题
        message.setSubject(subject);
        // 是否支持HTML
        if (flag) {
            // HTML
            message.setContent(msg, getCharset());
        } else {
            // 普通文本
            message.setText(msg);
        }
        // 添加附件
        if (!EmptyUtil.isEmpty(attach)) {
            // 附件
            MimeBodyPart messageBodyPart = new MimeBodyPart();
            DataSource source = null;
            // 判断是本地文件还是远程
            if (attach.indexOf("http") == -1) {
                // 本地文件
                source = new FileDataSource(attach);
            } else {
                // 远程文件
                source = new URLDataSource(new URL(attach));
            }
            messageBodyPart.setDataHandler(new DataHandler(source));
            // 设置描述名字等
            String name = StringUtil.subStringLast(attach, StringConstants.BACKSLASH, StringConstants.POINT);
            // 添加文件名和描述
            messageBodyPart.setText(name);
            messageBodyPart.setFileName(name);
            // 附件
            Multipart multipart = new MimeMultipart();
            // 添加附件
            multipart.addBodyPart(messageBodyPart);
            // 添加到正文中
            message.setContent(multipart);
        }
        // 保存设置
        message.saveChanges();
        // 发送邮件
        Transport.send(message);
    } catch (Exception e) {
        Logs.error(e);
    }
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) MimeMultipart(javax.mail.internet.MimeMultipart) Multipart(javax.mail.Multipart) URLDataSource(javax.activation.URLDataSource) Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) DataHandler(javax.activation.DataHandler) Properties(java.util.Properties) URL(java.net.URL) FileDataSource(javax.activation.FileDataSource) URLDataSource(javax.activation.URLDataSource) DataSource(javax.activation.DataSource) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) FileDataSource(javax.activation.FileDataSource) MimeBodyPart(javax.mail.internet.MimeBodyPart) Session(javax.mail.Session)

Aggregations

URLDataSource (javax.activation.URLDataSource)21 DataHandler (javax.activation.DataHandler)14 MimeBodyPart (javax.mail.internet.MimeBodyPart)11 URL (java.net.URL)9 DataSource (javax.activation.DataSource)9 MimeMultipart (javax.mail.internet.MimeMultipart)8 IOException (java.io.IOException)6 Multipart (javax.mail.Multipart)6 FileDataSource (javax.activation.FileDataSource)5 MimeMessage (javax.mail.internet.MimeMessage)5 InputStream (java.io.InputStream)4 Properties (java.util.Properties)4 Message (javax.mail.Message)4 Session (javax.mail.Session)4 InternetAddress (javax.mail.internet.InternetAddress)4 File (java.io.File)3 MessagingException (javax.mail.MessagingException)3 Date (java.util.Date)2 BodyPart (javax.mail.BodyPart)2 QName (javax.xml.namespace.QName)2