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;
}
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);
}
}
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");
}
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;
}
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);
}
}
Aggregations