This repository has been archived by the owner on Mar 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
/
EmailUpdateHandler.java
149 lines (127 loc) · 4.24 KB
/
EmailUpdateHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*******************************************************************************
* Copyright (c) 2019- UT-Battelle, LLC.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Initial API and implementation and/or initial documentation -
* Jay Jay Billings, Joe Osborn
*******************************************************************************/
package org.eclipse.ice.commands;
import java.io.IOException;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
/**
* This class provides email updates for Command job statuses. It takes a
* provided email address, password, etc. and just sends an email to the same
* address
*
* @author Joe Osborn
*
*/
public class EmailUpdateHandler implements ICommandUpdateHandler {
// The email address to send the message to
private String emailAddress = "";
// The text that the message should contain
private String emailText = "";
// The subject of the message
private String emailSubject = "Commands API Message";
// The host smtp server for the email address
private String emailHost = "";
// The password for the provided email to be able to send to itself
private String emailPassword = "";
/**
* Default constructor
*/
public EmailUpdateHandler() {
}
/**
* See {@link org.eclipse.ice.commands.ICommandUpdateHandler#postUpdate()}
*/
@Override
public void postUpdate() throws IOException {
// Create some properties and setup the default gmail
// server properties
Properties properties = System.getProperties();
properties.put("mail.smtp.port", "25");
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true"); // TLS
// Setup mail server
properties.setProperty("mail.smtp.host", emailHost);
// Get the default Session object.
Session session = Session.getInstance(properties, new javax.mail.Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(emailAddress, emailPassword);
}
});
// Set session to debug just to explicit
session.setDebug(true);
try {
// Create a default MimeMessage object
MimeMessage message = new MimeMessage(session);
// Set the sender and recipient of the email
message.setFrom(new InternetAddress(emailAddress));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(emailAddress));
// Give the email a subject and message content
message.setSubject(emailSubject);
message.setText(emailText);
Transport.send(message);
logger.info("Email sent successfully");
} catch (MessagingException mex) {
logger.error("Couldn't send email.", mex);
throw new IOException();
}
}
/**
* Setter for the email subject, see
* {@link org.eclipse.ice.commands.notification.EmailUpdateHandler#emailSubject}
*
* @param emailSubject
*/
public void setSubject(String emailSubject) {
this.emailSubject = emailSubject;
}
/**
* Setter for the email address, see
* {@link org.eclipse.ice.commands.notification.EmailUpdateHandler#emailAddress}
*
* @param option
*/
public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}
/**
* Setter for the email host for email authentication, see
* {@link org.eclipse.ice.commands.notification.EmailUpdateHandler#emailHost}
*
* @param emailPassword
*/
public void setSmtpHost(String emailHost) {
this.emailHost = emailHost;
}
/**
* Setter for the email password for email authentication, see
* {@link org.eclipse.ice.commands.notification.EmailUpdateHandler#emailPassword}
*
* @param emailPassword
*/
public void setPassword(String emailPassword) {
this.emailPassword = emailPassword;
}
/**
* See {@link org.eclipse.ice.commands.ICommandUpdateHandler#setMessage(String)}
*/
@Override
public void setMessage(String message) {
this.emailText = message;
}
}