11package org .myrobotlab .service ;
22
33import java .util .Date ;
4- import java .util .List ;
54import java .util .Properties ;
65
76import 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 );
0 commit comments