Thursday, 11 August 2011

Sending emails via Gmail (and others) in Java.

The following class will send emails from a java program. It definitely works on Gmail accounts and if you know the settings it should work on others as well. Keep in mind that it needs the javax.mail jar in your classpath.

You can use it like this:

private static void sendEmail(){  
     String[] toRecipients = {"", "", ""};  
     String[] ccRecipients = {"", "and@you.too"};  
     EmailSender es = new EmailSender("", 465, "", "myPassword");  
     boolean res = es.sendEmail("", toRecipients, ccRecipients, "Hey guys!", "I can automate email sending now!");  
     } else {  
          System.err.println("Hmmmm... Something went wrong...");  
And here is the class: