RawMessage mail2Content(Email email) throws IOException, MessagingException, EmailException { email.buildMimeMessage(); ByteArrayOutputStream out = new ByteArrayOutputStream(); email.getMimeMessage().writeTo(out); return new RawMessage().withData(ByteBuffer.wrap(out.toByteArray())); }
MimeMessage msg = email.getMimeMessage(); msg.saveChanges();
Properties props = new Properties(); props.setProperty("mail.transport.protocol", "smtp"); Session mailSession = Session.getDefaultInstance(props, null); Transport transport = mailSession.getTransport("smtp"); transport.connect("localhost", 25, null, null); Email email = new SimpleEmail(); email.setFrom("example@example.com"); email.addTo("example@example.com"); email.setSubject("Hello Example"); email.setMsg("Hello Example"); email.setHostName("localhost"); // buildMimeMessage call below freaks out without this // dug into the internals of commons email // basically send() is buildMimeMessage() + Transport.send(message) // so rather than using Transport, reuse the one that I already have email.buildMimeMessage(); Message m = email.getMimeMessage(); transport.sendMessage(m, m.getAllRecipients());
/** * Test sending a image HTML mail bases on a local HTML page and local image. * * @throws Exception the test failed */ @Test public void testImageHtmlEmailLocal() throws Exception { // use a simple HTML page with one image final File htmlFile = new File("./src/test/resources/html/www.apache.org.html"); final String htmlMsg1 = FileUtils.readFileToString(htmlFile, "ISO-8859-1"); final ImageHtmlEmail email = (ImageHtmlEmail) create(ImageHtmlEmail.class); email.setDataSourceResolver(new DataSourceUrlResolver(htmlFile.getParentFile().toURI().toURL(), false)); email.setSubject("[testImageHtmlEmail] 1.Test: simple html content"); email.setHtmlMsg(htmlMsg1); EmailUtils.writeMimeMessage( new File("./target/test-emails/testImageHtmlEmailLocal.eml"), send(email).getMimeMessage()); }
try email.getMimeMessage().writeTo(bout); log.debug("Sent email:\n" + new String(bout.toByteArray()));
/** * A sanity check that a simple email also works in reality. * * @throws Exception the test failed */ @Test public void testMultiPartEmail() throws Exception { final MultiPartEmail email = (MultiPartEmail) create(MultiPartEmail.class); email.setSubject("TestMultiPartMail"); email.setMsg("This is a test mail ... :-)"); email.attach(new File("./src/test/resources/attachments/logo.pdf")); EmailUtils.writeMimeMessage( new File("./target/test-emails/multipart.eml"), send(email).getMimeMessage()); }
/** * A sanity check that a simple email also works in reality. * * @throws Exception the test failed */ @Test public void testSimpleEmail() throws Exception { final SimpleEmail email = (SimpleEmail) create(SimpleEmail.class); email.setSubject("TestSimpleMail"); email.setMsg("This is a test mail ... :-)"); EmailUtils.writeMimeMessage( new File("./target/test-emails/simplemail.eml"), send(email).getMimeMessage()); }
/** * A sanity check that a header folding works correctly. * * @throws Exception the test failed */ @Test public void testFoldedHeaderValue() throws Exception { final SimpleEmail email = (SimpleEmail) create(SimpleEmail.class); email.setSubject("TestFoldedHeaderMail"); email.setMsg("This is a test mail with a folded header value... :-)"); email.addHeader("X-TestHeader", "This is a very long header value which should be folded into two lines, hopefully"); EmailUtils.writeMimeMessage( new File("./target/test-emails/foldedheader.eml"), send(email).getMimeMessage()); }
/** * This test checks the correct character encoding when sending * non-ASCII content using SimpleEmail. * * https://issues.apache.org/jira/browse/EMAIL-79 * * @throws Exception the test failed */ @Test public void testCorrectCharacterEncoding() throws Exception { // U+03B1 : GREEK SMALL LETTER ALPHA // U+03B2 : GREEK SMALL LETTER BETA // U+03B3 : GREEK SMALL LETTER GAMMA final String subject = "[email] 5.Test: Subject with three greek UTF-8 characters : \u03B1\u03B2\u03B3"; final String textMsg = "My test body with with three greek UTF-8 characters : \u03B1\u03B2\u03B3\n"; final String attachmentName = "\u03B1\u03B2\u03B3.txt"; // make sure to set the charset before adding the message content final MultiPartEmail email = (MultiPartEmail) create(MultiPartEmail.class); email.setSubject(subject); email.setMsg(textMsg); // create a proper UTF-8 sequence for the text attachment (matching our default charset) final DataSource attachment = new javax.mail.util.ByteArrayDataSource(textMsg.getBytes("utf-8"), "text/plain"); email.attach(attachment, attachmentName, "Attachment in Greek"); EmailUtils.writeMimeMessage( new File("./target/test-emails/correct-encoding.eml"), send(email).getMimeMessage()); }
/** * Testing if we are able to send a partial email with an invalid address. * * https://issues.apache.org/jira/browse/EMAIL-132 * * @throws Exception the test failed. */ @Test public void testPartialSend() throws Exception { final SimpleEmail email = (SimpleEmail) create(SimpleEmail.class); email.addTo(EmailConfiguration.TEST_TO); email.addTo("nobody@is.invalid"); email.setSubject("TestPartialMail"); email.setMsg("This is a test mail ... :-)"); email.setSendPartial(true); EmailUtils.writeMimeMessage( new File("./target/test-emails/partialmail.eml"), send(email).getMimeMessage()); }
htmlEmail1.setHtmlMsg(htmlMsg); EmailUtils.writeMimeMessage( new File("./target/test-emails/htmlemail1.eml"), send(htmlEmail1).getMimeMessage()); htmlEmail2.attach(url, "Apache Logo", "The official Apache logo" ); EmailUtils.writeMimeMessage( new File("./target/test-emails/htmlemail2.eml"), send(htmlEmail2).getMimeMessage()); htmlEmail3.setHtmlMsg(htmlMsg); EmailUtils.writeMimeMessage( new File("./target/test-emails/htmlemail3.eml"), send(htmlEmail3).getMimeMessage()); htmlEmail4.attach(attachment); EmailUtils.writeMimeMessage( new File("./target/test-emails/htmlemail4.eml"), send(htmlEmail4).getMimeMessage());