Quick If/Else problem and edits for Javascript?

edited September 2015 in How To...

Dear friends,

My question database project is nearly finished! I've managed to get my data lists working, create a working SMTP function, and I now boast 1,300+ questions. Woo! My final hurdle is this: where and how should I put my sendMail() command so that it will send me an email ONLY if the input text does not match anything in my data files? Where I have it right now (line 46) makes it send me an email after every question. Check it out:

String[] to_match;
String[] resps;
String top = "Ask me anything.";
String cur = "";
final int QUERY_LIMIT = 35;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;

void setup() {
  size(1200, 500);
  PFont jackFont;
  jackFont = loadFont("CourierNewPSMT-48.vlw");
  to_match = loadStrings("to_match_file.txt");
  resps = loadStrings("resps_file.txt");

void draw() {
  text(top, 20,20, 1200,500);
  fill(225, 0, 0);
  text("> "+ cur+((millis()%1000)<500?(" "):("_")), 20,440, 1200,400);

void keyPressed() {
  if (key>='a'&&key<='z') {
    if (cur.length()<=QUERY_LIMIT) cur =  cur + char(key+'A'-'a');
  if (key>='A'&&key<='Z') {
    if (cur.length()<=QUERY_LIMIT) cur =  cur + char(key);
  if (key==' '&&(cur.length()<=QUERY_LIMIT)) cur=cur+' ';
  if (key=='?'&&(cur.length()<=QUERY_LIMIT)) cur=cur+'?';
  if (key==DELETE||key==BACKSPACE) {
    if (cur.length()>0) cur=cur.substring(0, cur.length()-1);
  if (key==ENTER||key==RETURN) {
      top = "Please ask a QUESTION.";
    } else {
      top = "I don't know. Ask something else.";
      sendMail();  //you'd think it would go here, wouldn't you?
    for(int i=0; i < to_match.length; i++){

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;

public class Auth extends Authenticator {

  public Auth() {

  public PasswordAuthentication getPasswordAuthentication() {
    String username, password;
    username = "bauhausrva@gmail.com";
    password = "********";
    System.out.println("authenticating. . ");
    return new PasswordAuthentication(username, password);

void sendMail() {
  // Create a session
  String host="smtp.gmail.com";
  Properties props=new Properties();
  props.put("mail.pop3.host", "pop.gmail.com");

  // SMTP Session
  props.put("mail.transport.protocol", "smtp");
  props.put("mail.smtp.host", host);
  props.put("mail.smtp.port", "25");
  props.put("mail.smtp.auth", "true");
  // We need TTLS, which gmail requires

  // Create a session
  Session session = Session.getDefaultInstance(props, new Auth());

    // Make a new message
    MimeMessage message = new MimeMessage(session);

    // Who is this message from
    message.setFrom(new InternetAddress("bauhausrva@gmail.com", "Ask Me Anything"));

    // Who is this message to (we could do fancier things like make a list or add CC's)
    message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("jackstarcat@gmail.com", false));

    // Subject and body
    message.setSubject("Quick Question:");

    println("Mail sent!");
  catch(Exception e)


Also, are there any edits that I can do to make this program run as Javascript so that I could upload it to OpenProcessing? I tried uploading an email-less version (I know libraries can be impossible to carry over) where the code created this http://www.openprocessing.org/sketch/216890 so any help on that score would be appreciated as well. If you know of a place besides OpenProcessing that can host Processing applications without too much hullabaloo, I'm all ears also!

Thanks! -Jack

Sign In or Register to comment.