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.starttls.enable", "true");
36
            properties.put("mail.smtp.ssl.trust", "*");
37
        }
38
        Session session = javax.mail.Session.getInstance(properties,
39
                new Authenticator() {
40
                    protected PasswordAuthentication getPasswordAuthentication() {
41
                        return new PasswordAuthentication(username, password);
42
                    }
43
                });
44

    
45
        // Create a default MimeMessage object.
46
        MimeMessage message = new MimeMessage(session);
47

    
48
        // Set From: header field of the header.
49
        message.setFrom(new InternetAddress(from));
50

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

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

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

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

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

    
66
    }
67

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

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

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

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

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

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

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

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

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

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

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

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

    
116
}
(2-2/7)