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
    @Value( "${admintool.auth}" )
38
    private String auth = null;
39

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

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

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

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

    
65
            // Set To: header field of the header.
66
            if(!bcc) {
67
                for (String to : recipients) {
68
                    message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
69
                }
70
            }else{
71
                for (String to : recipients) {
72
                    message.addRecipient(Message.RecipientType.BCC, new InternetAddress(to));
73
                }
74
            }
75

    
76
            message.addRecipient(Message.RecipientType.BCC, new InternetAddress("openaire.test@gmail.com"));
77

    
78
            // Set Subject: header field
79
            message.setSubject(subject);
80

    
81
            // For simple text setText() can be used instead of setContent()
82

    
83
            // Send the actual HTML message, as big as you like
84
            message.setContent(body, "text/html");
85

    
86
            // Send message
87
            Transport.send(message);
88
            logger.debug("Sent message successfully....\n");
89
            return true;
90
        } catch (AddressException ae) {
91
            logger.error("Email could not be send.", ae);
92
            return false;
93
        } catch (MessagingException me) {
94
            logger.error("Email could not be send.", me);
95
            return false;
96
        }
97
    }
98

    
99
}
    (1-1/1)