Skip to content

Instantly share code, notes, and snippets.

@GaetanoPiazzolla
Last active October 1, 2021 13:43
Show Gist options
  • Save GaetanoPiazzolla/e54f88dd415ff639f46c6d9c847a1152 to your computer and use it in GitHub Desktop.
Save GaetanoPiazzolla/e54f88dd415ff639f46c6d9c847a1152 to your computer and use it in GitHub Desktop.
@Service
public class MailService implements MailServiceI {
@Value("${smtp.host}")
private String host;
@Value("${smtp.auth}")
private String auth;
@Value("${smtp.port}")
private String port;
@Value("${smtp.username}")
private String username;
@Value("${smtp.email-from}")
private String fromMail;
@Value("${smtp.email-from-name}")
private String fromName;
@Value("${smtp.password}")
private String password;
@Override
public void sendNewMail(String email, String mailMessage, String subject) throws MailNotSentException {
Properties props = new Properties();
props.put("mail.smtp.auth", auth);
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", port);
Session session = Session.getInstance(props);
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(fromMail, fromName));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(email));
message.setSubject(subject);
message.setContent(mailMessage, "text/html; charset=utf-8");
Transport.send(message, username, password);
}
}
@GaetanoPiazzolla
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment