/** * Safe a mail to a file using a more or less unique file name. * * @param email email * @throws IOException writing the email failed * @throws MessagingException writing the email failed */ protected void saveEmailToFile(final WiserMessage email) throws IOException, MessagingException { final int currCounter = fileNameCounter++ % 10; final String emailFileName = "email" + new Date().getTime() + "-" + currCounter + ".eml"; final File emailFile = new File(emailOutputDir, emailFileName); EmailUtils.writeMimeMessage(emailFile, email.getMimeMessage() ); }
/** * 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()); }
/** * 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 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 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());