Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4500b0a

Browse files
author
GroG
committed
email service
1 parent 85f500b commit 4500b0a

File tree

6 files changed

+163
-76
lines changed

6 files changed

+163
-76
lines changed

‎.github/workflows/build.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
run: sudo apt-get install -y libv4l-0 ffmpeg
2727

2828
- name: Clear Local Maven Cache
29-
run: rm -rf ~/.m2/repository
29+
run: rm -rf ~/.m2/repository && rm -f ./libraries/serviceData.json
3030

3131
- name: Dependency Test
3232
run: mvn test -Dtest=org.myrobotlab.framework.DependencyTest -q

‎src/main/java/org/myrobotlab/framework/interfaces/ServiceInterface.java‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
import org.myrobotlab.framework.Inbox;
1010
import org.myrobotlab.framework.MRLListener;
11-
import org.myrobotlab.framework.MethodCache;
1211
import org.myrobotlab.framework.MethodEntry;
1312
import org.myrobotlab.framework.Outbox;
1413
import org.myrobotlab.framework.Peer;

‎src/main/java/org/myrobotlab/service/Email.java‎

Lines changed: 86 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.myrobotlab.service;
22

33
import java.util.Date;
4-
import java.util.List;
54
import java.util.Properties;
65

76
import javax.activation.DataHandler;
@@ -82,11 +81,23 @@ public Properties setGmailProps(String user, String password) {
8281
public void onImage(ImageData img) {
8382
sendHtmlMail(null, null, img.src, null, img.src);
8483
}
84+
85+
public void sendImage(String imageFile) {
86+
sendHtmlMail(null, null, imageFile, null, imageFile);
87+
}
8588

8689
public void sendImage(String to, String imageFile) {
8790
sendHtmlMail(null, to, imageFile, null, imageFile);
8891
}
8992

93+
public void sendEmail(String to, String subject, String body) {
94+
sendEmail(to, subject, body, "html", null);
95+
}
96+
97+
public void sendEmail(String to, String subject, String body, String imageFile) {
98+
sendEmail(to, subject, body, "html", imageFile);
99+
}
100+
90101
/**
91102
*
92103
* @param to
@@ -95,8 +106,12 @@ public void sendImage(String to, String imageFile) {
95106
* @param imageFile
96107
*/
97108

98-
public void sendMail(String to, String subject, String body, String imageFile) {
99-
sendTextMail((String) props.get("mail.smtp.user"), to, subject, body, config.format, null);
109+
public void sendEmail(String to, String subject, String body, String format, String imageFile) {
110+
if ("text".equals(format)) {
111+
sendTextMail((String) props.get("mail.smtp.user"), to, subject, body, imageFile);
112+
} else {
113+
sendHtmlMail((String) props.get("mail.smtp.user"), to, subject, body, imageFile);
114+
}
100115
}
101116

102117
public void sendHtmlMail(String from, String to, String subject, String body, String imageFileName) {
@@ -106,12 +121,14 @@ public void sendHtmlMail(String from, String to, String subject, String body, St
106121
body = "";
107122
}
108123

109-
EmailConfig config = (EmailConfig) this.config;
110-
111124
if (to == null) {
112125
to = config.to;
113126
}
114127

128+
if (from == null) {
129+
from = config.from;
130+
}
131+
115132
Session session = Session.getDefaultInstance(props);
116133

117134
// Create a default MimeMessage object.
@@ -128,14 +145,16 @@ public void sendHtmlMail(String from, String to, String subject, String body, St
128145
multipart.addBodyPart(messageBodyPart);
129146

130147
// second part (the image)
131-
messageBodyPart = new MimeBodyPart();
132-
DataSource fds = new FileDataSource(imageFileName);
148+
if (imageFileName != null && !imageFileName.isEmpty()) {
149+
messageBodyPart = new MimeBodyPart();
150+
DataSource fds = new FileDataSource(imageFileName);
133151

134-
messageBodyPart.setDataHandler(new DataHandler(fds));
135-
messageBodyPart.setHeader("Content-ID", "<image>");
152+
messageBodyPart.setDataHandler(new DataHandler(fds));
153+
messageBodyPart.setHeader("Content-ID", "<image>");
136154

137-
// add image to the multipart
138-
multipart.addBodyPart(messageBodyPart);
155+
// add image to the multipart
156+
multipart.addBodyPart(messageBodyPart);
157+
}
139158

140159
// put everything together
141160
msg.setContent(multipart);
@@ -145,7 +164,7 @@ public void sendHtmlMail(String from, String to, String subject, String body, St
145164
// creates a new e-mail message
146165
// Message msg = new MimeMessage(session);
147166

148-
if (from != null) {
167+
if (from != null && !from.isEmpty()) {
149168
msg.setFrom(new InternetAddress(from));
150169
}
151170
InternetAddress[] toAddresses = { new InternetAddress(to) };
@@ -156,12 +175,6 @@ public void sendHtmlMail(String from, String to, String subject, String body, St
156175
// msg.setText(body);
157176

158177
Transport t = session.getTransport("smtp");
159-
// t.connect(host, userName, password);
160-
// t.connect();
161-
// t.connect(host, userName, password);
162-
// THIS IS RIDICULOUS - THEY SUPPLY A BAJILLION SESSION PROPS INCLUDING
163-
// USERNAME & PASSWORD
164-
// BUT THEY HAVE TO BE PULLED BACK OUT IN ORDER TO DO THE TRANSPORT ???
165178

166179
String user = props.getProperty("mail.smtp.user");
167180
String password = props.getProperty("mail.smtp.pass");
@@ -180,17 +193,20 @@ public void sendHtmlMail(String from, String to, String subject, String body, St
180193
error("port must be set");
181194
}
182195

196+
log.info("connecting to host");
183197
t.connect(host, Integer.parseInt(port), user, password);
198+
log.info("sending email message");
184199

185200
t.sendMessage(msg, msg.getAllRecipients());
201+
log.info("closing session");
186202
t.close();
187203
} catch (Exception e) {
188204
error(e);
189205
}
190206

191207
}
192208

193-
public void sendTextMail(String from, String to, String subject, String body, String format, List<Object> attachments) {
209+
public void sendTextMail(String from, String to, String subject, String body, String attachment) {
194210
try {
195211

196212
Session session = Session.getDefaultInstance(props);
@@ -245,15 +261,64 @@ public void sendTextMail(String from, String to, String subject, String body, St
245261

246262
}
247263

264+
/**
265+
* Load the config into memory
266+
*/
267+
@Override
268+
public EmailConfig getConfig() {
269+
super.getConfig();
270+
271+
config.auth = (String) props.getOrDefault("mail.smtp.auth", "true");
272+
config.debug = (String) props.getOrDefault("mail.smtp.debug", "true");
273+
config.host = (String) props.getOrDefault("mail.smtp.host", config.host);
274+
config.pass = (String) props.getOrDefault("mail.smtp.pass", config.pass);
275+
config.port = (String) props.getOrDefault("mail.smtp.port", config.port);
276+
config.protocols = (String) props.getOrDefault("mail.smtp.ssl.protocols", "TLSv1.2");
277+
config.socketFactory = (String) props.getOrDefault("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
278+
config.starttlsEnabled = (String) props.getOrDefault("mail.smtp.starttls.enable", "true");
279+
config.starttlsRequired = (String) props.getOrDefault("mail.smtp.starttls.required", "true");
280+
config.user = (String) props.getOrDefault("mail.smtp.user", config.user);
281+
282+
return config;
283+
}
284+
285+
/**
286+
* Applies the config to the service by unloading the config object data into
287+
* props.
288+
*/
289+
@Override
290+
public EmailConfig apply(EmailConfig c) {
291+
super.apply(c);
292+
293+
props.put("mail.smtp.auth", c.auth);
294+
props.put("mail.smtp.debug", c.debug);
295+
props.put("mail.smtp.host", c.host);
296+
props.put("mail.smtp.pass", c.pass);
297+
props.put("mail.smtp.port", c.port);
298+
props.put("mail.smtp.ssl.protocols", c.protocols);
299+
props.put("mail.smtp.socketFactory.class", c.socketFactory);
300+
props.put("mail.smtp.starttls.enable", c.starttlsEnabled);
301+
props.put("mail.smtp.starttls.required", c.starttlsRequired);
302+
props.put("mail.smtp.user", c.user);
303+
304+
return c;
305+
}
306+
248307
public static void main(String[] args) {
249308
try {
250309

310+
Runtime runtime = Runtime.getInstance();
251311
LoggingFactory.init(Level.INFO);
252312
Runtime.start("webgui", "WebGui");
253313
Runtime.start("python", "Python");
254314
Email email = (Email) Runtime.start("email", "Email");
255-
email.setGmailProps("supertick@gmail.com", "XXXXXXXXXX");
256-
email.sendImage("supertick@gmail.com", "data/OpenCV/i01.opencv-00136.png");
315+
// email.setGmailProps("supertick@gmail.com", "XXXXXX");
316+
// email.save();
317+
// email.sendImage("supertick@gmail.com", "data/OpenCV/cv-01991.png");
318+
email.sendEmail(fs, fs, fs, fs);
319+
email.sendHtmlMail("worky@gmail.com", "supertick@gmail.com", "Test", "Hello there !", "data/OpenCV/cv-01991.png");
320+
// Runtime.saveConfig("non-default");
321+
// runtime.save();
257322

258323
} catch (Exception e) {
259324
log.error("main threw", e);

‎src/main/java/org/myrobotlab/service/config/EmailConfig.java‎

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,21 @@
55
*/
66
public class EmailConfig extends ServiceConfig {
77

8-
public String to; // if set sends auto
9-
public String format = "text/html"; // text/html or text/plain
10-
public String user = null;
11-
public String host = null;
12-
public int port = 25; /* 465, 587 */
8+
// javax email props are so inane they don't preserve typeness
9+
// and error if typed
10+
11+
public String auth = "true";
12+
public String debug = "true";
13+
public String format = "html"; // text/html or text/plain
1314
public String from = null;
14-
boolean auth = true;
15-
boolean starttls = true;
16-
boolean debug = true;
17-
boolean starttlsRequired = true;
18-
String protocols = "TLSv1.2";
19-
String socketFactory = "javax.net.ssl.SSLSocketFactory";
20-
15+
public String host = null;
2116
public String pass = null;
17+
public String protocols = "TLSv1.2";
18+
public String port = "25"; /* 465, 587 */
19+
public String socketFactory = "javax.net.ssl.SSLSocketFactory";
20+
public String starttlsEnabled = "true";
21+
public String starttlsRequired = "true";
22+
public String to; // if set sends auto
23+
public String user = null;
2224

2325
}

‎src/main/resources/resource/WebGui/app/service/js/EmailGui.js‎

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ angular.module("mrlapp.service.EmailGui", []).controller("EmailGuiCtrl", [
3131
}
3232

3333
// Email data
34-
$scope.emailRecipient = ""
3534
$scope.emailMessage = ""
3635
$scope.attachment = null
3736

@@ -44,7 +43,7 @@ angular.module("mrlapp.service.EmailGui", []).controller("EmailGuiCtrl", [
4443

4544
// Send email function (placeholder)
4645
$scope.sendEmail = function () {
47-
if (!$scope.emailRecipient.trim()) {
46+
if (!$scope.service.config.to.trim()) {
4847
alert("Please enter a recipient email.")
4948
return
5049
}
@@ -55,14 +54,14 @@ angular.module("mrlapp.service.EmailGui", []).controller("EmailGuiCtrl", [
5554
}
5655

5756
console.log("Sending email...")
58-
console.log("To:", $scope.emailRecipient)
57+
console.log("To:", $scope.service.config.to)
5958
console.log("Message:", $scope.emailMessage)
6059
console.log("SMTP Config:", $scope.service.props)
6160
if ($scope.attachment) {
6261
console.log("Attachment:", $scope.attachment.name)
6362
}
64-
65-
alert("Email Sent Successfully! (This is a placeholder function)")
63+
// msg.send("broadcastState")
64+
msg.send("sendEmail",$scope.service.config.to,$scope.emailSubject,$scope.emailMessage,$scope.emailFormat,$scope.attachment)
6665
}
6766

6867
// GOOD TEMPLATE TO FOLLOW
@@ -81,23 +80,14 @@ angular.module("mrlapp.service.EmailGui", []).controller("EmailGuiCtrl", [
8180
_self.updateState(data)
8281
$scope.$apply()
8382
break
84-
case "onTime":
85-
const date = new Date(data)
86-
$scope.onTime = date.toLocaleString()
87-
$scope.$apply()
88-
break
89-
case "onEpoch":
90-
$scope.onEpoch = data
91-
$scope.$apply()
92-
break
9383
default:
9484
console.error("ERROR - unhandled method " + $scope.name + " " + inMsg.method)
9585
break
9686
}
9787
}
9888

99-
msg.subscribe("publishTime")
100-
msg.subscribe("publishEpoch")
89+
// msg.subscribe("publishTime")
90+
// msg.subscribe("publishEpoch")
10191
msg.subscribe(this)
10292
},
10393
])

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /