package com.mavlushechka.notary.util; import javax.mail.*; import javax.mail.internet.*; import java.util.Properties; public class EmailSender { private final static Session SESSION; static { Properties properties = new Properties(); properties.put("mail.smtp.auth", true); properties.put("mail.smtp.starttls.enable", true); properties.put("mail.smtp.host", "smtp.mailtrap.io"); properties.put("mail.smtp.port", "2525"); properties.put("mail.smtp.ssl.trust", "smtp.mailtrap.io"); properties.put("mail.smtp.ssl.protocols", "TLSv1.2"); SESSION = Session.getInstance(properties, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("4df6840637b3c8", "829aff4fc98967"); } }); } public static void send(String from, String to, String subject, String messageText) throws MessagingException { Message message = new MimeMessage(SESSION); message.setFrom(new InternetAddress(from)); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); message.setSubject(subject); MimeBodyPart mimeBodyPart = new MimeBodyPart(); mimeBodyPart.setContent(messageText, "text/html; charset=utf-8"); Multipart multipart = new MimeMultipart(); multipart.addBodyPart(mimeBodyPart); message.setContent(multipart); Transport.send(message); } }