Ejemplo n.º 1
0
  private static void sendMail(String str, String sendTo, String subject) {
    if (javaMail == null) {
      try {
        javaMail = new JavaMailSenderImpl();

        javaMail.setUsername(MAIL_USERNAME);
        javaMail.setPassword(MAIL_PASSWORD);

        Properties prop = new Properties();
        prop.setProperty("mail.smtp.auth", "true");
        prop.setProperty("mail.debug", "true");
        prop.setProperty("mail.smtp.socketFactory.port", "465");
        prop.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        prop.setProperty("mail.smtp.socketFactory.fallback", "false");
        prop.setProperty("mail.smtp.port", "465");
        prop.setProperty("host", MAIL_SMTP);

        javaMail.setJavaMailProperties(prop);
        javaMail.setProtocol("smtp");
      } catch (Exception ex) {
        ex.printStackTrace();
      }
    }
    // 初始化邮件信息
    SimpleMailMessage message = new SimpleMailMessage();
    message.setFrom(MAIL_USERNAME + "@" + MAIL_HOST);
    message.setTo(sendTo);
    message.setSubject(subject);
    message.setText(str);

    javaMail.send(message);
  }
  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);
      }
    }
  }
  @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);
  }
Ejemplo n.º 4
0
 @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()));
   }
 }
Ejemplo n.º 6
0
  @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;
  }