1
|
package eu.dnetlib.uoaadmintoolslibrary.emailSender;
|
2
|
|
3
|
import org.apache.log4j.Logger;
|
4
|
import org.springframework.beans.factory.annotation.Autowired;
|
5
|
import org.springframework.beans.factory.annotation.Configurable;
|
6
|
import org.springframework.stereotype.Service;
|
7
|
|
8
|
import javax.mail.*;
|
9
|
import javax.mail.internet.AddressException;
|
10
|
import javax.mail.internet.InternetAddress;
|
11
|
import javax.mail.internet.MimeMessage;
|
12
|
import java.util.List;
|
13
|
import java.util.Properties;
|
14
|
|
15
|
import eu.dnetlib.uoaadmintoolslibrary.configuration.properties.MailConfig;
|
16
|
|
17
|
@Service
|
18
|
@Configurable
|
19
|
public class EmailSender {
|
20
|
|
21
|
private static final Logger logger = Logger.getLogger(EmailSender.class);
|
22
|
|
23
|
@Autowired
|
24
|
private MailConfig mailConfig;
|
25
|
|
26
|
public boolean send(List<String> recipients, String subject, String body, Boolean bcc) {
|
27
|
// Get system properties
|
28
|
Properties properties = System.getProperties();
|
29
|
properties.setProperty("mail.smtp.host", mailConfig.getHost());
|
30
|
properties.put("mail.smtp.port", mailConfig.getPort());
|
31
|
properties.put("mail.smtp.auth", mailConfig.getAuth()); //enable authentication
|
32
|
properties.put("mail.smtp.starttls.enable", "true");
|
33
|
logger.debug("Try to connect to mail sender with "+ mailConfig.getUsername());
|
34
|
Session session = Session.getInstance(properties,
|
35
|
new javax.mail.Authenticator() {
|
36
|
protected PasswordAuthentication getPasswordAuthentication() {
|
37
|
return new PasswordAuthentication(mailConfig.getUsername(), mailConfig.getPassword());
|
38
|
}
|
39
|
});
|
40
|
|
41
|
try {
|
42
|
logger.debug("Try to sent e-mail to "+recipients.toString()+
|
43
|
"\nSubject: "+subject+
|
44
|
"\nBody:"+body);
|
45
|
|
46
|
// Create a default MimeMessage object.
|
47
|
MimeMessage message = new MimeMessage(session);
|
48
|
|
49
|
// Set From: header field of the header.
|
50
|
message.setFrom(new InternetAddress(mailConfig.getFrom()));
|
51
|
|
52
|
// Set To: header field of the header.
|
53
|
if(!bcc) {
|
54
|
for (String to : recipients) {
|
55
|
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
|
56
|
}
|
57
|
}else{
|
58
|
for (String to : recipients) {
|
59
|
message.addRecipient(Message.RecipientType.BCC, new InternetAddress(to));
|
60
|
}
|
61
|
}
|
62
|
|
63
|
message.addRecipient(Message.RecipientType.BCC, new InternetAddress("openaire.test@gmail.com"));
|
64
|
|
65
|
// Set Subject: header field
|
66
|
message.setSubject(subject);
|
67
|
|
68
|
// For simple text setText() can be used instead of setContent()
|
69
|
|
70
|
// Send the actual HTML message, as big as you like
|
71
|
message.setContent(body, "text/html");
|
72
|
|
73
|
// Send message
|
74
|
Transport.send(message);
|
75
|
logger.debug("Sent message successfully....\n");
|
76
|
return true;
|
77
|
} catch (AddressException ae) {
|
78
|
logger.error("Email could not be send.", ae);
|
79
|
return false;
|
80
|
} catch (MessagingException me) {
|
81
|
logger.error("Email could not be send.", me);
|
82
|
return false;
|
83
|
}
|
84
|
}
|
85
|
|
86
|
}
|