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:com.pamarin.income.component.MailSenderImpl.java

private JavaMailSender senderSetup() throws IOException {
    Properties config = loadMailConfig();
    Properties pro = propertiesSetup();
    String username = config.getProperty("email.username");
    String password = config.getProperty("email.password");

    if (username == null) {
        throw new UncheckedMailException("require property email.username on classpath:" + EMAIL_CONFIG);
    }// w  w  w  .  j a v a 2s  .  co m

    if (password == null) {
        throw new UncheckedMailException("require property email.password on classpath:" + EMAIL_CONFIG);
    }

    JavaMailSenderImpl sender = new JavaMailSenderImpl();
    sender.setJavaMailProperties(pro);
    sender.setUsername(username);
    sender.setPassword(password);
    sender.setProtocol("smtps");
    sender.setPort(465);
    sender.setHost(SMTP_HOST_NAME);
    sender.setDefaultEncoding("utf-8");

    return sender;
}

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

public EmailSender(EmailAccountInfo defaultEmailAccount) {
    this.toSendList = Collections.synchronizedList(new ArrayList<Email>());
    this.toRetryList = new HashMap<Email, Integer>();
    this.defaultEmailAccount = defaultEmailAccount;
    this.defaultMailSender = new JavaMailSenderImpl();
    this.mailSenderMap = Collections.synchronizedMap(new HashMap<EmailAccountInfo, JavaMailSenderImpl>());
}

From source file:com.github.dbourdette.otto.service.mail.Mailer.java

private JavaMailSender mailSender(MailConfiguration configuration) {
    JavaMailSenderImpl sender = new JavaMailSenderImpl();
    sender.setHost(configuration.getSmtp());

    if (configuration.getPort() != 0) {
        sender.setPort(configuration.getPort());
    }//from w  ww . j ava  2 s.c  om

    if (StringUtils.isNotEmpty(configuration.getUser())) {
        sender.setUsername(configuration.getUser());
        sender.setPassword(configuration.getPassword());

        Properties javaMailProperties = new Properties();
        javaMailProperties.put("mail.smtp.auth", "true");
        sender.setJavaMailProperties(javaMailProperties);
    }

    return sender;
}

From source file:org.obiba.mica.config.MailConfiguration.java

@Bean
public JavaMailSenderImpl javaMailSender() {
    log.debug("Configuring mail server");
    String host = propertyResolver.getProperty(PROP_HOST, DEFAULT_PROP_HOST);
    int port = propertyResolver.getProperty(PROP_PORT, Integer.class, 0);
    String user = propertyResolver.getProperty(PROP_USER);
    String password = propertyResolver.getProperty(PROP_PASSWORD);
    String protocol = propertyResolver.getProperty(PROP_PROTO);
    Boolean tls = propertyResolver.getProperty(PROP_TLS, Boolean.class, false);
    Boolean auth = propertyResolver.getProperty(PROP_AUTH, Boolean.class, false);

    JavaMailSenderImpl sender = new JavaMailSenderImpl();
    if (host != null && !host.isEmpty()) {
        sender.setHost(host);/*from  ww  w . ja v a 2  s . c  om*/
    } else {
        log.warn("Warning! Your SMTP server is not configured. We will try to use one on localhost.");
        log.debug("Did you configure your SMTP settings in your application.yml?");
        sender.setHost(DEFAULT_HOST);
    }
    sender.setPort(port);
    sender.setUsername(user);
    sender.setPassword(password);

    Properties sendProperties = new Properties();
    sendProperties.setProperty(PROP_SMTP_AUTH, auth.toString());
    sendProperties.setProperty(PROP_STARTTLS, tls.toString());
    sendProperties.setProperty(PROP_TRANSPORT_PROTO, protocol);
    sender.setJavaMailProperties(sendProperties);
    return sender;
}

From source file:com.gnizr.web.action.user.TestRequestPasswordReset.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 ww .  j  a v  a  2 s  .  co m*/

    templateMessage = new SimpleMailMessage();
    templateMessage.setSubject("Reset Password");

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

    action = new RequestPasswordReset();
    action.setUserManager(userManager);
    action.setTokenManager(tokenManager);
    action.setVerifyResetTemplate(templateMessage);
    action.setMailSender(mailSender);
    action.setFreemarkerEngine(freemarkerEngine);
    action.setGnizrConfiguration(gnizrConfiguration);
}

From source file:com.gst.infrastructure.reportmailingjob.service.ReportMailingJobEmailServiceImpl.java

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

        JavaMailSenderImpl javaMailSenderImpl = new JavaMailSenderImpl();
        javaMailSenderImpl.setHost(this.getGmailSmtpServer());
        javaMailSenderImpl.setPort(this.getGmailSmtpPort());
        javaMailSenderImpl.setUsername(this.getGmailSmtpUsername());
        javaMailSenderImpl.setPassword(this.getGmailSmtpPassword());
        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.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.icgc.dcc.release.client.config.MailConfig.java

@Bean
@Primary/*from ww w  . java2 s.com*/
public JavaMailSender javaMailSender() {
    val properties = new Properties();
    properties.putAll(mail.getProperties());

    val sender = new JavaMailSenderImpl();
    sender.setJavaMailProperties(properties);

    return sender;
}

From source file:org.tsm.concharto.lab.LabJavaMail.java

private void sendConfirmationEmail(User user) {
    //mailSender.setHost("skipper");
    SimpleMailMessage message = new SimpleMailMessage();
    message.setTo(user.getEmail());//from  w ww  .j  av a 2  s.  c o  m
    String messageText = StringUtils.replace(WELCOME_MESSAGE, PARAM_NAME, user.getUsername());
    message.setText(messageText);
    message.setSubject(WELCOME_SUBJECT);
    message.setFrom("<Concharto Notifications> notify@concharto.com");

    JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
    MimeMessage mimeMessage = mailSender.createMimeMessage();
    InternetAddress from = new InternetAddress();
    from.setAddress("notify@concharto.com");
    InternetAddress to = new InternetAddress();
    to.setAddress(user.getEmail());
    try {
        from.setPersonal("Concharto Notifications");
        mimeMessage.addRecipient(Message.RecipientType.TO, to);
        mimeMessage.setSubject(WELCOME_SUBJECT);
        mimeMessage.setText(messageText);
        mimeMessage.setFrom(from);
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (MessagingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    mailSender.setHost("localhost");
    mailSender.send(mimeMessage);

    /*
    Confirm your registration with Concharto
            
    Hello sanmi,
            
    Welcome to the Concharto community!
            
    Please click on this link to confirm your registration: http://www.concharto.com/member/confirm.htm?id=:confirmation 
            
    You can find out more about us at http://wiki.concharto.com/wiki/About.
            
    If you were not expecting this email, just ignore it, no further action is required to terminate the request.
     */
}

From source file:com.glaf.mail.business.MailSendThread.java

public void run() {
    logger.debug("---------------send mail----------------------------");
    if (mailItem != null && MailTools.isMailAddress(mailItem.getMailTo())) {
        /**// w w w  .j a va  2  s.  co  m
         * ?5????
         */
        if (mailItem.getRetryTimes() > 5) {
            return;
        }
        /**
         * ????????
         */
        if (mailItem.getSendStatus() == 1) {
            return;
        }
        /**
         * ????
         */
        if (mailItem.getLastModified() < (System.currentTimeMillis() - DateUtils.DAY * 30)) {
            return;
        }

        MailAccount mailAccount = null;
        JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
        List<MailAccount> accounts = task.getAccounts();
        if (accounts.size() > 1) {
            java.util.Random r = new java.util.Random();
            mailAccount = accounts.get(Math.abs(r.nextInt(accounts.size())));
        } else {
            mailAccount = accounts.get(0);
        }

        logger.debug("send account:" + mailAccount);

        String content = task.getContent();

        if (StringUtils.isEmpty(task.getCallbackUrl())) {
            String serviceUrl = SystemConfig.getServiceUrl();
            String callbackUrl = serviceUrl + "/website/mail/receive/view";
            task.setCallbackUrl(callbackUrl);
        }

        if (StringUtils.isNotEmpty(task.getCallbackUrl())) {
            MxMailHelper mailHelper = new MxMailHelper();
            String messageId = "{taskId:\"" + task.getId() + "\",itemId:\"" + mailItem.getId() + "\"}";
            messageId = RequestUtils.encodeString(messageId);
            String url = task.getCallbackUrl() + "?messageId=" + messageId;
            content = mailHelper.embedCallbackScript(content, url);
        }

        MailMessage mailMessage = new MailMessage();
        mailMessage.setFrom(mailAccount.getMailAddress());
        mailMessage.setTo(mailItem.getMailTo());
        mailMessage.setContent(content);
        mailMessage.setSubject(task.getSubject());
        mailMessage.setSaveMessage(false);

        javaMailSender.setHost(mailAccount.getSmtpServer());
        javaMailSender.setPort(mailAccount.getSendPort() > 0 ? mailAccount.getSendPort() : 25);
        javaMailSender.setProtocol(JavaMailSenderImpl.DEFAULT_PROTOCOL);
        javaMailSender.setUsername(mailAccount.getUsername());
        if (StringUtils.isNotEmpty(mailAccount.getPassword())) {
            String pwd = RequestUtils.decodeString(mailAccount.getPassword());
            javaMailSender.setPassword(pwd);
            // logger.debug("send account pwd:"+pwd);
        }
        javaMailSender.setDefaultEncoding("GBK");

        try {
            mailItem.setSendDate(new Date());
            mailItem.setRetryTimes(mailItem.getRetryTimes() + 1);
            MailSender mailSender = ContextFactory.getBean("mailSender");
            mailSender.send(javaMailSender, mailMessage);
            mailItem.setSendStatus(1);
        } catch (Throwable ex) {
            mailItem.setSendStatus(-1);
            if (LogUtils.isDebug()) {
                logger.debug(ex);
                ex.printStackTrace();
            }
            if (ex instanceof javax.mail.internet.ParseException) {
                mailItem.setSendStatus(-10);
            } else if (ex instanceof javax.mail.AuthenticationFailedException) {
                mailItem.setSendStatus(-20);
            } else if (ex instanceof javax.mail.internet.AddressException) {
                mailItem.setSendStatus(-30);
            } else if (ex instanceof javax.mail.SendFailedException) {
                mailItem.setSendStatus(-40);
            } else if (ex instanceof java.net.UnknownHostException) {
                mailItem.setSendStatus(-50);
            } else if (ex instanceof java.net.SocketException) {
                mailItem.setSendStatus(-60);
            } else if (ex instanceof java.io.IOException) {
                mailItem.setSendStatus(-70);
            } else if (ex instanceof java.net.ConnectException) {
                mailItem.setSendStatus(-80);
            } else if (ex instanceof javax.mail.MessagingException) {
                mailItem.setSendStatus(-90);
                if (ex.getMessage().indexOf("response: 554") != -1) {
                    mailItem.setSendStatus(-99);
                }
            }
        } finally {
            mailDataFacede.updateMail(task.getId(), mailItem);
        }
    }
}

From source file:com.thinkbiganalytics.metadata.sla.TestConfiguration.java

@Bean(name = "slaEmailSender")
public JavaMailSender javaMailSender(
        @Qualifier("slaEmailConfiguration") EmailConfiguration emailConfiguration) {
    JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
    Properties mailProperties = new Properties();
    mailProperties.put("mail.smtp.auth", emailConfiguration.getSmtpAuth());
    mailProperties.put("mail.smtp.starttls.enable", emailConfiguration.getStarttls());
    if (StringUtils.isNotBlank(emailConfiguration.getSmptAuthNtmlDomain())) {
        mailProperties.put("mail.smtp.auth.ntlm.domain", emailConfiguration.getSmptAuthNtmlDomain());
    }//from  w  w  w .j  av a 2s.  c o  m
    mailProperties.put("mail.debug", "true");

    mailSender.setJavaMailProperties(mailProperties);
    mailSender.setHost(emailConfiguration.getHost());
    mailSender.setPort(emailConfiguration.getPort());
    mailSender.setProtocol(emailConfiguration.getProtocol());
    mailSender.setUsername(emailConfiguration.getUsername());
    mailSender.setPassword(emailConfiguration.getPassword());
    return mailSender;
}