Example usage for org.springframework.mail.javamail JavaMailSenderImpl setPort

List of usage examples for org.springframework.mail.javamail JavaMailSenderImpl setPort

Introduction

In this page you can find the example usage for org.springframework.mail.javamail JavaMailSenderImpl setPort.

Prototype

public void setPort(int port) 

Source Link

Document

Set the mail server port.

Usage

From source file:org.apache.syncope.core.notification.NotificationTest.java

@Before
public void setupSMTP() throws Exception {
    JavaMailSenderImpl sender = (JavaMailSenderImpl) mailSender;
    sender.setDefaultEncoding(SyncopeConstants.DEFAULT_ENCODING);
    sender.setHost(smtpHost);/*from w  w  w .j av  a  2s  . com*/
    sender.setPort(smtpPort);
    sender.setUsername(mailAddress);
    sender.setPassword(mailPassword);
}

From source file:org.craftercms.commons.mail.impl.EmailFactoryImplTest.java

private JavaMailSenderImpl createMailSender() {
    JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
    mailSender.setHost("localhost");
    mailSender.setPort(25);
    mailSender.setProtocol("smtp");
    mailSender.setDefaultEncoding(ENCODING);

    return mailSender;
}

From source file:org.haiku.haikudepotserver.config.AppConfig.java

@Bean
public MailSender mailSender(@Value("${smtp.host}") String host, @Value("${smtp.port:25}") Integer port,
        @Value("${smtp.auth:false}") Boolean smtpAuth, @Value("${smtp.starttls:false}") Boolean startTls,
        @Value("${smtp.username:}") String username, @Value("${smtp.password:}") String password) {

    Properties properties = new Properties();
    properties.put("mail.smtp.auth", smtpAuth);
    properties.put("mail.smtp.starttls.enable", startTls);

    JavaMailSenderImpl result = new JavaMailSenderImpl();

    result.setHost(host);//w w w  .  ja  va  2  s . c om
    result.setPort(port);
    result.setProtocol("smtp");
    result.setJavaMailProperties(properties);

    if (StringUtils.isNotBlank(username)) {
        result.setUsername(username);
    }

    if (StringUtils.isNotBlank(password)) {
        result.setPassword(password);
    }

    return result;
}

From source file:org.kuali.kra.service.impl.KcEmailServiceImpl.java

private JavaMailSender createSender() {
    Properties props = getConfigProperties();
    JavaMailSenderImpl sender = null;

    if (props != null && StringUtils.isNotBlank(props.getProperty("mail.smtp.host"))
            && StringUtils.isNotBlank(props.getProperty("mail.smtp.port"))
            && StringUtils.isNotBlank(props.getProperty("mail.smtp.user"))
            && StringUtils.isNotBlank(props.getProperty("mail.user.credentials"))) {
        try {//from  w w w .j  a v a  2  s  .c  o  m
            sender = new JavaMailSenderImpl();

            sender.setJavaMailProperties(props);
            sender.setHost(props.getProperty("mail.smtp.host"));
            sender.setPort(Integer.parseInt(props.getProperty("mail.smtp.port")));
            sender.setUsername(props.getProperty("mail.smtp.user"));
            sender.setPassword(props.getProperty("mail.user.credentials"));
        } catch (Exception e) {
            LOG.warn(
                    "Unable to create email sender due to invalid configuration. The properties [mail.smtp.host, "
                            + "mail.smtp.port, mail.smtp.user, mail.user.credentials] need to be set.");
            sender = null;
        }
    } else {
        LOG.warn("Unable to create email sender due to missing configuration.  The properties [mail.smtp.host, "
                + "mail.smtp.port, mail.smtp.user, mail.user.credentials] need to be set.");
    }

    return sender;
}

From source file:org.kuali.rice.core.mail.MailSenderFactoryBean.java

@Override
protected Object createInstance() throws Exception {
    // Retrieve "mail.*" properties from the configuration system and construct a Properties object
    Properties properties = new Properties();
    Properties configProps = ConfigContext.getCurrentContextConfig().getProperties();
    LOG.debug("createInstance(): collecting mail properties.");
    for (Object keyObj : configProps.keySet()) {
        if (keyObj instanceof String) {
            String key = (String) keyObj;
            if (key.startsWith(MAIL_PREFIX)) {
                properties.put(key, configProps.get(key));
            }/*from w  w w.  j  a v  a2  s. c om*/
        }
    }

    // Construct an appropriate Java Mail Session
    // If username and password properties are found, construct a Session with SMTP authentication
    String username = properties.getProperty(USERNAME_PROPERTY);
    String password = properties.getProperty(PASSWORD_PROPERTY);
    if (username != null && password != null) {
        mailSession = Session.getInstance(properties, new SimpleAuthenticator(username, password));
        LOG.info("createInstance(): Initializing mail session using SMTP authentication.");
    } else {
        mailSession = Session.getInstance(properties);
        LOG.info("createInstance(): Initializing mail session. No SMTP authentication.");
    }

    // Construct and return a Spring Java Mail Sender
    JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
    LOG.debug("createInstance(): setting SMTP host.");
    mailSender.setHost(properties.getProperty(HOST_PROPERTY));
    if (properties.getProperty(PORT_PROPERTY) != null) {
        LOG.debug("createInstance(): setting SMTP port.");
        int smtpPort = Integer.parseInt(properties.getProperty(PORT_PROPERTY).trim());
        mailSender.setPort(smtpPort);
    }
    String protocol = properties.getProperty(PROTOCOL_PROPERTY);
    if (StringUtils.isNotBlank(protocol)) {
        LOG.debug("createInstance(): setting mail transport protocol = " + protocol);
        mailSender.setProtocol(protocol);
    }
    mailSender.setSession(mailSession);

    LOG.info("createInstance(): Mail Sender Factory Bean initialized.");
    return mailSender;
}

From source file:org.mifosplatform.infrastructure.reportmailingjob.service.ReportMailingJobEmailServiceImpl.java

@Override
public void sendEmailWithAttachment(ReportMailingJobEmailData reportMailingJobEmailData) {
    try {/*from w  w  w .j  av a  2  s .c  om*/
        // get all ReportMailingJobConfiguration objects from the database
        this.reportMailingJobConfigurationDataCollection = this.reportMailingJobConfigurationReadPlatformService
                .retrieveAllReportMailingJobConfigurations();

        JavaMailSenderImpl javaMailSenderImpl = new JavaMailSenderImpl();
        javaMailSenderImpl.setHost(this.getReportSmtpServer());
        javaMailSenderImpl.setPort(this.getRerportSmtpPort());
        javaMailSenderImpl.setUsername(this.getReportSmtpUsername());
        javaMailSenderImpl.setPassword(this.getReportSmtpPassword());
        javaMailSenderImpl.setJavaMailProperties(this.getJavaMailProperties());

        MimeMessage mimeMessage = javaMailSenderImpl.createMimeMessage();

        // use the true flag to indicate you need a multipart message
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);

        mimeMessageHelper.setTo(reportMailingJobEmailData.getTo());
        mimeMessageHelper.setFrom(this.getReportSmtpFromAddress());
        mimeMessageHelper.setText(reportMailingJobEmailData.getText());
        mimeMessageHelper.setSubject(reportMailingJobEmailData.getSubject());

        if (reportMailingJobEmailData.getAttachment() != null) {
            mimeMessageHelper.addAttachment(reportMailingJobEmailData.getAttachment().getName(),
                    reportMailingJobEmailData.getAttachment());
        }

        javaMailSenderImpl.send(mimeMessage);
    }

    catch (MessagingException e) {
        // handle the exception
        e.printStackTrace();
    }
}

From source file:org.openhie.openempi.webapp.action.BaseActionTestCase.java

@Override
protected void onSetUpBeforeTransaction() throws Exception {
    smtpPort = smtpPort + (int) (Math.random() * 100);

    LocalizedTextUtil.addDefaultResourceBundle(Constants.BUNDLE_KEY);
    ActionContext.getContext().setSession(new HashMap());

    // change the port on the mailSender so it doesn't conflict with an 
    // existing SMTP server on localhost
    JavaMailSenderImpl mailSender = (JavaMailSenderImpl) applicationContext.getBean("mailSender");
    mailSender.setPort(getSmtpPort());
    mailSender.setHost("localhost");

    // populate the request so getRequest().getSession() doesn't fail in BaseAction.java
    ServletActionContext.setRequest(new MockHttpServletRequest());
}

From source file:org.springframework.integration.samples.mailattachments.MimeMessageParsingTest.java

/**
 * 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.
 *///  w  w  w  .  j a  va  2s . c o  m
@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);

    }
}

From source file:org.springframework.integration.samples.mailattachments.MimeMessageParsingTest.java

/**
 * This test will create a Mime Message that in return contains another
 * mime message. The nested mime message contains an attachment.
 *
 * The root message consist of both HTML and Text message.
 *
 *///from ww  w . j a v a2s .co m
@Test
public void testProcessingOfNestedEmailAttachments() {

    final JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
    mailSender.setPort(this.wiserPort);

    final MimeMessage rootMessage = mailSender.createMimeMessage();

    try {

        final MimeMessageHelper messageHelper = new MimeMessageHelper(rootMessage, true);

        messageHelper.setFrom("testfrom@springintegration.org");
        messageHelper.setTo("testto@springintegration.org");
        messageHelper.setSubject("Parsing of Attachments");
        messageHelper.setText("Spring Integration Rocks!", "Spring Integration <b>Rocks</b>!");

        final String pictureName = "picture.png";

        final ByteArrayResource byteArrayResource = getFileData(pictureName);

        messageHelper.addInline("picture12345", byteArrayResource, "image/png");

    } catch (MessagingException e) {
        throw new MailParseException(e);
    }

    mailSender.send(rootMessage);

    final List<WiserMessage> wiserMessages = wiser.getMessages();

    Assert.assertTrue(wiserMessages.size() == 1);

    boolean foundTextMessage = false;
    boolean foundPicture = false;
    boolean foundHtmlMessage = false;

    for (WiserMessage wiserMessage : wiserMessages) {

        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() == 3);

        for (EmailFragment emailFragment : emailFragments) {
            if ("<picture12345>".equals(emailFragment.getFilename())) {
                foundPicture = true;
            }

            if ("message.txt".equals(emailFragment.getFilename())) {
                foundTextMessage = true;
            }

            if ("message.html".equals(emailFragment.getFilename())) {
                foundHtmlMessage = true;
            }
        }

        Assert.assertTrue(foundPicture);
        Assert.assertTrue(foundTextMessage);
        Assert.assertTrue(foundHtmlMessage);

    }
}

From source file:org.thingsboard.rule.engine.mail.TbSendEmailNode.java

private JavaMailSenderImpl createMailSender() {
    JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
    mailSender.setHost(this.config.getSmtpHost());
    mailSender.setPort(this.config.getSmtpPort());
    mailSender.setUsername(this.config.getUsername());
    mailSender.setPassword(this.config.getPassword());
    mailSender.setJavaMailProperties(createJavaMailProperties());
    return mailSender;
}