public void postMail(
      String[] recipients, String subject, String message, String from, String[] cc)
      throws MessagingException {
    sender.setUsername((String) properties.getProperty("mail.user"));
    sender.setPassword((String) properties.getProperty("mail.password"));
    sender.setHost((String) properties.getProperty("mail.smtp.host"));
    String portString = (String) properties.getProperty("mail.smtp.port");
    sender.setPort(Integer.valueOf(portString));
    sender.setProtocol((String) properties.getProperty("mail.transport.protocol"));
    sender.setJavaMailProperties(properties);
    MimeMessage mimeMessage = sender.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, false, "UTF-8");
    helper.setFrom(from);
    helper.setText(message);
    helper.setSubject(subject);

    if (cc != null) {
      helper.setCc(cc);
    }

    for (String receiver : recipients) {
      if (receiver != null) {
        helper.setTo(receiver);
        sender.send(mimeMessage);
      }
    }
  }
  @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);
    } 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;
  }
  @Bean
  public JavaMailSenderImpl javaMailSender(JHipsterProperties jHipsterProperties) {
    log.debug("Configuring mail server");
    String host = jHipsterProperties.getMail().getHost();
    int port = jHipsterProperties.getMail().getPort();
    String user = jHipsterProperties.getMail().getUsername();
    String password = jHipsterProperties.getMail().getPassword();
    String protocol = jHipsterProperties.getMail().getProtocol();
    Boolean tls = jHipsterProperties.getMail().isTls();
    Boolean auth = jHipsterProperties.getMail().isAuth();

    JavaMailSenderImpl sender = new JavaMailSenderImpl();
    if (host != null && !host.isEmpty()) {
      sender.setHost(host);
    } 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;
  }
Exemple #4
0
 private void sendEmail(User user, String subject, String text) {
   if (host != null && !host.equals("")) {
     JavaMailSenderImpl sender = new JavaMailSenderImpl();
     sender.setHost(host);
     sender.setPort(port);
     sender.setUsername(smtpUser);
     sender.setPassword(smtpPassword);
     SimpleMailMessage message = new SimpleMailMessage();
     message.setTo(user.getLogin());
     message.setFrom(from);
     message.setSubject(subject);
     message.setText(text);
     try {
       sender.send(message);
       if (log.isDebugEnabled()) {
         log.debug("Sent e-mail to User '" + user.getLogin() + "'!");
       }
     } catch (MailException e) {
       log.warn("Warning! SMTP server error, could not send e-mail.");
       if (log.isDebugEnabled()) {
         log.debug("SMTP Error : " + e.getMessage());
         log.debug("Did you configure your SMTP settings in /META-INF/tatami/tatami.properties ?");
       }
     }
   } else {
     log.debug("SMTP server is not configured in /META-INF/tatami/tatami.properties");
   }
 }
  @Before
  public void onSetUp() {
    smtpPort = smtpPort + (int) (Math.random() * 100);

    LocalizedTextUtil.addDefaultResourceBundle(Constants.BUNDLE_KEY);

    // Initialize ActionContext
    ConfigurationManager configurationManager = new ConfigurationManager();
    configurationManager.addContainerProvider(new XWorkConfigurationProvider());
    Configuration config = configurationManager.getConfiguration();
    Container container = config.getContainer();

    ValueStack stack = container.getInstance(ValueStackFactory.class).createValueStack();
    stack.getContext().put(ActionContext.CONTAINER, container);
    ActionContext.setContext(new ActionContext(stack.getContext()));

    ActionContext.getContext().setSession(new HashMap<String, Object>());

    // 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());
  }
Exemple #6
0
 @Bean
 public JavaMailSender mailSender() {
   JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
   mailSender.setDefaultEncoding("UTF-8");
   mailSender.setHost(mailHost);
   mailSender.setPort(new Integer(mailPort));
   return mailSender;
 }
  @Test
  public void testSendMessageWithAttachment() throws Exception {
    final String ATTACHMENT_NAME = "boring-attachment.txt";

    // mock smtp server
    Wiser wiser = new Wiser();
    int port = 2525 + (int) (Math.random() * 100);
    mailSender.setPort(port);
    wiser.setPort(port);
    wiser.start();

    Date dte = new Date();
    String emailSubject = "grepster testSendMessageWithAttachment: " + dte;
    String emailBody = "Body of the grepster testSendMessageWithAttachment message sent at: " + dte;

    ClassPathResource cpResource = new ClassPathResource("/test-attachment.txt");
    // a null from should work
    mailEngine.sendMessage(
        new String[] {"*****@*****.**"}, null, cpResource, emailBody, emailSubject, ATTACHMENT_NAME);

    mailEngine.sendMessage(
        new String[] {"*****@*****.**"},
        mailMessage.getFrom(),
        cpResource,
        emailBody,
        emailSubject,
        ATTACHMENT_NAME);

    wiser.stop();
    // one without and one with from
    assertTrue(wiser.getMessages().size() == 2);

    WiserMessage wm = wiser.getMessages().get(0);
    MimeMessage mm = wm.getMimeMessage();

    Object o = wm.getMimeMessage().getContent();
    assertTrue(o instanceof MimeMultipart);
    MimeMultipart multi = (MimeMultipart) o;
    int numOfParts = multi.getCount();

    boolean hasTheAttachment = false;
    for (int i = 0; i < numOfParts; i++) {
      BodyPart bp = multi.getBodyPart(i);
      String disp = bp.getDisposition();
      if (disp == null) { // the body of the email
        Object innerContent = bp.getContent();
        MimeMultipart innerMulti = (MimeMultipart) innerContent;
        assertEquals(emailBody, innerMulti.getBodyPart(0).getContent());
      } else if (disp.equals(Part.ATTACHMENT)) { // the attachment to the email
        hasTheAttachment = true;
        assertEquals(ATTACHMENT_NAME, bp.getFileName());
      } else {
        fail("Did not expect to be able to get here.");
      }
    }
    assertTrue(hasTheAttachment);
    assertEquals(emailSubject, mm.getSubject());
  }
 @Before
 public void onSetUp() {
   smtpPort = smtpPort + (int) (Math.random() * 100);
   // change the port on the mailSender so it doesn't conflict with an
   // existing SMTP server on localhost
   final JavaMailSenderImpl mailSender =
       (JavaMailSenderImpl) applicationContext.getBean("mailSender");
   mailSender.setPort(smtpPort);
   mailSender.setHost("localhost");
 }
 @Bean
 public JavaMailSender javaMailService() {
   JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
   javaMailSender.setHost(env.getRequiredProperty(PROPERTY_HOST));
   javaMailSender.setPort(parsePortProperty(env.getRequiredProperty(PROPERTY_PORT)));
   javaMailSender.setUsername(env.getRequiredProperty(PROPERTY_USERNAME));
   javaMailSender.setPassword(env.getRequiredProperty(PROPERTY_PASSWORD));
   javaMailSender.setJavaMailProperties(getMailProperties());
   javaMailSender.setDefaultEncoding(WebMvcConfiguration.DEFAULT_CHAR_ENCODING);
   return javaMailSender;
 }
  @Bean
  public JavaMailSender mailSender() {
    JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
    mailSender.setHost("localhost");
    mailSender.setPort(25);

    Properties javaMailProperties = new Properties();
    javaMailProperties.put("mail.transport.protocol", "smtp");
    mailSender.setJavaMailProperties(javaMailProperties);
    return mailSender;
  }
  @PostConstruct
  public void init() throws IOException {
    mailSender.setHost(mailHost);
    mailSender.setPort(mailPort);
    mailSender.setProtocol(mailProtocol);
    mailSender.setPassword(mailPwd);
    mailSender.setUsername(mailUser);

    Properties props = new Properties();
    props.load(mailProps.getInputStream());
    mailSender.setJavaMailProperties(props);
  }
 JavaMailSender createSender() {
   Map<String, String> settings = systemSettingsStore.getSettings();
   String host = settings.get(SystemSettingsStore.MAIL_HOST_NAME);
   int port = Integer.parseInt(settings.get(SystemSettingsStore.MAIL_HOST_PORT));
   if (StringUtils.isNotBlank(host)) {
     JavaMailSenderImpl sender = new JavaMailSenderImpl();
     sender.setHost(host);
     sender.setPort(port);
     return sender;
   } else {
     return null;
   }
 }
 @Bean
 public JavaMailSenderImpl javaMailSenderImpl() {
   JavaMailSenderImpl mailSenderImpl = new JavaMailSenderImpl();
   mailSenderImpl.setHost(env.getProperty("smtp.host"));
   mailSenderImpl.setPort(env.getProperty("smtp.port", Integer.class));
   mailSenderImpl.setProtocol(env.getProperty("smtp.protocol"));
   mailSenderImpl.setUsername(env.getProperty("smtp.username"));
   mailSenderImpl.setPassword(env.getProperty("smtp.password"));
   Properties javaMailProps = new Properties();
   javaMailProps.put("mail.smtp.auth", true);
   javaMailProps.put("mail.smtp.starttls.enable", true);
   mailSenderImpl.setJavaMailProperties(javaMailProps);
   return mailSenderImpl;
 }
 private void applyProperties(JavaMailSenderImpl sender) {
   sender.setHost(this.properties.getHost());
   if (this.properties.getPort() != null) {
     sender.setPort(this.properties.getPort());
   }
   sender.setUsername(this.properties.getUsername());
   sender.setPassword(this.properties.getPassword());
   sender.setProtocol(this.properties.getProtocol());
   if (this.properties.getDefaultEncoding() != null) {
     sender.setDefaultEncoding(this.properties.getDefaultEncoding().name());
   }
   if (!this.properties.getProperties().isEmpty()) {
     sender.setJavaMailProperties(asProperties(this.properties.getProperties()));
   }
 }
  @Bean
  public MailSender mailSender() {
    JavaMailSenderImpl javaMailSenderImpl = new JavaMailSenderImpl();
    javaMailSenderImpl.setHost("smtp.ig.com.br");
    javaMailSenderImpl.setPassword("leonardo941231cordeiro");
    javaMailSenderImpl.setPort(587);
    javaMailSenderImpl.setUsername("*****@*****.**");

    Properties mailProperties = new Properties();
    mailProperties.put("mail.smtp.auth", true);
    mailProperties.put("mail.smtp.starttls.enable", true);
    javaMailSenderImpl.setJavaMailProperties(mailProperties);

    return javaMailSenderImpl;
  }
  @Bean
  public MailSender mailSender() {

    JavaMailSenderImpl javaMailSenderImpl = new JavaMailSenderImpl();
    /*javaMailSenderImpl.setHost("smtp.gmail.com");*/
    javaMailSenderImpl.setHost("mail.loccoagencia.com.br");
    javaMailSenderImpl.setPassword("Locco1234");
    /*javaMailSenderImpl.setPort(587);*/
    javaMailSenderImpl.setPort(26);
    javaMailSenderImpl.setUsername("*****@*****.**");
    Properties mailProperties = new Properties();
    mailProperties.put("mail.smtp.auth", true);
    mailProperties.put("mail.smtp.starttls.enable", true);
    javaMailSenderImpl.setJavaMailProperties(mailProperties);

    return javaMailSenderImpl;
  }
 private void organMailContent() {
   mailSender.setHost(ConfigManager.getInstance().getString("mail.host"));
   mailSender.setPort(ConfigManager.getInstance().getInt("mail.port", 25));
   mailSender.setUsername(ConfigManager.getInstance().getString("mail.username"));
   mailSender.setPassword(ConfigManager.getInstance().getString("mail.password"));
   mailSender.setDefaultEncoding("utf-8");
   log.info(
       "HOST>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>mailSender.getHost()="
           + mailSender.getHost()
           + ",mailSender.getPort()="
           + mailSender.getPort());
   Properties props = new Properties();
   props.setProperty("mail.smtp.auth", "true");
   props.setProperty("mail.smtp.timeout", "0");
   mailSender.setJavaMailProperties(props);
   this.setMailSender(mailSender);
 }
  @Bean
  public JavaMailSenderImpl javaMailSenderImpl() {
    JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();

    javaMailSender.setProtocol(env.getRequiredProperty("email.protocol"));
    javaMailSender.setHost(env.getRequiredProperty("email.host"));
    javaMailSender.setPort(Integer.parseInt(env.getRequiredProperty("email.port")));

    javaMailSender.setUsername(env.getRequiredProperty("email.username"));
    javaMailSender.setPassword(env.getRequiredProperty("email.password"));

    Properties properties = new Properties();
    properties.putAll(emailProperties());
    javaMailSender.setJavaMailProperties(properties);

    return javaMailSender;
  }
 protected Wiser startWiser(int smtpPort) {
   Wiser wiser = new Wiser();
   wiser.setPort(smtpPort);
   try {
     wiser.start();
   } catch (RuntimeException re) {
     if (re.getCause() instanceof BindException) {
       int nextPort = smtpPort + 1;
       if (nextPort - smtpPort > 10) {
         log.error("Exceeded 10 attempts to start SMTP server, aborting...");
         throw re;
       }
       log.error("SMTP port " + smtpPort + " already in use, trying " + nextPort);
       return startWiser(nextPort);
     }
   }
   mailSender.setPort(smtpPort);
   return wiser;
 }
Exemple #20
0
  private void sendEmail(Task.Status exitStatus, File logFile)
      throws IOException, MessagingException, TemplateException {
    String smtpServer = RepoxContextUtil.getRepoxManager().getConfiguration().getSmtpServer();
    if (smtpServer == null || smtpServer.isEmpty()) {
      return;
    }

    String fromEmail = RepoxContextUtil.getRepoxManager().getConfiguration().getDefaultEmail();
    String recipientsEmail =
        RepoxContextUtil.getRepoxManager().getConfiguration().getAdministratorEmail();
    String adminMailPass = RepoxContextUtil.getRepoxManager().getConfiguration().getMailPassword();
    String subject = "REPOX Data Source ingesting finished. Exit status: " + exitStatus.toString();

    EmailSender emailSender = new EmailSender();
    String pathIngestFile =
        URLDecoder.decode(
            Thread.currentThread().getContextClassLoader().getResource("ingest.html.ftl").getFile(),
            "ISO-8859-1");
    emailSender.setTemplate(
        pathIngestFile.substring(0, pathIngestFile.lastIndexOf("/")) + "/ingest");

    HashMap map = new HashMap<String, String>();
    map.put("exitStatus", exitStatus.toString());
    map.put("id", id);

    JavaMailSenderImpl mail = new JavaMailSenderImpl();
    // mail.setUsername(fromEmail);
    mail.setUsername(recipientsEmail);

    mail.setPassword(adminMailPass);

    mail.setPort(25);

    Properties props = System.getProperties();

    props.put("mail.smtp.host", smtpServer);
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.auth", "true");
    mail.setJavaMailProperties(props);

    emailSender.setMailSender(mail);
    emailSender.sendEmail(recipientsEmail, fromEmail, subject, map, logFile.getAbsolutePath());
  }
  @Bean
  public JavaMailSender mailSender() {
    JavaMailSender mailSender = null;
    if (isMailSendingConfigured()) {
      mailSender = new JavaMailSenderImpl();
      ((JavaMailSenderImpl) mailSender).setHost(host);
      ((JavaMailSenderImpl) mailSender).setPort(port);
      if (!StringUtils.isEmpty(userName)) {
        ((JavaMailSenderImpl) mailSender).setUsername(userName);
      }
      if (!StringUtils.isEmpty(password)) {
        ((JavaMailSenderImpl) mailSender).setPassword(password);
      }
      ((JavaMailSenderImpl) mailSender).setJavaMailProperties(getJavaMailProperties());
    } else {
      mailSender = new DummyEmailSender();
    }

    return mailSender;
  }
Exemple #22
0
  public static void main(String[] args) throws Exception {
    String smtpServer = RepoxContextUtil.getRepoxManager().getConfiguration().getSmtpServer();
    if (smtpServer == null || smtpServer.isEmpty()) {
      return;
    }

    String fromEmail = RepoxContextUtil.getRepoxManager().getConfiguration().getDefaultEmail();
    String recipientsEmail =
        RepoxContextUtil.getRepoxManager().getConfiguration().getAdministratorEmail();
    String adminMailPass = RepoxContextUtil.getRepoxManager().getConfiguration().getMailPassword();
    String subject = "REPOX Data Source ingesting finished. Exit status: ";

    EmailSender emailSender = new EmailSender();
    String pathIngestFile =
        URLDecoder.decode(
            Thread.currentThread().getContextClassLoader().getResource("ingest.html.ftl").getFile(),
            "ISO-8859-1");
    emailSender.setTemplate(
        pathIngestFile.substring(0, pathIngestFile.lastIndexOf("/")) + "/ingest");

    HashMap map = new HashMap<String, String>();
    map.put("exitStatus", "OK");
    map.put("id", "1111");

    JavaMailSenderImpl mail = new JavaMailSenderImpl();
    mail.setUsername(recipientsEmail);
    mail.setPassword(adminMailPass);

    mail.setPort(25);

    Properties props = System.getProperties();

    props.put("mail.smtp.host", smtpServer);
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.auth", "false");
    mail.setJavaMailProperties(props);

    emailSender.setMailSender(mail);
    emailSender.sendEmail(
        recipientsEmail, fromEmail, subject, map, "C:\\Users\\GPedrosa\\Desktop\\indexSec.txt");
  }
 @Bean
 public JavaMailSender javaMailSender() {
   JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
   javaMailSender.setHost(host);
   javaMailSender.setPort(Integer.parseInt(port));
   javaMailSender.setUsername(username);
   javaMailSender.setPassword(password);
   Properties properties = new Properties();
   properties.put("mail.smtp.auth", "true");
   properties.put("mail.smtp.starttls.enable", "true");
   properties.put("mail.smtp.ssl.trust", "smtp.gmail.com");
   MailSSLSocketFactory sf = null;
   try {
     sf = new MailSSLSocketFactory();
     sf.setTrustAllHosts(true);
     properties.put("mail.smtp.ssl.socketFactory", sf);
   } catch (GeneralSecurityException e) {
     e.printStackTrace();
   }
   javaMailSender.setJavaMailProperties(properties);
   return javaMailSender;
 }
  public void testSend() throws Exception {
    // mock smtp server
    Wiser wiser = new Wiser();
    // set the port to a random value so there's no conflicts between tests
    int port = 2525 + (int) (Math.random() * 100);
    mailSender.setPort(port);
    wiser.setPort(port);
    wiser.start();

    Date dte = new Date();
    this.mailMessage.setTo("*****@*****.**");
    String emailSubject = "grepster testSend: " + dte;
    String emailBody = "Body of the grepster testSend message sent at: " + dte;
    this.mailMessage.setSubject(emailSubject);
    this.mailMessage.setText(emailBody);
    this.mailEngine.send(this.mailMessage);

    wiser.stop();
    assertTrue(wiser.getMessages().size() == 1);
    WiserMessage wm = wiser.getMessages().get(0);
    assertEquals(emailSubject, wm.getMimeMessage().getSubject());
    assertEquals(emailBody, wm.getMimeMessage().getContent());
  }