View Javadoc
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  public class SendEmailUtil {
17  
18  	public static void sendRecoveryMail(UserTO userTO) throws AddressException, MessagingException {
19  
20  		Properties systemProperties = LoadPropertiesUtil.loadProperties("booking.properties");
21  
22  		// Recipient's email ID needs to be mentioned.
23  		String to = userTO.getEmail();
24  
25  		// Sender's email ID needs to be mentioned
26  		String from = systemProperties.getProperty("mail.smtp.from");
27  
28  		// Get system properties
29  		final String username = systemProperties.getProperty("mail.smtp.username");
30  		final String password = systemProperties.getProperty("mail.smtp.password");
31  
32  		Properties props = new Properties();
33  		props.put("mail.smtp.auth", systemProperties.getProperty("mail.smtp.auth"));
34  		props.put("mail.smtp.starttls.enable", systemProperties.getProperty("mail.smtp.starttls.enable"));
35  		props.put("mail.smtp.host", systemProperties.getProperty("mail.smtp.host"));
36  		props.put("mail.smtp.port", systemProperties.getProperty("mail.smtp.port"));
37  
38  		Session session = Session.getInstance(props, new javax.mail.Authenticator() {
39  			protected PasswordAuthentication getPasswordAuthentication() {
40  				return new PasswordAuthentication(username, password);
41  			}
42  		});
43  
44  		// Create a default MimeMessage object.
45  		MimeMessage message = new MimeMessage(session);
46  
47  		// Set From: header field of the header.
48  		message.setFrom(new InternetAddress(from));
49  
50  		// Set To: header field of the header.
51  		message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
52  
53  		// Set Subject: header field
54  		message.setSubject(systemProperties.getProperty("recovery.subject"));
55  
56  		// Now set the actual message
57  		message.setText(systemProperties.getProperty("recovery.body")
58  		        + systemProperties.getProperty("recovery.service") + userTO.getEmail() + "/"
59  		        + userTO.getRecoveryPasswordHash());
60  
61  		// Send message
62  		Transport.send(message);
63  		System.out.println("Sent message successfully....");
64  
65  	}
66  }