public static boolean send(String replyTo, String from_email, String from_name, String to_email, String subject, String body, String type, String bcc_email) {
boolean sent = false;
try {
Properties prop = new Properties();
prop.put("mail.smtp.port", "25");
prop.put("mail.smtp.socketFactory.fallback", "false");
prop.put("mail.smtp.quitwait", "false");
prop.put("mail.smtp.host", "smtp.gmail.com");
prop.put("mail.smtp.auth", "true");
prop.put("mail.smtp.starttls.enable", "true");
Session session = Session.getDefaultInstance(prop);
String username = (String) prop.get("gmail.username");
String password = (String) prop.get("gmail.password");
Message msg = new MimeMessage(session);
msg.setSubject(subject);
InternetAddress from = new InternetAddress(from_email, from_name);
InternetAddress to = new InternetAddress(to_email);
msg.addRecipient(Message.RecipientType.TO, to);
// add bcc if not null
if ((bcc_email != null) && (!bcc_email.equals(""))) msg.addRecipient(Message.RecipientType.BCC, new InternetAddress(bcc_email));
msg.setFrom(from);
// set reply to email address
InternetAddress[] replyToAddress = new InternetAddress[1];
replyToAddress[0] = new InternetAddress(replyTo);
msg.setReplyTo(replyToAddress);
Multipart multipart = new MimeMultipart("related");
BodyPart htmlPart = new MimeBodyPart();
if (type.equals("html")) htmlPart.setContent(body, "text/html");
else htmlPart.setContent(body, "text/plain");
multipart.addBodyPart(htmlPart);
msg.setContent(multipart);
Transport transport = session.getTransport("smtp");
transport.connect(GMAIL_USERNAME, GMAIL_PASSWORD);
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
sent = true;
System.out.println("[MailTool] mail sent successfully ...");
} catch (Exception e) {
System.err.println("[MailTool] send() : " + e.getMessage());
e.printStackTrace();
}
return sent;
}