Project

General

Profile

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

    
3
import javax.mail.Message;
4
import javax.mail.MessagingException;
5
import javax.mail.PasswordAuthentication;
6
import javax.mail.Session;
7
import javax.mail.Transport;
8
import javax.mail.internet.AddressException;
9
import javax.mail.internet.InternetAddress;
10
import javax.mail.internet.MimeMessage;
11
import java.util.Properties;
12

    
13
/**
14
 * Created by antleb on 3/9/15.
15
 */
16
public class MailLibrary {
17
	private String mailhost = null;
18
	private String from = null;
19
	private String replyTo = null;
20
	private int smtpPort = 25;
21
	private String username = null;
22
	private String password = null;
23
	private boolean authenticate = false;
24
	private String mode = "plain";
25
	boolean debug = false;
26

    
27
	private Properties props = null;
28

    
29
	public void init() {
30
		props = new Properties();
31

    
32
		if (mode.equals("plain")) {
33
			props.put("mail.transport.protocol", "smtp");
34

    
35
			props.put("mail.smtp.host", mailhost);
36
			props.put("mail.smtp.port", smtpPort);
37
		} else if (mode.equals("ssl")) {
38
			props.put("mail.transport.protocol", "smtps");
39

    
40
			props.put("mail.smtps.host", mailhost);
41
			props.put("mail.smtps.port", smtpPort);
42
			props.put("mail.smtps.auth", "true");
43
			props.put("mail.smtps.user", username);
44
			props.put("mail.smtps.password", password);
45
		} else {
46
			throw new IllegalArgumentException("Unkown mode '" + mode + "'");
47
		}
48

    
49
		props.setProperty("mail.debug", this.debug + "");
50
	}
51

    
52
	public void sendEmail(String email, String subject, String text) throws AddressException, MessagingException {
53
		sendEmail(new String[] { email }, subject, text);
54
	}
55

    
56
	public void sendEmail(String[] recipients, String subject, String text) throws AddressException, MessagingException {
57
		Session session = Session.getInstance(props,
58
				(this.authenticate ? new SMTPAuthenticator() : null));
59

    
60
		session.setDebug(debug);
61

    
62
		Message msg = new MimeMessage(session);
63

    
64
		msg.setFrom(new InternetAddress(this.from));
65
		msg.setReplyTo(new InternetAddress[] { new InternetAddress(replyTo) });
66
		msg.setSubject(subject);
67
		msg.setContent(text, "text/plain");
68

    
69
		for (String recipient : recipients)
70
			msg.addRecipient(javax.mail.Message.RecipientType.TO,
71
					new InternetAddress(recipient));
72

    
73
		Transport tr = session.getTransport();
74

    
75
		if (authenticate) {
76
			tr.connect(mailhost, smtpPort, username, password);
77
		} else {
78
			tr.connect();
79
		}
80

    
81
		tr.sendMessage(msg, msg.getAllRecipients());
82
		tr.close();
83
	}
84

    
85
	private class SMTPAuthenticator extends javax.mail.Authenticator {
86
		public PasswordAuthentication getPasswordAuthentication() {
87
			return new PasswordAuthentication(username, password);
88
		}
89
	}
90

    
91
	public String getMailhost() {
92
		return mailhost;
93
	}
94

    
95
	public void setMailhost(String mailhost) {
96
		this.mailhost = mailhost;
97
	}
98

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

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

    
107
	public String getReplyTo() {
108
		return replyTo;
109
	}
110

    
111
	public void setReplyTo(String replyTo) {
112
		this.replyTo = replyTo;
113
	}
114

    
115
	public int getSmtpPort() {
116
		return smtpPort;
117
	}
118

    
119
	public void setSmtpPort(int smtpPort) {
120
		this.smtpPort = smtpPort;
121
	}
122

    
123
	public String getUsername() {
124
		return username;
125
	}
126

    
127
	public void setUsername(String username) {
128
		this.username = username;
129
	}
130

    
131
	public String getPassword() {
132
		return password;
133
	}
134

    
135
	public void setPassword(String password) {
136
		this.password = password;
137
	}
138

    
139
	public boolean isAuthenticate() {
140
		return authenticate;
141
	}
142

    
143
	public void setAuthenticate(boolean authenticate) {
144
		this.authenticate = authenticate;
145
	}
146

    
147
	public String getMode() {
148
		return mode;
149
	}
150

    
151
	public void setMode(String mode) {
152
		this.mode = mode;
153
	}
154

    
155
	public boolean isDebug() {
156
		return debug;
157
	}
158

    
159
	public void setDebug(boolean debug) {
160
		this.debug = debug;
161
	}
162
}
(4-4/5)