Project

General

Profile

1
package eu.dnetlib.uoaadmintools.emailSender;
2

    
3
import org.apache.log4j.Logger;
4
import org.hibernate.validator.constraints.Email;
5
import org.springframework.beans.factory.annotation.Autowired;
6
import org.springframework.beans.factory.annotation.Configurable;
7
import org.springframework.beans.factory.annotation.Value;
8
import org.springframework.boot.context.properties.ConfigurationProperties;
9
import org.springframework.context.annotation.Bean;
10
import org.springframework.stereotype.*;
11
import org.springframework.stereotype.Service;
12
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
13

    
14
import javax.mail.*;
15
import javax.mail.internet.AddressException;
16
import javax.mail.internet.InternetAddress;
17
import javax.mail.internet.MimeMessage;
18
import java.text.SimpleDateFormat;
19
import java.util.*;
20

    
21
@Service
22
@Configurable
23
public class EmailSender {
24

    
25
    private static final Logger logger = Logger.getLogger(EmailSender.class);
26

    
27
    @Value( "${admintool.username}" )
28
    private String username = null;
29
    @Value( "${admintool.password}" )
30
    private String password = null;
31
    @Value( "${admintool.host}" )
32
    private String host = null;
33
    @Value( "${admintool.port}" )
34
    private String port = null;
35
    @Value( "${admintool.from}" )
36
    private String from = null;
37

    
38
    public boolean send(List<String> recipients, String subject, String body) {
39
        // Get system properties
40
        Properties properties = System.getProperties();
41
        properties.setProperty("mail.smtp.host", host);
42
        properties.put("mail.smtp.port", port);
43
        properties.put("mail.smtp.auth", "true"); //enable authentication
44
        properties.put("mail.smtp.starttls.enable", "true");
45
        logger.debug("Try to connect to mail sender with "+username);
46
        Session session = Session.getInstance(properties,
47
                new javax.mail.Authenticator() {
48
                    protected PasswordAuthentication getPasswordAuthentication() {
49
                        return new PasswordAuthentication(username, password);
50
                    }
51
                });
52

    
53
        try {
54
            logger.debug("Try to sent e-mail to "+recipients.toString()+
55
            "\nSubject: "+subject+
56
            "\nBody:"+body);
57

    
58
            // Create a default MimeMessage object.
59
            MimeMessage message = new MimeMessage(session);
60

    
61
            // Set From: header field of the header.
62
            message.setFrom(new InternetAddress(from));
63

    
64
            // Set To: header field of the header.
65
            for(String to : recipients) {
66
                message.addRecipient(Message.RecipientType.BCC, new InternetAddress(to));
67
            }
68
            // Set Subject: header field
69
            message.setSubject(subject);
70

    
71
            // For simple text setText() can be used instead of setContent()
72

    
73
            // Send the actual HTML message, as big as you like
74
            message.setContent(body, "text/html");
75

    
76
            // Send message
77
            Transport.send(message);
78
            logger.debug("Sent message successfully....\n");
79
            return true;
80
        } catch (AddressException ae) {
81
            logger.error("Email could not be send.", ae);
82
            return false;
83
        } catch (MessagingException me) {
84
            logger.error("Email could not be send.", me);
85
            return false;
86
        }
87
    }
88

    
89
}
    (1-1/1)