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

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

Introduction

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

Prototype

public JavaMailSenderImpl() 

Source Link

Document

Create a new instance of the JavaMailSenderImpl class.

Usage

From source file:gov.nih.nci.cacis.nav.AbstractSendMail.java

/**
 * Default constructor
 */
public AbstractSendMail() {
    super();
    mailSender = new JavaMailSenderImpl();
}

From source file:com.formkiq.core.service.notification.MailSenderService.java

/**
 * @param prop {@link Properties}// ww w  .  java2 s.  c o m
 * @return {@link JavaMailSenderImpl}
 * @throws IOException IOException
 */
private JavaMailSenderImpl createJavaMailSender(final Properties prop) throws IOException {

    JavaMailSenderImpl sender = new JavaMailSenderImpl();

    sender.setHost(prop.getProperty("mail.host"));
    sender.setPort(Integer.parseInt(prop.getProperty("mail.port")));

    sender.setUsername(prop.getProperty("mail.username"));
    sender.setPassword(prop.getProperty("mail.password"));

    Properties property = sender.getJavaMailProperties();

    if (prop.containsKey("mail.smtp.auth")) {
        property.setProperty("mail.smtp.auth", prop.getProperty("mail.smtp.auth"));
    }

    if (prop.containsKey("mail.smtp.starttls.enable")) {
        property.setProperty("mail.smtp.starttls.enable", prop.getProperty("mail.smtp.starttls.enable"));
    }

    if (prop.containsKey("mail.smtp.quitwait")) {
        property.setProperty("mail.smtp.quitwait", prop.getProperty("mail.smtp.quitwait"));
    }

    LOG.log(Level.INFO, "Setting mail.host = " + sender.getHost() + ":" + sender.getPort());
    LOG.log(Level.INFO, "Setting mail.username = " + sender.getUsername());

    for (Map.Entry<Object, Object> e : sender.getJavaMailProperties().entrySet()) {
        LOG.log(Level.INFO, "Setting " + e.getKey() + " = " + e.getValue());
    }

    return sender;
}

From source file:com.rxx.common.util.MailUtil.java

/**
 * html/*from w ww  . ja  v a  2  s.  c  om*/
 *
 * @param host 
 * @param port
 * @param userName
 * @param password
 * @param title
 * @param contenthtml
 * @param toUser
 * @throws javax.mail.MessagingException
 */
public static void sendHtml(String host, int port, String userName, String password, String title,
        String content, String[] toUser) {
    JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
    // mail server
    senderImpl.setHost(host);
    senderImpl.setPort(port);
    // ,html
    MimeMessage mailMessage = senderImpl.createMimeMessage();

    try {
        MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage, true, "UTF-8");

        try {
            // 
            messageHelper.setTo(toUser);
            messageHelper.setFrom(userName);
            messageHelper.setSubject(title);
            // true HTML
            messageHelper.setText(content, true);
        } catch (javax.mail.MessagingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        senderImpl.setUsername(userName); // ,username
        senderImpl.setPassword(password); // , password
        Properties prop = new Properties();
        prop.put("mail.smtp.auth", "true"); // true,
        prop.put("mail.smtp.timeout", "25000");
        senderImpl.setJavaMailProperties(prop);
        // 
        senderImpl.send(mailMessage);
    } catch (javax.mail.MessagingException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

}

From source file:com.springsource.insight.plugin.mail.MessageSendOperationCollectionAspectTest.java

private void testSendMessage(int port) {
    JavaMailSenderImpl sender = new JavaMailSenderImpl();
    sender.setHost(NetworkAddressUtil.LOOPBACK_ADDRESS);
    sender.setProtocol(JavaMailSenderImpl.DEFAULT_PROTOCOL);
    sender.setPort(port);//from   w  w w . j a  va 2  s . c  om

    SimpleMailMessage message = new SimpleMailMessage();
    message.setFrom("from@com.springsource.insight.plugin.mail");
    message.setTo("to@com.springsource.insight.plugin.mail");
    message.setCc("cc@com.springsource.insight.plugin.mail");
    message.setBcc("bcc@com.springsource.insight.plugin.mail");

    Date now = new Date(System.currentTimeMillis());
    message.setSentDate(now);
    message.setSubject(now.toString());
    message.setText("Test at " + now.toString());
    sender.send(message);

    Operation op = getLastEntered();
    assertNotNull("No operation extracted", op);
    assertEquals("Mismatched operation type", MailDefinitions.SEND_OPERATION, op.getType());
    assertEquals("Mismatched protocol", sender.getProtocol(),
            op.get(MailDefinitions.SEND_PROTOCOL, String.class));
    assertEquals("Mismatched host", sender.getHost(), op.get(MailDefinitions.SEND_HOST, String.class));
    if (port == -1) {
        assertEquals("Mismatched default port", 25, op.getInt(MailDefinitions.SEND_PORT, (-1)));
    } else {
        assertEquals("Mismatched send port", sender.getPort(), op.getInt(MailDefinitions.SEND_PORT, (-1)));
    }

    if (getAspect().collectExtraInformation()) {
        assertAddresses(op, MailDefinitions.SEND_SENDERS, 1);
        assertAddresses(op, MailDefinitions.SEND_RECIPS, 3);

        OperationMap details = op.get(MailDefinitions.SEND_DETAILS, OperationMap.class);
        assertNotNull("No details extracted", details);
        assertEquals("Mismatched subject", message.getSubject(),
                details.get(MailDefinitions.SEND_SUBJECT, String.class));
    }
}

From source file:com.gnizr.web.action.user.TestRegisterUser.java

protected void setUp() throws Exception {
    super.setUp();

    gnizrConfiguration = new GnizrConfiguration();
    gnizrConfiguration.setSiteContactEmail("admin@mysite.com");
    gnizrConfiguration.setWebApplicationUrl("http://foo.com/gnizr");

    FreeMarkerConfigurationFactory factory = new FreeMarkerConfigurationFactory();
    factory.setTemplateLoaderPath("/templates");
    freemarkerEngine = factory.createConfiguration();

    userManager = new UserManager(getGnizrDao());
    tokenManager = new TokenManager();
    tokenManager.setUserManager(userManager);
    tokenManager.init();/*from  w  w  w. j  av a 2  s . c om*/

    templateMessage = new SimpleMailMessage();
    templateMessage.setSubject("Email Verification");

    notifyMessage = new SimpleMailMessage();
    notifyMessage.setSubject("Account Registration Pending");

    approvalMessage = new SimpleMailMessage();
    approvalMessage.setSubject("Account Registration Approval");

    mailSender = new JavaMailSenderImpl();
    mailSender.setHost("localhost");

    action = new RegisterUser();
    action.setUserManager(userManager);
    action.setTokenManager(tokenManager);
    action.setVerifyEmailTemplate(templateMessage);
    action.setApprovalEmailTemplate(approvalMessage);
    action.setNotifyEmailTemplate(notifyMessage);
    action.setMailSender(mailSender);
    action.setFreemarkerEngine(freemarkerEngine);
    action.setGnizrConfiguration(gnizrConfiguration);
}

From source file:io.lavagna.model.MailConfig.java

private JavaMailSender toMailSender() {
    JavaMailSenderImpl r = new JavaMailSenderImpl();
    r.setDefaultEncoding("UTF-8");
    r.setHost(host);/*w  w  w.  j  a  v a2 s . c o  m*/
    r.setPort(port);
    r.setProtocol(protocol);
    r.setUsername(username);
    r.setPassword(password);
    if (properties != null) {
        try {
            Properties prop = PropertiesLoaderUtils.loadProperties(
                    new EncodedResource(new ByteArrayResource(properties.getBytes("UTF-8")), "UTF-8"));
            r.setJavaMailProperties(prop);
        } catch (IOException e) {
            LOG.warn("error while setting the mail sender properties", e);
        }
    }
    return r;
}

From source file:com.mycompany.spring.batch.mail.demo.batch.SampleBatchApplication.java

@Bean
public SimpleMailMessageItemWriter writer() {
    SimpleMailMessageItemWriter writer = new SimpleMailMessageItemWriter();
    writer.setMailSender(new JavaMailSenderImpl());
    return writer;
}

From source file:com.miserablemind.butter.bootstrap.RootContext.java

/**
 * E-mail Sender configured using {@link ConfigMail}.
 *
 * @return {@link JavaMailSender} bean./*from   www .ja  v a2s  .c o  m*/
 */
@Bean
public JavaMailSender emailSender() {

    ConfigMail configMail = configSystem.getConfigMail();

    JavaMailSenderImpl emailSender = new JavaMailSenderImpl();

    emailSender.setHost(configMail.getHost());
    emailSender.setUsername(configMail.getUsername());
    emailSender.setPassword(configMail.getPassword());
    emailSender.setPort(configMail.getPort());

    Properties props = new Properties();

    props.put("mail.smtp.auth", configMail.isSmtpAuth());
    props.put("mail.smtp.starttls.enable", configMail.isStartTls());
    props.put("mail.transport.protocol", configMail.getProtocol());
    props.put("mail.smtp.socketFactory.class", configMail.getSocketFactoryClass());
    props.put("mail.smtp.socketFactory.fallback", configMail.isSocketFactoryFallback());

    emailSender.setJavaMailProperties(props);

    return emailSender;
}

From source file:com.logsniffer.app.MailAppConfig.java

@Bean
public JavaMailSenderImpl mailSender() {
    mailSettings();/*  w  w  w .j  av  a2 s .c  o m*/
    mailSender = new JavaMailSenderImpl();
    mailSender.setDefaultEncoding("UTF-8");
    refreshMailSenderConfiguration();
    return mailSender;
}

From source file:org.jnap.core.email.EmailSender.java

public void send(Email email) {
    EmailAccountInfo accountInfo = defaultEmailAccount;
    JavaMailSenderImpl sender = this.defaultMailSender;
    if (email.getAccountInfo() != null) {
        accountInfo = email.getAccountInfo();
        synchronized (this.mailSenderMap) {
            sender = this.mailSenderMap.get(accountInfo);
            if (sender == null) {
                sender = new JavaMailSenderImpl();
                Properties props = new Properties(this.defaultEmailAccount.getJavaMailProperties());
                props.putAll(accountInfo.getJavaMailProperties());
                sender.setJavaMailProperties(props);
                sender.setUsername(accountInfo.getUsername());
                sender.setPassword(accountInfo.getPassword());
                this.mailSenderMap.put(accountInfo, sender);
            }/*  w  w w  .  ja  v a 2  s  .  c  o  m*/
        }
    } else {
        email.setAccountInfo(accountInfo);
    }
    sender.send((MimeMessagePreparator) email);
}