Java Code Examples for javax.mail.Message#writeTo()

The following examples show how to use javax.mail.Message#writeTo() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LargeMessageTest.java    From greenmail with Apache License 2.0 6 votes vote down vote up
/**
 * Retrieve message from retriever and check the body content
 *
 * @param server Server to read from
 * @param to     Account to retrieve
 */
private void retrieveAndCheckBody(AbstractServer server, String to) throws MessagingException, IOException {
    try (Retriever retriever = new Retriever(server)) {
        Message[] messages = retriever.getMessages(to);
        assertEquals(1, messages.length);
        Message message = messages[0];
        assertTrue(message.getContentType().equalsIgnoreCase("application/blubb"));

        // Check content
        InputStream contentStream = (InputStream) message.getContent();
        byte[] bytes = IOUtils.toByteArray(contentStream);
        assertArrayEquals(createLargeByteArray(), bytes);

        // Dump complete mail message. This leads to a FETCH command without section or "len" specified.
        message.writeTo(new ByteArrayOutputStream());
    }
}
 
Example 2
Source File: TestUtils.java    From ApprovalTests.Java with Apache License 2.0 6 votes vote down vote up
public static void displayEmail(Message email)
{
  if (email == null)
  { return; }
  try
  {
    File f = File.createTempFile("email", ".eml");
    f.deleteOnExit();
    FileOutputStream out = new FileOutputStream(f);
    email.writeTo(out);
    out.close();
    displayFile(f.getAbsolutePath());
  }
  catch (Exception e)
  {
    throw ObjectUtils.throwAsError(e);
  }
}
 
Example 3
Source File: MIMEStream.java    From domino-jna with Apache License 2.0 5 votes vote down vote up
/**
 * Writes the MIME content of a {@link Message} to the stream
 * 
 * @param message message to append to the stream
 * @return this instance
 * @throws IOException in case of MIME stream I/O errors
 * @throws MessagingException in case of read errors from the {@link Message}
 */
public MIMEStream write(Message message) throws IOException, MessagingException {
	//size of in-memory buffer to transfer MIME data from Message object to Domino MIME stream
	final int BUFFERSIZE = 16384;
	
	final DisposableMemory buf = new DisposableMemory(BUFFERSIZE);
	
	message.writeTo(new OutputStream() {
		int bytesInBuffer = 0;

		@Override
		public void write(int b) throws IOException {
			buf.setByte(bytesInBuffer, (byte) (b & 0xff));
			bytesInBuffer++;
			if (bytesInBuffer == buf.size()) {
				flushBuffer();
			}
		}

		@Override
		public void close() throws IOException {
			flushBuffer();
		}

		private void flushBuffer() throws IOException {
			if (bytesInBuffer > 0) {
				int resultAsInt = NotesNativeAPI.get().MIMEStreamWrite(buf, bytesInBuffer, m_hMIMEStream);

				if (resultAsInt == NotesConstants.MIME_STREAM_IO) {
					throw new IOException("I/O error received during MIME stream operation");
				}

				bytesInBuffer = 0;
			}
		}
	});
	
	return this;
}
 
Example 4
Source File: Transport.java    From development with Apache License 2.0 5 votes vote down vote up
@Override
public void sendMessage(Message msg, Address[] addresses)
        throws MessagingException {
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        msg.writeTo(out);
        lastMail = new String(out.toByteArray(), "UTF-8");
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
}
 
Example 5
Source File: CommonTest.java    From hawkular-alerts with Apache License 2.0 5 votes vote down vote up
protected void writeEmailFile(Message msg, String fileName) throws Exception {
    File dir = new File("target/test-emails");
    if (!dir.exists()) {
        dir.mkdirs();
    }
    File file = new File(dir, fileName);
    FileOutputStream fos = new FileOutputStream(file);
    msg.writeTo(fos);
    fos.close();
}
 
Example 6
Source File: NoteDetailActivity.java    From ImapNote2 with GNU General Public License v3.0 5 votes vote down vote up
private void WriteMailToFile (String suid, Message message) {
    String directory = (ImapNotes2.getAppContext()).getFilesDir() + "/" +
            Listactivity.imapNotes2Account.GetAccountname();
    try {
        File outfile = new File (directory, suid);
        OutputStream str = new FileOutputStream(outfile);
        message.writeTo(str);
    }  catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
 
Example 7
Source File: SMTPMessageSender.java    From james-project with Apache License 2.0 4 votes vote down vote up
private String asString(Message message) throws IOException, MessagingException {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    message.writeTo(outputStream);
    return new String(outputStream.toByteArray(), StandardCharsets.UTF_8);
}
 
Example 8
Source File: FileMsgTransport.java    From javamail with Apache License 2.0 4 votes vote down vote up
@Override
protected void writeMessage(Message message, OutputStream os) throws IOException, MessagingException {
    message.writeTo(os);
}