title: How to Output Results From Console to Email?
body: <p>I'm working on a java project that uses JDBC and Java Mail. I'm trying to send the output that I get from running my SQL statements to my email. </p>
  
  <p>The problem is the msg.setText(), this is the method that inserts the text into the body of the email, is static. So, I can't put any of my methods that execute SQL statements in there as parameters because I can't make a static reference to the non-static method. </p>
  
  <p>How do I send my output (the results in console) to an email address?</p>
  
  <p>The expected output from the console is this: </p>
  
  <pre><code>ALLDONE: 0
  COUNT(1): 0
  IN_AC_IND: 0
  Billing Count(1): 0
  Signon: 0
  </code></pre>
  
  <p>So, in the body of the email that would be the entire message. </p>
  
  <pre><code>The code: 
  import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.ResultSet;
  import java.sql.SQLException;
  import java.sql.Statement; 
  import java.util.Date;
  import java.util.Properties;
  import javax.mail.Authenticator;
  import javax.mail.Message;
  import javax.mail.MessagingException;
  import javax.mail.PasswordAuthentication;
  import javax.mail.Session;
  import javax.mail.Transport;
  import javax.mail.internet.AddressException;
  import javax.mail.internet.InternetAddress;
  import javax.mail.internet.MimeMessage;
  
  public class test {
  
      Connection conn; 
  
      public static void main (String[] args) {
  
      new test();
       email();
  
        } // end of main class
  
      public static void email(){
          final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
            // Get a Properties object
               Properties props = System.getProperties();
               props.setProperty("mail.smtp.host", "smtp.gmail.com");
               props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
               props.setProperty("mail.smtp.socketFactory.fallback", "false");
               props.setProperty("mail.smtp.port", "587");
               props.setProperty("mail.smtp.socketFactory.port", "587");
               props.put("mail.smtp.auth", "true");
               props.put("mail.debug", "true");
               props.put("mail.store.protocol", "pop3");
               props.put("mail.transport.protocol", "smtp");
               final String username = "user";//
               final String password = "pass";
               try{
               Session session = Session.getDefaultInstance(props, 
                                    new Authenticator(){
                                       protected PasswordAuthentication getPasswordAuthentication() {
                                          return new PasswordAuthentication(username, password);
                                       }});
  
             // -- Create a new message --
               Message msg = new MimeMessage(session);
  
            // -- Set the FROM and TO fields --
               msg.setFrom(new InternetAddress("email"));
               msg.setRecipients(Message.RecipientType.TO, 
                                InternetAddress.parse("email",false));
               msg.setSubject("Daily Activity Report");
               msg.setText("This is a test email ");
               msg.setSentDate(new Date());
               Transport.send(msg);
               System.out.println("Message sent.");
  
            }catch (MessagingException e){ System.out.println("Error: " + e);}
      }
  
      public test() {
          try
          {
  
              Class.forName("value");
              conn = DriverManager.getConnection("value", "value" , "value");
              execute();
              conn.close();
  
          }catch (ClassNotFoundException ex) {System.err.println(ex.getMessage());
          }catch (SQLException ex)           {System.err.println(ex.getMessage());}
          }
  
      public void execute() {
          method();
          // someOtherMethod();
          method2();
          method3();
      }
  
      public void method(){
      String query = "SELECT ALLDONE FROM table";
      try{
          Statement st = conn.createStatement();
          ResultSet rs = st.executeQuery(query);
          while (rs.next())
          {
              String s = rs.getString ("ALLDONE");
              System.out.println("ALLDONE: " + s);
          }
      }catch (SQLException ex){
          System.err.println(ex.getMessage());
      }
  </code></pre>