1 | |
package booking.model.util; |
2 | |
|
3 | |
import java.util.Properties; |
4 | |
|
5 | |
import javax.mail.Message; |
6 | |
import javax.mail.MessagingException; |
7 | |
import javax.mail.PasswordAuthentication; |
8 | |
import javax.mail.Session; |
9 | |
import javax.mail.Transport; |
10 | |
import javax.mail.internet.AddressException; |
11 | |
import javax.mail.internet.InternetAddress; |
12 | |
import javax.mail.internet.MimeMessage; |
13 | |
|
14 | |
import booking.model.entity.UserTO; |
15 | |
|
16 | 0 | public class SendEmailUtil { |
17 | |
|
18 | |
public static void sendRecoveryMail(UserTO userTO) throws AddressException, MessagingException { |
19 | |
|
20 | 0 | Properties systemProperties = LoadPropertiesUtil.loadProperties("booking.properties"); |
21 | |
|
22 | |
|
23 | 0 | String to = userTO.getEmail(); |
24 | |
|
25 | |
|
26 | 0 | String from = systemProperties.getProperty("mail.smtp.from"); |
27 | |
|
28 | |
|
29 | 0 | final String username = systemProperties.getProperty("mail.smtp.username"); |
30 | 0 | final String password = systemProperties.getProperty("mail.smtp.password"); |
31 | |
|
32 | 0 | Properties props = new Properties(); |
33 | 0 | props.put("mail.smtp.auth", systemProperties.getProperty("mail.smtp.auth")); |
34 | 0 | props.put("mail.smtp.starttls.enable", systemProperties.getProperty("mail.smtp.starttls.enable")); |
35 | 0 | props.put("mail.smtp.host", systemProperties.getProperty("mail.smtp.host")); |
36 | 0 | props.put("mail.smtp.port", systemProperties.getProperty("mail.smtp.port")); |
37 | |
|
38 | 0 | Session session = Session.getInstance(props, new javax.mail.Authenticator() { |
39 | |
protected PasswordAuthentication getPasswordAuthentication() { |
40 | 0 | return new PasswordAuthentication(username, password); |
41 | |
} |
42 | |
}); |
43 | |
|
44 | |
|
45 | 0 | MimeMessage message = new MimeMessage(session); |
46 | |
|
47 | |
|
48 | 0 | message.setFrom(new InternetAddress(from)); |
49 | |
|
50 | |
|
51 | 0 | message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); |
52 | |
|
53 | |
|
54 | 0 | message.setSubject(systemProperties.getProperty("recovery.subject")); |
55 | |
|
56 | |
|
57 | 0 | message.setText(systemProperties.getProperty("recovery.body") |
58 | |
+ systemProperties.getProperty("recovery.service") + userTO.getEmail() + "/" |
59 | |
+ userTO.getRecoveryPasswordHash()); |
60 | |
|
61 | |
|
62 | 0 | Transport.send(message); |
63 | 0 | System.out.println("Sent message successfully...."); |
64 | |
|
65 | 0 | } |
66 | |
} |