Project

General

Profile

1
package eu.dnetlib.goldoa.service.utils;
2

    
3
import org.apache.commons.logging.Log;
4
import org.apache.commons.logging.LogFactory;
5
import org.springframework.stereotype.Service;
6

    
7
import javax.mail.*;
8
import javax.mail.internet.AddressException;
9
import javax.mail.internet.InternetAddress;
10
import javax.mail.internet.MimeMessage;
11
import java.io.IOException;
12
import java.util.Properties;
13

    
14
/**
15
 * Created by antleb on 3/9/15.
16
 */
17
@Service
18
public class MailLibrary {
19

    
20
	private String mailhost;
21
	private String from ;
22
	private String replyTo;
23
	private String smtpPort;
24
	private String username;
25
	private String password;
26
	private String authenticate ;
27
	private String mode ;
28
	private String debug ;
29

    
30
	private Properties props ;
31

    
32
	private Log log = LogFactory.getLog(MailLibrary.class);
33

    
34
	public void init() {
35
		props = new Properties();
36

    
37
		log.debug("mode : "  + mode);
38

    
39
		if (mode.equals("plain")) {
40
			props.put("mail.transport.protocol", "smtp");
41

    
42
			props.put("mail.smtp.host", mailhost);
43
			props.put("mail.smtp.port", smtpPort);
44
		} else if (mode.equals("ssl")) {
45
			props.put("mail.transport.protocol", "smtps");
46

    
47
			props.put("mail.smtps.host", mailhost);
48
			props.put("mail.smtps.port", smtpPort);
49
			props.put("mail.smtps.auth", "true");
50
			props.put("mail.smtps.user", username);
51
			props.put("mail.smtps.password", password);
52
		} else {
53
			throw new IllegalArgumentException("Unkown mode '" + mode + "'");
54
		}
55

    
56
		props.setProperty("mail.debug", this.debug + "");
57
	}
58

    
59
	public void sendEmail(String email, String subject, String text) throws AddressException, MessagingException {
60
		sendEmail(new String[]{email}, subject, text);
61
	}
62

    
63
	public void sendEmail(String[] recipients, String subject, String text) throws AddressException, MessagingException {
64

    
65
		Session session = Session.getInstance(props,
66
				(Boolean.parseBoolean(this.authenticate) ? new SMTPAuthenticator() : null));
67

    
68
		session.setDebug(Boolean.parseBoolean(this.debug));
69

    
70
		Message msg = new MimeMessage(session);
71

    
72
		msg.setFrom(new InternetAddress(this.from));
73
		msg.setReplyTo(new InternetAddress[]{new InternetAddress(replyTo)});
74
		msg.setSubject(subject);
75
		msg.setContent(text, "text/plain");
76

    
77

    
78
		for (String recipient : recipients)
79
			msg.addRecipient(javax.mail.Message.RecipientType.TO,
80
					new InternetAddress(recipient));
81

    
82
		Transport tr = session.getTransport();
83

    
84
		if (Boolean.parseBoolean(this.authenticate)) {
85
			tr.connect(mailhost, Integer.parseInt(smtpPort), username, password);
86
		} else {
87
			tr.connect();
88
		}
89
		try {
90

    
91
			System.out.println(msg.getContent().toString());
92
		} catch (IOException e) {
93
			e.printStackTrace();
94
		}
95
		tr.sendMessage(msg, msg.getAllRecipients());
96
		tr.close();
97
	}
98

    
99
	private class SMTPAuthenticator extends javax.mail.Authenticator {
100
		public PasswordAuthentication getPasswordAuthentication() {
101
			return new PasswordAuthentication(username, password);
102
		}
103
	}
104

    
105
	public String getMailhost() {
106
		return mailhost;
107
	}
108

    
109
	public void setMailhost(String mailhost) {
110
		this.mailhost = mailhost;
111
	}
112

    
113
	public String getFrom() {
114
		return from;
115
	}
116

    
117
	public void setFrom(String from) {
118
		this.from = from;
119
	}
120

    
121
	public String getReplyTo() {
122
		return replyTo;
123
	}
124

    
125
	public void setReplyTo(String replyTo) {
126
		this.replyTo = replyTo;
127
	}
128

    
129
	public String getSmtpPort() {
130
		return smtpPort;
131
	}
132

    
133
	public void setSmtpPort(String smtpPort) {
134
		this.smtpPort = smtpPort;
135
	}
136

    
137
	public String getUsername() {
138
		return username;
139
	}
140

    
141
	public void setUsername(String username) {
142
		this.username = username;
143
	}
144

    
145
	public String getPassword() {
146
		return password;
147
	}
148

    
149
	public void setPassword(String password) {
150
		this.password = password;
151
	}
152

    
153
	public String isAuthenticate() {
154
		return authenticate;
155
	}
156

    
157
	public void setAuthenticate(String authenticate) {
158
		this.authenticate = authenticate;
159
	}
160

    
161
	public String getMode() {
162
		return mode;
163
	}
164

    
165
	public void setMode(String mode) {
166
		this.mode = mode;
167
	}
168

    
169
	public String isDebug() {
170
		return debug;
171
	}
172

    
173
	public void setDebug(String debug) {
174
		this.debug = debug;
175
	}
176
}
(5-5/7)