Project

General

Profile

1
package eu.dnetlib.openaire.user.utils;
2

    
3
import org.apache.log4j.Logger;
4

    
5
import javax.mail.*;
6
import javax.mail.internet.InternetAddress;
7
import javax.mail.internet.MimeMessage;
8
import java.util.Properties;
9

    
10
/**
11
 * Created by kiatrop on 9/10/2017.
12
 */
13
public class EmailSender {
14

    
15
    private String username;
16
    private String password;
17
    private String host;
18
    private String port;
19
    private String from;
20
    private String auth;
21

    
22
    Logger logger = Logger.getLogger(EmailSender.class);
23

    
24
    public void sendEmail(String recipient, String subject, String body) throws MessagingException {
25

    
26
        // Get system properties
27
        Properties properties = System.getProperties();
28

    
29
        properties.put("mail.transport.protocol", "smtp");
30
        properties.put("mail.smtp.host", host);
31
        properties.put("mail.smtp.port", port);
32
        properties.put("mail.smtp.auth", auth);
33
        //Dev for sending email from gmail
34
        if(host.equals("smtp.gmail.com")) {
35
            properties.put("mail.smtp.ssl.protocols", "TLSv1.2");
36
            properties.put("mail.smtp.starttls.enable", "true");
37
            properties.put("mail.smtp.ssl.trust", "*");
38
        }
39
        Session session = javax.mail.Session.getInstance(properties,
40
                new Authenticator() {
41
                    protected PasswordAuthentication getPasswordAuthentication() {
42
                        return new PasswordAuthentication(username, password);
43
                    }
44
                });
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(from));
51

    
52
        // Set To: header field of the header.
53
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient));
54

    
55
        // Set Subject: header field
56
        message.setSubject(subject, "UTF-8");
57

    
58
        // For simple text setText() can be used instead of setContent()
59

    
60
        // Send the actual HTML message, as big as you like
61
        message.setContent(body, "text/html;charset=UTF-8");
62

    
63
        // Send message
64
        Transport.send(message);
65
        logger.debug("Sent message successfully....\n");
66

    
67
    }
68

    
69
    public String getUsername() {
70
        return username;
71
    }
72

    
73
    public void setUsername(String username) {
74
        this.username = username;
75
    }
76

    
77
    public String getPassword() {
78
        return password;
79
    }
80

    
81
    public void setPassword(String password) {
82
        this.password = password;
83
    }
84

    
85
    public String getHost() {
86
        return host;
87
    }
88

    
89
    public void setHost(String host) {
90
        this.host = host;
91
    }
92

    
93
    public String getPort() {
94
        return port;
95
    }
96

    
97
    public void setPort(String port) {
98
        this.port = port;
99
    }
100

    
101
    public String getFrom() {
102
        return from;
103
    }
104

    
105
    public void setFrom(String from) {
106
        this.from = from;
107
    }
108

    
109
    public String getAuth() {
110
        return auth;
111
    }
112

    
113
    public void setAuth(String auth) {
114
        this.auth = auth;
115
    }
116

    
117
}
(2-2/7)