Example usage for javax.activation DataHandler DataHandler

List of usage examples for javax.activation DataHandler DataHandler

Introduction

In this page you can find the example usage for javax.activation DataHandler DataHandler.

Prototype

public DataHandler(URL url) 

Source Link

Document

Create a DataHandler instance referencing a URL.

Usage

From source file:FirstStatMain.java

private void btnsendemailActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnsendemailActionPerformed
    final String senderemail = txtEmailfrom.getText();
    final String sendPass = txtPassword.getText();
    String send_to = txtEmailto.getText();
    String email_sub = txtemailsbj.getText();
    String email_body = tABody.getText();

    Properties prop = new Properties();
    prop.put("mail.smtp.host", "smtp.gmail.com");
    prop.put("mail.smtp.socketFactory.port", "465");
    prop.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    prop.put("mail.smtp.auth", "true");
    prop.put("mail.smtp.port", "465");

    Session session = Session.getDefaultInstance(prop, new javax.mail.Authenticator() {
        @Override//  w w w . ja  v  a 2s  . c  om
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(senderemail, sendPass);
        }
    });
    try {
        /* Message Header*/
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(senderemail));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(send_to));
        message.setSubject(email_sub);
        /*setting text message*/
        MimeBodyPart messageBodyPart = new MimeBodyPart();
        messageBodyPart.setText(email_body);
        Multipart multipart = new MimeMultipart();
        multipart.addBodyPart(messageBodyPart);
        /*attaching file*/
        messageBodyPart = new MimeBodyPart();
        DataSource source = new FileDataSource(file_path);
        messageBodyPart.setDataHandler(new DataHandler(source));
        messageBodyPart.setFileName(txtattachmentname.getText());
        multipart.addBodyPart(messageBodyPart);

        message.setContent(multipart);
        Transport.send(message);
        JOptionPane.showMessageDialog(rootPane, "Message sent");
    } catch (Exception e) {
        JOptionPane.showMessageDialog(rootPane, e);
    }
}

From source file:com.flexoodb.common.FlexUtils.java

/**
* use this method to obtain a mimemessage with the file wrapped in it.
*
* @param  filename filename of the file.
* @param  data the file in bytes.//w  w  w .  j a v a 2s  .  c  om
* @return a byte array of the mimemessage.
*/
static public byte[] wrapInMimeMessage(String filename, byte[] data) throws Exception {
    MimeMessage mimemessage = new MimeMessage(javax.mail.Session.getDefaultInstance(new Properties(), null));
    BodyPart messageBodyPart = new MimeBodyPart();
    Multipart multipart = new MimeMultipart();
    DataSource source = new ByteArrayDataSource(data, "application/octet-stream");
    messageBodyPart.setDataHandler(new DataHandler(source));
    messageBodyPart.setFileName(filename);
    multipart.addBodyPart(messageBodyPart);
    mimemessage.setContent(multipart);
    mimemessage.saveChanges();

    // finally pipe to an array so we can conver to string
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    mimemessage.writeTo(bos);
    return bos.toString().getBytes();
}

From source file:it.cnr.icar.eric.client.ui.thin.RegistryObjectCollectionBean.java

/**
* This method will upload file in registry for Extrensic Objects 
* 
*///w  w  w.  j a v a  2s  . co m
public String doUpload(File file, String contentType) {
    String status = "failure";
    RegistryObject ro = getCurrentRegistryObjectBean().getRegistryObject();
    if (ro instanceof ExtrinsicObject) {
        if (file == null) {
            append(WebUIResourceBundle.getInstance().getString("messgeFileUpload"));
            log.error(WebUIResourceBundle.getInstance()
                    .getString("message.NullFileObjectPassedToDoUploadMethod"));
        } else {
            try {
                DataHandler handler = new DataHandler(new FileDataSource(file));
                ((ExtrinsicObject) ro).setRepositoryItem(handler);
                if (contentType == null) {
                    log.error(WebUIResourceBundle.getInstance()
                            .getString("message.NullContentTypePassedToDoUploadMethod"));
                } else {
                    ((ExtrinsicObject) ro).setMimeType(contentType);
                }
                status = "success";
            } catch (JAXRException ex) {
                String errMsg = WebUIResourceBundle.getInstance().getString("errorUploadingFile");
                append(errMsg + " " + ex.getMessage());
                log.error(errMsg + " " + ex.getMessage());
            }
        }
    }
    return status;
}