Search in sources :

Example 41 with JavaMailSenderImpl

use of org.springframework.mail.javamail.JavaMailSenderImpl in project PublicCMS-preview by sanluan.

the class SendTask method getMailSender.

/**
 * @param siteId
 * @param config
 * @return mail sender
 */
public synchronized JavaMailSender getMailSender(short siteId, Map<String, String> config) {
    JavaMailSenderImpl javaMailSender = cache.get(siteId);
    if (null == javaMailSender) {
        javaMailSender = new JavaMailSenderImpl();
        javaMailSender.setDefaultEncoding(config.get(CONFIG_DEFAULTENCODING));
        javaMailSender.setHost(config.get(CONFIG_HOST));
        javaMailSender.setPort(Integer.parseInt(config.get(CONFIG_PORT)));
        javaMailSender.setUsername(config.get(CONFIG_USERNAME));
        javaMailSender.setPassword(config.get(CONFIG_PASSWORD));
        Properties properties = new Properties();
        properties.setProperty("mail.smtp.auth", config.get(CONFIG_AUTH));
        properties.setProperty("mail.smtp.timeout", config.get(CONFIG_TIMEOUT));
        javaMailSender.setJavaMailProperties(properties);
        cache.put(siteId, javaMailSender);
    }
    return javaMailSender;
}
Also used : JavaMailSenderImpl(org.springframework.mail.javamail.JavaMailSenderImpl) Properties(java.util.Properties)

Example 42 with JavaMailSenderImpl

use of org.springframework.mail.javamail.JavaMailSenderImpl in project spring-integration-samples by spring-projects.

the class MimeMessageParsingTest method testProcessingOfEmailAttachments.

/**
 * This test will create a Mime Message that contains an Attachment, send it
 * to an SMTP Server (Using Wiser) and retrieve and process the Mime Message.
 *
 * This test verifies that the parsing of the retrieved Mime Message is
 * successful and that the correct number of {@link EmailFragment}s is created.
 */
@Test
public void testProcessingOfEmailAttachments() {
    final JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
    mailSender.setPort(this.wiserPort);
    final MimeMessage message = mailSender.createMimeMessage();
    final String pictureName = "picture.png";
    final ByteArrayResource byteArrayResource = getFileData(pictureName);
    try {
        final MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom("testfrom@springintegration.org");
        helper.setTo("testto@springintegration.org");
        helper.setSubject("Parsing of Attachments");
        helper.setText("Spring Integration Rocks!");
        helper.addAttachment(pictureName, byteArrayResource, "image/png");
    } catch (MessagingException e) {
        throw new MailParseException(e);
    }
    mailSender.send(message);
    final List<WiserMessage> wiserMessages = wiser.getMessages();
    Assert.assertTrue(wiserMessages.size() == 1);
    boolean foundTextMessage = false;
    boolean foundPicture = false;
    for (WiserMessage wiserMessage : wiserMessages) {
        final List<EmailFragment> emailFragments = new ArrayList<EmailFragment>();
        try {
            final MimeMessage mailMessage = wiserMessage.getMimeMessage();
            EmailParserUtils.handleMessage(null, mailMessage, emailFragments);
        } catch (MessagingException e) {
            throw new IllegalStateException("Error while retrieving Mime Message.");
        }
        Assert.assertTrue(emailFragments.size() == 2);
        for (EmailFragment emailFragment : emailFragments) {
            if ("picture.png".equals(emailFragment.getFilename())) {
                foundPicture = true;
            }
            if ("message.txt".equals(emailFragment.getFilename())) {
                foundTextMessage = true;
            }
        }
        Assert.assertTrue(foundPicture);
        Assert.assertTrue(foundTextMessage);
    }
}
Also used : MessagingException(javax.mail.MessagingException) ArrayList(java.util.ArrayList) WiserMessage(org.subethamail.wiser.WiserMessage) ByteArrayResource(org.springframework.core.io.ByteArrayResource) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) JavaMailSenderImpl(org.springframework.mail.javamail.JavaMailSenderImpl) MimeMessage(javax.mail.internet.MimeMessage) MailParseException(org.springframework.mail.MailParseException) EmailFragment(org.springframework.integration.samples.mailattachments.support.EmailFragment) Test(org.junit.Test)

Example 43 with JavaMailSenderImpl

use of org.springframework.mail.javamail.JavaMailSenderImpl in project molgenis by molgenis.

the class JavaMailSenderFactoryTest method testCreateMailSenderWithDefaultProperties.

@Test
public void testCreateMailSenderWithDefaultProperties() {
    JavaMailSenderImpl actual = javaMailSenderFactory.createMailSender(mailSettings);
    assertEquals(actual.getHost(), "host");
    assertEquals(actual.getPort(), 1234);
    assertEquals(actual.getUsername(), "username");
    assertEquals(actual.getPassword(), "password");
    assertEquals(actual.getDefaultEncoding(), "UTF-8");
    final Properties actualProperties = actual.getJavaMailProperties();
    assertEquals(actualProperties.getProperty("mail.smtp.starttls.enable"), "true");
    assertEquals(actualProperties.getProperty("mail.smtp.quitwait"), "false");
    assertEquals(actualProperties.getProperty("mail.smtp.auth"), "true");
}
Also used : JavaMailSenderImpl(org.springframework.mail.javamail.JavaMailSenderImpl) Properties(java.util.Properties) AbstractMockitoTest(org.molgenis.test.AbstractMockitoTest) Test(org.testng.annotations.Test)

Example 44 with JavaMailSenderImpl

use of org.springframework.mail.javamail.JavaMailSenderImpl in project molgenis by molgenis.

the class JavaMailSenderFactory method createMailSender.

@Override
public JavaMailSenderImpl createMailSender(MailSettings mailSettings) {
    LOG.trace("createMailSender");
    if (mailSettings.getUsername() == null || mailSettings.getPassword() == null) {
        throw new IllegalStateException("Cannot create mail sender. Username or password of the mail account not specified in the system mail settings.");
    }
    JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
    mailSender.setHost(mailSettings.getHost());
    mailSender.setPort(mailSettings.getPort());
    mailSender.setProtocol(mailSettings.getProtocol());
    mailSender.setUsername(mailSettings.getUsername());
    mailSender.setPassword(mailSettings.getPassword());
    mailSender.setDefaultEncoding(mailSettings.getDefaultEncoding().name());
    Properties properties = new Properties(defaultProperties);
    properties.putAll(mailSettings.getJavaMailProperties());
    LOG.debug("Mail properties: {}; defaults: {}", properties, defaultProperties);
    mailSender.setJavaMailProperties(properties);
    return mailSender;
}
Also used : JavaMailSenderImpl(org.springframework.mail.javamail.JavaMailSenderImpl) Properties(java.util.Properties)

Example 45 with JavaMailSenderImpl

use of org.springframework.mail.javamail.JavaMailSenderImpl in project molgenis by molgenis.

the class JavaMailSenderFactory method validateConnection.

@Override
public void validateConnection(MailSettings mailSettings) {
    LOG.info("Validating mail settings...");
    try {
        JavaMailSenderImpl sender = createMailSender(mailSettings);
        sender.testConnection();
        LOG.info("OK.");
    } catch (MessagingException ex) {
        String message = format("Unable to ping to %s", mailSettings.getHost());
        LOG.info(message, ex);
        throw new IllegalStateException(message, ex);
    }
}
Also used : JavaMailSenderImpl(org.springframework.mail.javamail.JavaMailSenderImpl) MessagingException(javax.mail.MessagingException)

Aggregations

JavaMailSenderImpl (org.springframework.mail.javamail.JavaMailSenderImpl)56 Properties (java.util.Properties)25 Bean (org.springframework.context.annotation.Bean)14 MimeMessage (javax.mail.internet.MimeMessage)11 MessagingException (javax.mail.MessagingException)10 MimeMessageHelper (org.springframework.mail.javamail.MimeMessageHelper)10 Test (org.junit.Test)9 JavaMailSender (org.springframework.mail.javamail.JavaMailSender)6 Date (java.util.Date)4 IOException (java.io.IOException)3 ByteArrayResource (org.springframework.core.io.ByteArrayResource)3 MailException (org.springframework.mail.MailException)3 SimpleMailMessage (org.springframework.mail.SimpleMailMessage)3 GreenMail (com.icegreen.greenmail.util.GreenMail)2 BufferedReader (java.io.BufferedReader)2 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 MalformedURLException (java.net.MalformedURLException)2 URL (java.net.URL)2 ArrayList (java.util.ArrayList)2