-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmailService.java
More file actions
44 lines (37 loc) · 1.56 KB
/
Copy pathEmailService.java
File metadata and controls
44 lines (37 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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 EmailService {
// port that javax.mail work with
private static final String SMTP_HOST = "smtp.gmail.com";
private static final String SMTP_PORT = "587";
// auth things
private static final String USERNAME = System.getenv("EMAIL_USERNAME");
private static final String PASSWORD = System.getenv("EMAIL_PASSWORD");
public static void sendEmail(String recipient, String subject, String body)
throws MessagingException {
// like headers for our work
Properties props = new Properties();
props.put("mail.smtp.host", SMTP_HOST);
props.put("mail.smtp.port", SMTP_PORT);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
Session session = Session.getInstance(props, new javax.mail.Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(USERNAME, PASSWORD);
}
});
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(USERNAME));
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient));
msg.setSubject(subject);
msg.setText(body);
Transport.send(msg);
}
}