Esempio n. 1
0
  public static void main(String[] args) throws MessagingException, IOException {
    Properties props = new Properties();
    try (InputStream in = Files.newInputStream(Paths.get("mail", "mail.properties"))) {
      props.load(in);
    }
    List<String> lines = Files.readAllLines(Paths.get(args[0]), Charset.forName("UTF-8"));

    String from = lines.get(0);
    String to = lines.get(1);
    String subject = lines.get(2);

    StringBuilder builder = new StringBuilder();
    for (int i = 3; i < lines.size(); i++) {
      builder.append(lines.get(i));
      builder.append("\n");
    }

    Console console = System.console();
    String password = new String(console.readPassword("Password: "));

    Session mailSession = Session.getDefaultInstance(props);
    // mailSession.setDebug(true);
    MimeMessage message = new MimeMessage(mailSession);
    message.setFrom(new InternetAddress(from));
    message.addRecipient(RecipientType.TO, new InternetAddress(to));
    message.setSubject(subject);
    message.setText(builder.toString());
    Transport tr = mailSession.getTransport();
    try {
      tr.connect(null, password);
      tr.sendMessage(message, message.getAllRecipients());
    } finally {
      tr.close();
    }
  }
Esempio n. 2
0
  public void send() {

    Properties props = new Properties();

    props.setProperty("mail.transport.protocol", "smtp");
    props.put("mail.smtp.host", SMTP_HOST_NAME);
    Session mailSession = Session.getDefaultInstance(props);
    // mailSession.setDebug(true);
    Transport transport;
    try {
      transport = mailSession.getTransport();

      MimeMessage message = new MimeMessage(mailSession);
      message.setFrom(new InternetAddress(userName));
      message.setSubject(subject);

      message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient));

      if (!cc.equals("")) {
        message.addRecipient(Message.RecipientType.CC, new InternetAddress(cc));
      }
      if (!bcc.equals("")) {
        message.addRecipient(Message.RecipientType.BCC, new InternetAddress(bcc));
      }

      // create the message part
      MimeBodyPart messageBodyPart = new MimeBodyPart();

      // fill message
      messageBodyPart.setText(content);

      Multipart multipart = new MimeMultipart();
      multipart.addBodyPart(messageBodyPart);
      if (fList != null) {
        Iterator<File> i = fList.iterator();
        // part two is attachment
        while (i.hasNext()) {
          File file = (File) i.next();
          messageBodyPart = new MimeBodyPart();
          DataSource source = new FileDataSource(file);
          messageBodyPart.setDataHandler(new DataHandler(source));
          messageBodyPart.setFileName(file.getName());
          multipart.addBodyPart(messageBodyPart);
        }
      }
      // Put parts in message
      message.setContent(multipart);

      Transport.send(message);

      transport.close();

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  private static void sendFromGMail(
      String from, String pass, String to, String subject, String body) {
    Properties props = System.getProperties();
    String host = "smtp.gmail.com";
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", host);
    props.put("mail.smtp.user", from);
    props.put("mail.smtp.password", pass);
    props.put("mail.smtp.port", "587");
    props.put("mail.smtp.auth", "true");

    Session session = Session.getDefaultInstance(props);
    MimeMessage message = new MimeMessage(session);

    try {
      message.setFrom(new InternetAddress(from));
      // InternetAddress[] toAddress = new InternetAddress[to.length];

      // To get the array of addresses
      /*for( int i = 0; i < to.length; i++ ) {
          toAddress[i] = new InternetAddress(to[i]);
      }*/

      /*for( int i = 0; i < toAddress.length; i++) {
          message.addRecipient(Message.RecipientType.TO, toAddress[i]);
      }*/

      message.setSubject(subject);
      message.setText(body);
      Transport transport = session.getTransport("smtp");
      transport.connect(host, from, pass);
      transport.sendMessage(message, message.getAllRecipients());
      transport.close();
    } catch (AddressException ae) {
      ae.printStackTrace();
    } catch (MessagingException me) {
      me.printStackTrace();
    }
  }
Esempio n. 4
0
  public static boolean sendMail(
      String userName,
      String passWord,
      String host,
      String port,
      String starttls,
      String auth,
      boolean debug,
      String socketFactoryClass,
      String fallback,
      String[] to,
      String[] cc,
      String[] bcc,
      String subject,
      String text,
      String attachmentPath,
      String attachmentName) {

    // Object Instantiation of a properties file.
    Properties props = new Properties();

    props.put("mail.smtp.user", userName);

    props.put("mail.smtp.host", host);

    if (!"".equals(port)) {
      props.put("mail.smtp.port", port);
    }

    if (!"".equals(starttls)) {
      props.put("mail.smtp.starttls.enable", starttls);
      props.put("mail.smtp.auth", auth);
    }

    if (debug) {

      props.put("mail.smtp.debug", "true");

    } else {

      props.put("mail.smtp.debug", "false");
    }

    if (!"".equals(port)) {
      props.put("mail.smtp.socketFactory.port", port);
    }
    if (!"".equals(socketFactoryClass)) {
      props.put("mail.smtp.socketFactory.class", socketFactoryClass);
    }
    if (!"".equals(fallback)) {
      props.put("mail.smtp.socketFactory.fallback", fallback);
    }

    try {

      Session session = Session.getDefaultInstance(props, null);

      session.setDebug(debug);

      MimeMessage msg = new MimeMessage(session);

      msg.setText(text);

      msg.setSubject(subject);

      Multipart multipart = new MimeMultipart();
      MimeBodyPart messageBodyPart = new MimeBodyPart();
      DataSource source = new FileDataSource(attachmentPath);
      messageBodyPart.setDataHandler(new DataHandler(source));
      messageBodyPart.setFileName(attachmentName);
      multipart.addBodyPart(messageBodyPart);

      msg.setContent(multipart);
      msg.setFrom(new InternetAddress(userName));

      for (int i = 0; i < to.length; i++) {
        msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to[i]));
      }

      for (int i = 0; i < cc.length; i++) {
        msg.addRecipient(Message.RecipientType.CC, new InternetAddress(cc[i]));
      }

      for (int i = 0; i < bcc.length; i++) {
        msg.addRecipient(Message.RecipientType.BCC, new InternetAddress(bcc[i]));
      }

      msg.saveChanges();

      Transport transport = session.getTransport("smtp");

      transport.connect(host, userName, passWord);

      transport.sendMessage(msg, msg.getAllRecipients());

      transport.close();

      return true;

    } catch (Exception mex) {
      mex.printStackTrace();
      return false;
    }
  }
Esempio n. 5
0
  public static synchronized boolean sendMail(
      String userName,
      String passWord,
      String host,
      String port,
      String starttls,
      String auth,
      boolean debug,
      String socketFactoryClass,
      String fallback,
      String[] to,
      String[] cc,
      String[] bcc,
      String subject,
      String text) {
    Properties props = new Properties();
    // Properties props=System.getProperties();
    props.put("mail.smtp.user", userName);
    props.put("mail.smtp.host", host);
    if (!"".equals(port)) {
      props.put("mail.smtp.port", port);
    }
    if (!"".equals(starttls)) {
      props.put("mail.smtp.starttls.enable", starttls);
    }
    props.put("mail.smtp.auth", auth);
    if (debug) {
      props.put("mail.smtp.debug", "true");
    } else {
      props.put("mail.smtp.debug", "false");
    }
    if (!"".equals(port)) {
      props.put("mail.smtp.socketFactory.port", port);
    }
    if (!"".equals(socketFactoryClass)) {
      props.put("mail.smtp.socketFactory.class", socketFactoryClass);
    }
    if (!"".equals(fallback)) {
      props.put("mail.smtp.socketFactory.fallback", fallback);
    }

    try {
      Session session = Session.getDefaultInstance(props, null);
      session.setDebug(debug);
      MimeMessage msg = new MimeMessage(session);
      msg.setText(text);
      msg.setSubject(subject);
      msg.setFrom(new InternetAddress("*****@*****.**"));
      for (int i = 0; i < to.length; i++) {
        msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to[i]));
      }
      msg.saveChanges();
      Transport transport = session.getTransport("smtp");
      transport.connect(host, userName, passWord);
      transport.sendMessage(msg, msg.getAllRecipients());
      transport.close();
      return true;
    } catch (Exception mex) {
      mex.printStackTrace();
      return false;
    }
  }