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
        //Dev for sending email from gmail
33
        if(host.equals("smtp.gmail.com")) {
34
            properties.put("mail.smtp.auth", "true"); //enable authentication
35
            properties.put("mail.smtp.starttls.enable", "true");
36
            properties.put("mail.smtp.ssl.trust", "*");
37
        } else {
38
            properties.put("mail.smtp.auth", "false");
39
        }
40
        Session session = javax.mail.Session.getInstance(properties,
41
                new Authenticator() {
42
                    protected PasswordAuthentication getPasswordAuthentication() {
43
                        return new PasswordAuthentication(username, password);
44
                    }
45
                });
46

    
47
        // Create a default MimeMessage object.
48
        MimeMessage message = new MimeMessage(session);
49

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

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

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

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

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

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

    
68
    }
69

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

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

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

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

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

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

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

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

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

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

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

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

    
118
}
(2-2/7)