Project

General

Profile

« Previous | Next » 

Revision 53566

change mail controller method names, mail sender returns boolean to check if mail send or not

View differences:

modules/uoa-admin-tools/src/main/java/eu/dnetlib/uoaadmintools/controllers/EmailController.java
29 29
    //notify about new manager - check manager option
30 30
    //notify about new subscribers - check manager option
31 31
    //notify for claims - check manager option --> claim API
32
    @RequestMapping(value = "/email", method = RequestMethod.POST)
33
    public Boolean sendEmail(@RequestBody Email email ) throws Exception {
32
    @RequestMapping(value = "/sendMail", method = RequestMethod.POST)
33
    public Integer sendEmail(@RequestBody Email email ) {
34
        int send = 0;
34 35
        for(String userMail:email.getRecipients()){
35 36
            ArrayList<String> sendTo = new ArrayList<>();
36 37
            sendTo.add(userMail);
37
            emailSender.send(sendTo,email.getSubject(),email.getBody());
38
            boolean success =emailSender.send(sendTo,email.getSubject(),email.getBody());
39
            if(success){
40
                send ++;
41
            }
38 42
        }
43
        return send;
39 44

  
40
        return true;
41 45
    }
42 46

  
43
    @RequestMapping(value = "/notifyNewManagers/{pid}", method = RequestMethod.POST)
47
    @RequestMapping(value = "/notifyForNewManagers/{pid}", method = RequestMethod.POST)
44 48
    public Boolean notifyNewManagers(@PathVariable(value = "pid") String pid,@RequestBody Email email ) throws Exception {
45 49
        List<String> notifyrecipients = new ArrayList<String>();
46 50
        if(communityDAO.findByPid(pid) == null){
......
54 58
            }
55 59
        }
56 60
        if(notifyrecipients.size() > 0){
57
            emailSender.send(notifyrecipients,email.getSubject(),email.getBody());
61
            return emailSender.send(notifyrecipients,email.getSubject(),email.getBody());
58 62
        }else{
59 63
            log.debug("There are no users to notify ");
60 64
        }
61 65

  
62 66
        return true;
63 67
    }
64
    @RequestMapping(value = "/notifyNewSubscribers/{pid}", method = RequestMethod.POST)
68
    @RequestMapping(value = "/notifyForNewSubscribers/{pid}", method = RequestMethod.POST)
65 69
    public Boolean notifyNewSubscribers(@PathVariable(value = "pid") String pid,@RequestBody Email email ) throws Exception {
66 70
        List<String> notifyrecipients = new ArrayList<String>();
67 71
        if(communityDAO.findByPid(pid) == null){
......
75 79
            }
76 80
        }
77 81
        if(notifyrecipients.size() > 0){
78
            emailSender.send(notifyrecipients,email.getSubject(),email.getBody());
82
            return emailSender.send(notifyrecipients,email.getSubject(),email.getBody());
79 83
        }else{
80 84
            log.debug("There are no users to notify ");
81 85
        }
modules/uoa-admin-tools/src/main/java/eu/dnetlib/uoaadmintools/emailSender/EmailSender.java
35 35
    @Value( "${admintool.from}" )
36 36
    private String from = null;
37 37

  
38
    public void send(List<String> recipients, String subject, String body) {
38
    public boolean send(List<String> recipients, String subject, String body) {
39 39
        // Get system properties
40 40
        Properties properties = System.getProperties();
41 41
        properties.setProperty("mail.smtp.host", host);
42 42
        properties.put("mail.smtp.port", port);
43 43
        properties.put("mail.smtp.auth", "true"); //enable authentication
44 44
        properties.put("mail.smtp.starttls.enable", "true");
45

  
45
        logger.debug("Try to connect to mail sender with "+username);
46 46
        Session session = Session.getInstance(properties,
47 47
                new javax.mail.Authenticator() {
48 48
                    protected PasswordAuthentication getPasswordAuthentication() {
......
76 76
            // Send message
77 77
            Transport.send(message);
78 78
            logger.debug("Sent message successfully....\n");
79

  
79
            return true;
80 80
        } catch (AddressException ae) {
81 81
            logger.error("Email could not be send.", ae);
82

  
82
            return false;
83 83
        } catch (MessagingException me) {
84 84
            logger.error("Email could not be send.", me);
85
            return false;
85 86
        }
86 87
    }
87 88

  

Also available in: Unified diff