Example usage for javax.mail.internet MimeMessage writeTo

List of usage examples for javax.mail.internet MimeMessage writeTo

Introduction

In this page you can find the example usage for javax.mail.internet MimeMessage writeTo.

Prototype

@Override
public void writeTo(OutputStream os) throws IOException, MessagingException 

Source Link

Document

Output the message as an RFC 822 format stream.

Usage

From source file:org.yccheok.jstock.alert.GoogleMail.java

private static Message createMessageWithEmail(MimeMessage email) throws MessagingException, IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    email.writeTo(baos);
    String encodedEmail = Base64.encodeBase64URLSafeString(baos.toByteArray());
    Message message = new Message();
    message.setRaw(encodedEmail);//from   www . j  a v  a 2 s  . c  o m
    return message;
}

From source file:gt.dakaik.common.Common.java

public static Message createMessageWithEmail(MimeMessage email) throws MessagingException, IOException {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    email.writeTo(bytes);
    String encodedEmail = Base64.encodeBase64URLSafeString(bytes.toByteArray());
    Message message = new Message();
    message.setRaw(encodedEmail);// www .  j  a  v  a 2s  .  c o  m
    return message;
}

From source file:org.oscarehr.util.EmailUtils.java

/**
 * This method is like a toString for Email objects.
 *///from  www.j a  v  a 2s  .c o  m
public static String getAsString(MimeMessage mimeMessage) throws IOException, MessagingException {
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    mimeMessage.writeTo(os);
    return (os.toString());
}

From source file:com.openkm.util.impexp.RepositoryExporter.java

/**
 * Export mail from OpenKM repository to filesystem.
 *///w ww . j  a  va  2  s. co m
public static ImpExpStats exportMail(String token, String mailPath, String destPath, boolean metadata,
        Writer out, InfoDecorator deco) throws PathNotFoundException, RepositoryException, DatabaseException,
        IOException, AccessDeniedException, ParseException, NoSuchGroupException, MessagingException {
    MailModule mm = ModuleManager.getMailModule();
    MetadataAdapter ma = MetadataAdapter.getInstance(token);
    Mail mailChild = mm.getProperties(token, mailPath);
    Gson gson = new Gson();
    ImpExpStats stats = new ImpExpStats();
    MimeMessage msg = MailUtils.create(token, mailChild);
    FileOutputStream fos = new FileOutputStream(destPath);
    msg.writeTo(fos);
    IOUtils.closeQuietly(fos);
    FileLogger.info(BASE_NAME, "Created document ''{0}''", mailChild.getPath());

    // Metadata
    if (metadata) {
        MailMetadata mmd = ma.getMetadata(mailChild);
        String json = gson.toJson(mmd);
        fos = new FileOutputStream(destPath + Config.EXPORT_METADATA_EXT);
        IOUtils.write(json, fos);
        IOUtils.closeQuietly(fos);
    }

    if (out != null) {
        out.write(deco.print(mailChild.getPath(), mailChild.getSize(), null));
        out.flush();
    }

    // Stats
    stats.setSize(stats.getSize() + mailChild.getSize());
    stats.setMails(stats.getMails() + 1);

    return stats;
}

From source file:mitm.common.mail.MailUtils.java

/**
 * Checks whether the message can be converted to RFC2822 raw message source. Messages that
 * contain unsupported encoding types, corrupts content transfer encoding etc. will result
 * in a MessagingException or IOException.
 * @param message/*ww  w . j  a  v a  2  s . c  o m*/
 * @throws MessagingException
 * @throws IOException
 */
public static void validateMessage(MimeMessage message) throws MessagingException, IOException {
    OutputStream bitsink = new OutputStream() {
        @Override
        public void write(int ignore) {
        }
    };

    message.writeTo(bitsink);
}

From source file:com.ikon.util.impexp.RepositoryExporter.java

/**
 * Export mail from openkm repository to filesystem.
 *///  www.j  ava2  s.co  m
public static ImpExpStats exportMail(String token, String mailPath, String destPath, String metadata,
        Writer out, InfoDecorator deco) throws PathNotFoundException, RepositoryException, DatabaseException,
        IOException, AccessDeniedException, ParseException, NoSuchGroupException, MessagingException {
    MailModule mm = ModuleManager.getMailModule();
    MetadataAdapter ma = MetadataAdapter.getInstance(token);
    Mail mailChild = mm.getProperties(token, mailPath);
    Gson gson = new Gson();
    ImpExpStats stats = new ImpExpStats();
    MimeMessage msg = MailUtils.create(token, mailChild);
    FileOutputStream fos = new FileOutputStream(destPath);
    msg.writeTo(fos);
    IOUtils.closeQuietly(fos);
    FileLogger.info(BASE_NAME, "Created document ''{0}''", mailChild.getPath());

    // Metadata
    if (metadata.equals("JSON")) {
        MailMetadata mmd = ma.getMetadata(mailChild);
        String json = gson.toJson(mmd);
        fos = new FileOutputStream(destPath + Config.EXPORT_METADATA_EXT);
        IOUtils.write(json, fos);
        IOUtils.closeQuietly(fos);
    } else if (metadata.equals("XML")) {
        fos = new FileOutputStream(destPath + ".xml");

        MailMetadata mmd = ma.getMetadata(mailChild);
        JAXBContext jaxbContext;
        try {
            jaxbContext = JAXBContext.newInstance(MailMetadata.class);
            Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

            // output pretty printed
            jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            jaxbMarshaller.marshal(mmd, fos);
        } catch (JAXBException e) {
            log.error(e.getMessage(), e);
            FileLogger.error(BASE_NAME, "XMLException ''{0}''", e.getMessage());
        }
    }

    if (out != null) {
        out.write(deco.print(mailChild.getPath(), mailChild.getSize(), null));
        out.flush();
    }

    // Stats
    stats.setSize(stats.getSize() + mailChild.getSize());
    stats.setMails(stats.getMails() + 1);

    return stats;
}

From source file:javamailclient.GmailAPI.java

/**
 * Create a Message from an email/*from w  w  w. j  av  a2s .co  m*/
 *
 * @param email Email to be set to raw of message
 * @return Message containing base64url encoded email.
 * @throws IOException
 * @throws MessagingException
 */
public static Message createMessageWithEmail(MimeMessage email) throws MessagingException, IOException {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    email.writeTo(bytes);
    String encodedEmail = Base64.encodeBase64URLSafeString(bytes.toByteArray());
    Message message = new Message();
    message.setRaw(encodedEmail);
    return message;
}

From source file:org.apache.james.server.core.MimeMessageUtil.java

/**
 * Calculate the size of the give mimeMessage
 * /* w w w . j a  va  2  s. c om*/
 * @param message
 *            the MimeMessage
 * @return size the calculated size
 * @throws MessagingException
 *             if a problem occours while calculate the message size
 */
public static long calculateMessageSize(MimeMessage message) throws MessagingException {
    long size;
    // SK: Should probably eventually store this as a locally
    // maintained value (so we don't have to load and reparse
    // messages each time).
    size = message.getSize();
    if (size != -1) {
        Enumeration<String> e = message.getAllHeaderLines();
        if (e.hasMoreElements()) {
            size += 2;
        }
        while (e.hasMoreElements()) {
            // add 2 bytes for the CRLF
            size += e.nextElement().length() + 2;
        }
    }

    if (size == -1) {
        SizeCalculatorOutputStream out = new SizeCalculatorOutputStream();
        try {
            message.writeTo(out);
        } catch (IOException e) {
            // should never happen as SizeCalculator does not actually throw
            // IOExceptions.
            throw new MessagingException("IOException wrapped by getMessageSize", e);
        }
        size = out.getSize();
    }
    return size;
}

From source file:org.obm.sync.server.mailer.EventChangeMailerTest.java

private static String getRawMessage(MimeMessage actualMessage)
        throws IOException, MessagingException, UnsupportedEncodingException {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    actualMessage.writeTo(output);
    String rawMessage = new String(output.toByteArray(), Charsets.UTF_8.displayName());
    return rawMessage;
}

From source file:org.apache.james.core.MimeMessageUtil.java

/**
 * Calculate the size of the give mimeMessage
 * /*from  ww w  . j  a  v  a 2 s.c o m*/
 * @param message
 *            the MimeMessage
 * @return size the calculated size
 * @throws MessagingException
 *             if a problem occours while calculate the message size
 */
public static long calculateMessageSize(MimeMessage message) throws MessagingException {
    long size;
    // SK: Should probably eventually store this as a locally
    // maintained value (so we don't have to load and reparse
    // messages each time).
    size = message.getSize();
    if (size != -1) {
        Enumeration<?> e = message.getAllHeaderLines();
        if (e.hasMoreElements()) {
            size += 2;
        }
        while (e.hasMoreElements()) {
            // add 2 bytes for the CRLF
            size += ((String) e.nextElement()).length() + 2;
        }
    }

    if (size == -1) {
        SizeCalculatorOutputStream out = new SizeCalculatorOutputStream();
        try {
            message.writeTo(out);
        } catch (IOException e) {
            // should never happen as SizeCalculator does not actually throw
            // IOExceptions.
            throw new MessagingException("IOException wrapped by getMessageSize", e);
        }
        size = out.getSize();
    }
    return size;
}