if (EmailUtils.isEmpty(aContentType)) if (this.contentType.startsWith("text/") && EmailUtils.isNotEmpty(this.charset))
/** * Sets the email subject. Replaces end-of-line characters with spaces. * * @param aSubject A String. * @return An Email. * @since 1.0 */ public Email setSubject(final String aSubject) { this.subject = EmailUtils.replaceEndOfLineCharactersWithSpaces(aSubject); return this; }
EmailUtils.notNull(aSession, "no mail session supplied"); final String password = sessionProperties.getProperty(EmailConstants.MAIL_SMTP_PASSWORD); if (EmailUtils.isNotEmpty(userName) && EmailUtils.isNotEmpty(password))
/** * Gets the host name of the SMTP server, * * @return host name */ public String getHostName() { if (this.session != null) { return this.session.getProperty(EmailConstants.MAIL_HOST); } else if (EmailUtils.isNotEmpty(this.hostName)) { return this.hostName; } return null; }
/** * Adds a header ( name, value ) to the headers Map. * * @param name A String with the name. * @param value A String with the value. * @since 1.0 * @throws IllegalArgumentException if either {@code name} or {@code value} is null or empty */ public void addHeader(final String name, final String value) { if (EmailUtils.isEmpty(name)) { throw new IllegalArgumentException("name can not be null or empty"); } if (EmailUtils.isEmpty(value)) { throw new IllegalArgumentException("value can not be null or empty"); } this.headers.put(name, value); }
@Test public void testUrlEncoding() throws UnsupportedEncodingException { assertEquals("abcdefg", EmailUtils.encodeUrl("abcdefg")); assertEquals("0123456789", EmailUtils.encodeUrl("0123456789")); assertEquals("Test%20CID", EmailUtils.encodeUrl("Test CID")); assertEquals("joe.doe@apache.org", EmailUtils.encodeUrl("joe.doe@apache.org")); assertEquals("joe+doe@apache.org", EmailUtils.encodeUrl("joe+doe@apache.org")); assertEquals("peter%26paul%26mary@oldmusic.org", EmailUtils.encodeUrl("peter&paul&mary@oldmusic.org")); } }
/** * Embeds a file in the HTML. This implementation delegates to * {@link #embed(File, String)}. * * @param file The <code>File</code> object to embed * @return A String with the Content-ID of the file. * @throws EmailException when the supplied <code>File</code> cannot be * used; also see {@link javax.mail.internet.MimeBodyPart} for definitions * * @see #embed(File, String) * @since 1.1 */ public String embed(final File file) throws EmailException { final String cid = EmailUtils.randomAlphabetic(HtmlEmail.CID_LENGTH).toLowerCase(Locale.ENGLISH); return embed(file, cid); }
/** * Creates a random string whose length is the number of characters specified. * * <p> * Characters will be chosen from the set of alphabetic characters. * </p> * * @param count the length of random string to create * * @return the random string * * @since Commons Lang v2.1, svn 201930 */ static String randomAlphabetic(final int count) { return random(count, 0, 0, true, false, null, RANDOM); }
/** * Sends the previously created MimeMessage to the SMTP server. * * @return the message id of the underlying MimeMessage * @throws IllegalArgumentException if the MimeMessage has not been created * @throws EmailException the sending failed */ public String sendMimeMessage() throws EmailException { EmailUtils.notNull(this.message, "MimeMessage has not been created yet"); try { Transport.send(this.message); return this.message.getMessageID(); } catch (final Throwable t) { final String msg = "Sending the email to the following server failed : " + this.getHostName() + ":" + this.getSmtpPort(); throw new EmailException(msg, t); } }
/** * 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() ); }
/** * Returns the current SSL port used by the SMTP transport. * * @return the current SSL port used by the SMTP transport */ public String getSslSmtpPort() { if (this.session != null) { return this.session.getProperty(EmailConstants.MAIL_SMTP_SOCKET_FACTORY_PORT); } else if (EmailUtils.isNotEmpty(this.sslSmtpPort)) { return this.sslSmtpPort; } return null; }
/** * Set the text content. * * @param aText A String. * @return An HtmlEmail. * @throws EmailException see javax.mail.internet.MimeBodyPart * for definitions * @since 1.0 */ public HtmlEmail setTextMsg(final String aText) throws EmailException { if (EmailUtils.isEmpty(aText)) { throw new EmailException("Invalid message supplied"); } this.text = aText; return this; }
EmailUtils.notNull(aSession, "no mail session supplied"); final String password = sessionProperties.getProperty(EmailConstants.MAIL_SMTP_PASSWORD); if (EmailUtils.isNotEmpty(userName) && EmailUtils.isNotEmpty(password))
/** * Test that the specified Content-ID is used when embedding a File * object in an HtmlEmail. * * Rolled back the changes since they broke real emails therefore * the test is currently disabled. * * see https://issues.apache.org/jira/browse/EMAIL-101 */ @Test public void testEmbedFileWithCID() throws Exception { // ==================================================================== // Test Success // ==================================================================== final File file = File.createTempFile("testEmbedFile", "txt"); file.deleteOnExit(); final String testCid = "Test CID"; final String encodedCid = EmailUtils.encodeUrl(testCid); // if we embed a new file, do we get the content ID we specified back? final String strEmbed = this.email.embed(file, testCid); assertNotNull(strEmbed); assertEquals("didn't get same CID when embedding with a specified CID", encodedCid, strEmbed); // if we embed the same file again, do we get the same content ID // back? final String returnedCid = this.email.embed(file); assertEquals("didn't get same CID after embedding same file twice", encodedCid, returnedCid); }
final String cid = EmailUtils.randomAlphabetic(HtmlEmail.CID_LENGTH).toLowerCase(); return embed(dataSource, name, cid);
/** * Creates a random string whose length is the number of characters specified. * * <p> * Characters will be chosen from the set of alphabetic characters. * </p> * * @param count the length of random string to create * * @return the random string * * @since Commons Lang v2.1, svn 201930 */ static String randomAlphabetic(final int count) { return random(count, 0, 0, true, false, null, RANDOM); }
/** * Sends the previously created MimeMessage to the SMTP server. * * @return the message id of the underlying MimeMessage * @throws IllegalArgumentException if the MimeMessage has not been created * @throws EmailException the sending failed */ public String sendMimeMessage() throws EmailException { EmailUtils.notNull(this.message, "MimeMessage has not been created yet"); try { Transport.send(this.message); return this.message.getMessageID(); } catch (final Throwable t) { final String msg = "Sending the email to the following server failed : " + this.getHostName() + ":" + this.getSmtpPort(); throw new EmailException(msg, t); } }
/** * 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()); }
if (EmailUtils.isEmpty(msg)) if (primary instanceof MimePart && EmailUtils.isNotEmpty(charset))