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();
    }
  }
Exemple #2
0
  public static void sendEmail(
      InternetAddress from, InternetAddress to, String content, String requester)
      throws MessagingException, UnsupportedEncodingException {

    Properties props = new Properties();

    props.put("mail.transport.protocol", "smtps");
    props.put("mail.smtps.host", SMTP_HOST_NAME);
    props.put("mail.smtps.auth", "true");

    Session mailSession = Session.getDefaultInstance(props);
    mailSession.setDebug(false);
    Transport transport = mailSession.getTransport();

    MimeMessage message = new MimeMessage(mailSession);
    message.setSubject("MN Traffic Generation is used by " + requester);
    message.setContent(content, "text/plain");

    message.addRecipient(Message.RecipientType.TO, to);

    InternetAddress[] replyto = new InternetAddress[1];
    replyto[0] = from;

    message.setFrom(from);
    message.setReplyTo(replyto);
    message.setSender(from);

    transport.connect(SMTP_HOST_NAME, SMTP_HOST_PORT, SMTP_AUTH_USER, SMTP_AUTH_PWD);

    transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));

    transport.close();
  }
Exemple #3
0
  public static void sendEmail(String email, String id)
      throws MessagingException, UnsupportedEncodingException {

    Properties props = new Properties();

    props.put("mail.transport.protocol", "smtps");
    props.put("mail.smtps.host", SMTP_HOST_NAME);
    props.put("mail.smtps.auth", "true");

    Session mailSession = Session.getDefaultInstance(props);
    mailSession.setDebug(false);
    Transport transport = mailSession.getTransport();

    MimeMessage message = new MimeMessage(mailSession);
    message.setSubject("Traffic Generation Request #" + id + " has received");

    message.setContent(
        "Hi Sir/Madam, \n\n Your traffic generation request #"
            + id
            + " has been received by DMLab@UMN. \n You will be notified via email "
            + email
            + " , when we finish our processing.\n\n Please be patient. If you have any inquries, please send email to [email protected]. \n\n Thanks, \n DMLab@UMN",
        "text/plain");

    InternetAddress[] mntgAddress = new InternetAddress[1];
    mntgAddress[0] =
        new InternetAddress(
            "*****@*****.**",
            "Minnesota Traffic Generator"); // here we set our email alias and the desired display
    // name
    InternetAddress customer_email = new InternetAddress(email); // customer email

    message.addRecipient(Message.RecipientType.TO, customer_email);

    message.addRecipients(Message.RecipientType.CC, mntgAddress);
    message.addRecipients(Message.RecipientType.BCC, mntgAddress);

    message.setFrom(mntgAddress[0]);
    message.setReplyTo(mntgAddress);
    message.setSender(mntgAddress[0]);

    transport.connect(SMTP_HOST_NAME, SMTP_HOST_PORT, SMTP_AUTH_USER, SMTP_AUTH_PWD);

    Address[] recipientsTo = message.getRecipients(Message.RecipientType.TO);
    Address[] recipientsCC = message.getRecipients(Message.RecipientType.CC);
    Address[] recipientsBCC = message.getRecipients(Message.RecipientType.BCC);

    Address[] allRecipients =
        new Address[recipientsTo.length + recipientsCC.length + recipientsBCC.length];
    int allIndex = 0;
    for (int i = 0; i < recipientsTo.length; ++i, ++allIndex) {
      allRecipients[allIndex] = recipientsTo[i];
    }
    for (int i = 0; i < recipientsCC.length; ++i, ++allIndex) {
      allRecipients[allIndex] = recipientsCC[i];
    }
    for (int i = 0; i < recipientsBCC.length; ++i, ++allIndex) {
      allRecipients[allIndex] = recipientsBCC[i];
    }

    transport.sendMessage(message, allRecipients);

    transport.close();

    //  InternetAddress notifyAddress = new InternetAddress("*****@*****.**", "Admin MailList");
    // //here we set our email alias and the desired display name
    // sendEmail(mntgAddress[0], notifyAddress,                "Traffic request #" + id + " has just
    // been submitted by user " + email + ".", email);

    /*
     * InternetAddress notifyAddress3 = new
     * InternetAddress("*****@*****.**", "Mohamed Mokbel"); //here we set
     * our email alias and the desired display name
     * sendEmail(mntgAddress[0], notifyAddress3, "Traffic request #" + id +
     * " has just been submitted by user " + email + ".", email);
     *
     */

  }