public class EmailHandler
{
private static String host = "smtp.gmail.com";
private static String from = "moc.liamg|emanresu#moc.liamg|emanresu";
private static String fromname = "Communication Portal";
private static String pass = "password";
public static void sendEmail(String[] to, String subject, String messageText) throws Exception
{
try
{
Properties properties = new Properties();
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.host", host);
properties.put("mail.smtp.user", from);
properties.put("mail.smtp.password", pass);
properties.put("mail.smtp.port", "587");
properties.put("mail.smtp.auth", "true");
//Authenticator auth = new SMTPAuthenticator();
Session session = Session.getDefaultInstance(properties, null);
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from, fromname));
InternetAddress[] to_address = new InternetAddress[to.length];
// To get the array of addresses
for (int i = 0; i < to_address.length; i++)
{
to_address[i] = new InternetAddress(to[i]);
}
for (int i = 0; i < to_address.length; i++)
{
message.addRecipient(Message.RecipientType.BCC, to_address[i]);
}
message.setSubject(subject);
message.setContent(messageText,"text/html" );
// sends the email message:
Transport transport = session.getTransport("smtp");
transport.connect(host, from, pass);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}