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.apache.nifi.processors.email.GenerateAttachment.java

public byte[] SimpleEmail() {
    Email email = new SimpleEmail();
    try {//w w  w .j  a  v a 2 s  .  c o  m
        email.setFrom(from);
        email.addTo(to);
        email.setSubject(subject);
        email.setMsg(message);
        email.setHostName(hostName);
        email.buildMimeMessage();
    } catch (EmailException e) {
        e.printStackTrace();
    }

    ByteArrayOutputStream output = new ByteArrayOutputStream();
    MimeMessage mimeMessage = email.getMimeMessage();
    try {
        mimeMessage.writeTo(output);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (MessagingException e) {
        e.printStackTrace();
    }

    return output.toByteArray();
}

From source file:org.apache.nifi.processors.email.GenerateAttachment.java

public byte[] WithAttachments(int amount) {
    MultiPartEmail email = new MultiPartEmail();
    try {/* ww  w  .  ja  v a2 s .com*/

        email.setFrom(from);
        email.addTo(to);
        email.setSubject(subject);
        email.setMsg(message);
        email.setHostName(hostName);

        int x = 1;
        while (x <= amount) {
            // Create an attachment with the pom.xml being used to compile (yay!!!)
            EmailAttachment attachment = new EmailAttachment();
            attachment.setPath("pom.xml");
            attachment.setDisposition(EmailAttachment.ATTACHMENT);
            attachment.setDescription("pom.xml");
            attachment.setName("pom.xml" + String.valueOf(x));
            //  attach
            email.attach(attachment);
            x++;
        }
        email.buildMimeMessage();
    } catch (EmailException e) {
        e.printStackTrace();
    }
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    MimeMessage mimeMessage = email.getMimeMessage();
    try {
        mimeMessage.writeTo(output);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (MessagingException e) {
        e.printStackTrace();
    }

    return output.toByteArray();
}

From source file:edu.unc.lib.dl.util.MockJavaMailSender.java

private void logit(MimeMessage msg) {
    log.debug("Dumping message content to standard out.");
    try {/*ww  w .j a v  a  2  s.c  om*/
        msg.writeTo(System.out);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (MessagingException e) {
        e.printStackTrace();
    }
}

From source file:AmazonSESSample.java

private static RawMessage getRawMessage() throws MessagingException, IOException {
    // JavaMail representation of the message
    Session s = Session.getInstance(new Properties(), null);
    s.setDebug(true);/*from  w ww  .  j  a v  a 2  s.  c  om*/
    MimeMessage msg = new MimeMessage(s);

    // Sender and recipient
    msg.setFrom(new InternetAddress("aravind@gofastpay.com"));
    InternetAddress[] address = { new InternetAddress("aravind@gofastpay.com") };
    msg.setRecipients(javax.mail.Message.RecipientType.TO, address);
    msg.setSentDate(new Date());
    // Subject
    msg.setSubject(SUBJECT);

    // Add a MIME part to the message
    //MimeMultipart mp = new MimeMultipart();
    Multipart mp = new MimeMultipart();
    MimeBodyPart mimeBodyPart = new MimeBodyPart();
    //mimeBodyPart.setText(BODY);

    //BodyPart part = new MimeBodyPart();
    //String myText = BODY;
    //part.setContent(URLEncoder.encode(myText, "US-ASCII"), "text/html");
    //part.setText(BODY);
    //mp.addBodyPart(part);
    //msg.setContent(mp);
    mimeBodyPart.setContent(BODY, "text/html");
    mp.addBodyPart(mimeBodyPart);
    msg.setContent(mp);

    // Print the raw email content on the console
    //PrintStream out = System.out;
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    msg.writeTo(out);
    //String rawString = out.toString();
    //byte[] bytes = IOUtils.toByteArray(msg.getInputStream());
    //ByteBuffer byteBuffer = ByteBuffer.allocate(bytes.length);
    //ByteBuffer byteBuffer = ByteBuffer.wrap(Base64.getEncoder().encode(rawString.getBytes()));

    //byteBuffer.put(bytes);
    //byteBuffer.put(Base64.getEncoder().encode(bytes));
    RawMessage rawMessage = new RawMessage(ByteBuffer.wrap(out.toByteArray()));
    return rawMessage;
}

From source file:fr.aliacom.obm.common.calendar.MailSendTest.java

private String writeEventMail(EventMail eventMail) throws IOException, MessagingException {
    MimeMessage mail = eventMail.buildMimeMail(Session.getDefaultInstance(new Properties()));
    ByteArrayOutputStream mailByteStream = new ByteArrayOutputStream();

    mail.writeTo(mailByteStream);

    return new String(mailByteStream.toByteArray(), CharsetNames.CS_UTF8);
}

From source file:EmailNotificationTest.java

public void setMailMock() {
    // setup mail sender mock invocations
    reset(this.javaMailSender);
    when(this.javaMailSender.createMimeMessage()).thenCallRealMethod();

    Answer dumpMessage = new Answer() {
        @Override//from   w w  w .j a v a  2 s. co m
        public Object answer(InvocationOnMock invocation) throws Throwable {
            Object arg = invocation.getArguments()[0];
            if (arg instanceof MimeMessage) {
                MimeMessage m = (MimeMessage) arg;
                System.out.println("EMAIL DUMP:");
                m.writeTo(System.out);
            } else if (arg instanceof SimpleMailMessage) {
                SimpleMailMessage m = (SimpleMailMessage) arg;
                System.out.println("EMAIL DUMP:");
                System.out.println(m.toString());
            } else {
                throw new Error("Could not print email: " + arg);
            }
            return null;
        }
    };
    doAnswer(dumpMessage).when(this.javaMailSender).send(any(MimeMessage.class));
    doAnswer(dumpMessage).when(this.javaMailSender).send(any(SimpleMailMessage.class));
}

From source file:org.apache.axis2.transport.mail.server.POP3Worker.java

private void processInput(String input, PrintWriter printWriter) {
    byte[] CR_LF_DOT_CR_LF = new byte[] { 0x0D, 0x0A, '.', 0x0D, 0x0A };
    String user = "";

    if (input == null) {
        this.doneProcess = true; // This should not be happening
    } else {/*from  ww w  . j a  v  a 2s  .c o  m*/
        ArrayList tokens = new ArrayList();
        StringTokenizer stk = new StringTokenizer(input);

        while (stk.hasMoreTokens()) {
            tokens.add(stk.nextToken());
        }

        if (tokens.get(0).equals(Constants.USER)) {
            user = (String) tokens.get(1);
            messages = st.popUserMails(user);
            printWriter.println(Constants.OK);
        } else if (tokens.get(0).equals(Constants.PASS)) {
            printWriter.println(Constants.OK); // Passwords are not checked.
        } else if (input.equals(Constants.QUIT)) {
            printWriter.println(Constants.OK + "POP3 server signing off");
            doneProcess = true;
        } else if (input.equals(Constants.STAT)) {
            printWriter.println(Constants.OK + messages.size() + " 1"); // We take the maildrop size as one.
        } else if (tokens.get(0).equals(Constants.LIST)) { // scan listing
            if (tokens.size() > 1) {
                try {
                    int optArg = Integer.parseInt((String) tokens.get(1));
                    int messageArrayIndex = optArg - 1;

                    if ((messageArrayIndex < messages.size()) && (messageArrayIndex >= 0)) { // that is OK careful with numbering
                        printWriter.println(Constants.OK + messageArrayIndex + 1 + " 120"); // Mail size of 120 is just some number.
                    } else {
                        printWriter.println(Constants.ERR + "no such message, only " + (messages.size() + 1)
                                + " messages in maildrop");
                    }
                } catch (NumberFormatException e) {
                    log.info(e.getMessage());
                    printWriter.println(
                            Constants.ERR + "problem passing the index. Index submited was " + tokens.get(1));
                }
            } else {
                printWriter.println(Constants.OK + messages.size());

                for (int i = 0; i < messages.size(); i++) {
                    int messageIndex = i + 1;

                    printWriter.println(messageIndex + " 120"); // List out all the messages with a message size octet of 120
                }

                printWriter.println(".");
            }
        } else if (tokens.get(0).equals(Constants.RETR)) {
            String i = (String) tokens.get(1);

            try {
                int index = Integer.parseInt(i);

                printWriter.println(Constants.OK);

                MimeMessage m = (MimeMessage) messages.get(index - 1);

                m.writeTo(socket.getOutputStream());

                socket.getOutputStream().write(CR_LF_DOT_CR_LF); // This is a bit of a hack to get it working. Have to find a bette way to handle this.
                socket.getOutputStream().flush();
            } catch (NumberFormatException e) {
                printWriter.println(Constants.ERR);
            } catch (IOException e1) {
                printWriter.println(Constants.ERR);
            } catch (MessagingException e2) {
                printWriter.println(Constants.ERR);
            }
        } else if (tokens.get(0).equals(Constants.DELE)) {
            String smIndex = (String) tokens.get(1);

            try {
                int mIndex = Integer.parseInt(smIndex) - 1 - numDeleted; // When one mail is deleted the index of the other mails will reduce. Asumed that the delete will occure from bottom up.

                if ((mIndex >= 0) && (mIndex < messages.size())) {
                    messages.remove(mIndex);
                    numDeleted++;
                    printWriter.println(Constants.OK);
                } else {
                    printWriter.println(Constants.ERR);
                }
            } catch (NumberFormatException e) {
                printWriter.println(Constants.ERR);
            }
        } else if (tokens.get(0).equals(Constants.NOOP) || tokens.get(0).equals(Constants.RSET)) {
            printWriter.println(Constants.OK);
        } else {
            printWriter.println(Constants.ERR);
        }
    }
}

From source file:org.apache.james.transport.mailets.SerialiseToHTTP.java

private String getSerialisedMessage(MimeMessage message) throws IOException, MessagingException {
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    message.writeTo(os);
    return os.toString();
}

From source file:com.ikon.webdav.resource.MailResource.java

@Override
public void sendContent(OutputStream out, Range range, Map<String, String> params, String contentType)
        throws IOException, NotAuthorizedException, BadRequestException {
    log.debug("sendContent({}, {})", params, contentType);

    try {/*w w  w.ja  v  a  2  s.  c  om*/
        String fixedMailPath = ResourceUtils.fixRepositoryPath(mail.getPath());
        Mail mail = OKMMail.getInstance().getProperties(null, fixedMailPath);

        if (mail.getAttachments().isEmpty()) {
            IOUtils.write(mail.getContent(), out);
        } else {
            MimeMessage m = MailUtils.create(null, mail);
            m.writeTo(out);
            out.flush();
        }
    } catch (PathNotFoundException e) {
        log.error("PathNotFoundException: " + e.getMessage(), e);
        throw new RuntimeException("Failed to update content: " + mail.getPath());
    } catch (AccessDeniedException e) {
        log.error("AccessDeniedException: " + e.getMessage(), e);
        throw new RuntimeException("Failed to update content: " + mail.getPath());
    } catch (RepositoryException e) {
        log.error("RepositoryException: " + e.getMessage(), e);
        throw new RuntimeException("Failed to update content: " + mail.getPath());
    } catch (DatabaseException e) {
        log.error("DatabaseException: " + e.getMessage(), e);
        throw new RuntimeException("Failed to update content: " + mail.getPath());
    } catch (MessagingException e) {
        log.error("MessagingException: " + e.getMessage(), e);
        throw new RuntimeException("Failed to update content: " + mail.getPath());
    }
}

From source file:org.apache.camel.component.google.mail.GmailUsersMessagesIntegrationTest.java

private 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  ww  w.j  a  v  a  2 s  . com*/
    return message;
}