Revision 49766
Added by Sofia Baltzi over 6 years ago
modules/uoa-user-management/trunk/src/main/java/eu/dnetlib/openaire/user/utils/EmailSender.java | ||
---|---|---|
1 |
package eu.dnetlib.openaire.user.utils; |
|
2 |
|
|
3 |
import org.apache.log4j.Logger; |
|
4 |
|
|
5 |
import javax.mail.*; |
|
6 |
|
|
7 |
import javax.mail.internet.AddressException; |
|
8 |
import javax.mail.internet.InternetAddress; |
|
9 |
import javax.mail.internet.MimeMessage; |
|
10 |
import java.util.Properties; |
|
11 |
|
|
12 |
/** |
|
13 |
* Created by kiatrop on 9/10/2017. |
|
14 |
*/ |
|
15 |
public class EmailSender { |
|
16 |
|
|
17 |
private String username; |
|
18 |
private String password; |
|
19 |
private String host; |
|
20 |
private String port; |
|
21 |
private String from; |
|
22 |
|
|
23 |
Logger logger = Logger.getLogger(EmailSender.class); |
|
24 |
|
|
25 |
public void sendEmail(String recipient, String subject, String body) throws MessagingException { |
|
26 |
|
|
27 |
// Get system properties |
|
28 |
Properties properties = System.getProperties(); |
|
29 |
properties.setProperty("mail.smtp.host", host); |
|
30 |
properties.put("mail.smtp.port", port); |
|
31 |
properties.put("mail.smtp.auth", "true"); //enable authentication |
|
32 |
properties.put("mail.smtp.starttls.enable", "true"); |
|
33 |
|
|
34 |
Session session = javax.mail.Session.getInstance(properties, |
|
35 |
new Authenticator() { |
|
36 |
protected PasswordAuthentication getPasswordAuthentication() { |
|
37 |
return new PasswordAuthentication(username, password); |
|
38 |
} |
|
39 |
}); |
|
40 |
|
|
41 |
// Create a default MimeMessage object. |
|
42 |
MimeMessage message = new MimeMessage(session); |
|
43 |
|
|
44 |
// Set From: header field of the header. |
|
45 |
message.setFrom(new InternetAddress(from)); |
|
46 |
|
|
47 |
// Set To: header field of the header. |
|
48 |
message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient)); |
|
49 |
|
|
50 |
// Set Subject: header field |
|
51 |
message.setSubject(subject); |
|
52 |
|
|
53 |
// For simple text setText() can be used instead of setContent() |
|
54 |
|
|
55 |
// Send the actual HTML message, as big as you like |
|
56 |
message.setContent(body, "text/html"); |
|
57 |
|
|
58 |
// Send message |
|
59 |
Transport.send(message); |
|
60 |
logger.debug("Sent message successfully....\n"); |
|
61 |
|
|
62 |
} |
|
63 |
|
|
64 |
public String getUsername() { |
|
65 |
return username; |
|
66 |
} |
|
67 |
|
|
68 |
public void setUsername(String username) { |
|
69 |
this.username = username; |
|
70 |
} |
|
71 |
|
|
72 |
public String getPassword() { |
|
73 |
return password; |
|
74 |
} |
|
75 |
|
|
76 |
public void setPassword(String password) { |
|
77 |
this.password = password; |
|
78 |
} |
|
79 |
|
|
80 |
public String getHost() { |
|
81 |
return host; |
|
82 |
} |
|
83 |
|
|
84 |
public void setHost(String host) { |
|
85 |
this.host = host; |
|
86 |
} |
|
87 |
|
|
88 |
public String getPort() { |
|
89 |
return port; |
|
90 |
} |
|
91 |
|
|
92 |
public void setPort(String port) { |
|
93 |
this.port = port; |
|
94 |
} |
|
95 |
|
|
96 |
public String getFrom() { |
|
97 |
return from; |
|
98 |
} |
|
99 |
|
|
100 |
public void setFrom(String from) { |
|
101 |
this.from = from; |
|
102 |
} |
|
103 |
} |
Also available in: Unified diff
Add EmailSender.java