Here you can find the source of send(final String username, final String password, final String toEmail, final String subject, final String content)
public static void send(final String username, final String password, final String toEmail, final String subject, final String content)
//package com.java2s; //License from project: Apache License import java.util.Properties; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; public class Main { public static void send(final String username, final String password, final String toEmail, final String subject, final String content) { // Get system properties Properties properties = System.getProperties(); // Setup mail server properties.put("mail.smtp.auth", "true"); properties.put("mail.smtp.starttls.enable", "true"); properties.put("mail.smtp.host", "smtp.gmail.com"); properties.put("mail.smtp.port", "587"); ////from ww w . ja v a2 s.c o m Session session = Session.getInstance(properties, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); try { Message message = new MimeMessage(session); message.setFrom(new InternetAddress(username)); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail)); message.setSubject(subject); message.setText(content); Transport.send(message); System.out.println("Done Sending E-mail."); } catch (MessagingException e) { throw new RuntimeException(e); } } }