Last active
August 29, 2015 14:02
-
-
Save geissebn/83b98ecbd45946c79b27 to your computer and use it in GitHub Desktop.
demo code for the jadice knowledge base article at https://levigo.de/info/display/JKB/Senden+einer+E-Mail
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Copyright (c) 2015, levigo holding gmbh. | |
All rights reserved. | |
Redistribution and use in source and binary forms, with or without modification, | |
are permitted provided that the following conditions are met: | |
* Redistributions of source code must retain the above copyright notice, | |
this list of conditions and the following disclaimer. | |
* Redistributions in binary form must reproduce the above copyright notice, | |
this list of conditions and the following disclaimer in the documentation | |
and/or other materials provided with the distribution. | |
* Neither the name of levigo holding gmbh nor the names of its contributors | |
may be used to endorse or promote products derived from this software | |
without specific prior written permission. | |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | |
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | |
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | |
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | |
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
POSSIBILITY OF SUCH DAMAGE. | |
*/ | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.net.URI; | |
import java.util.concurrent.TimeUnit; | |
import javax.swing.JFileChooser; | |
import javax.swing.JOptionPane; | |
import org.apache.activemq.ActiveMQConnectionFactory; | |
import com.levigo.jadice.server.Job; | |
import com.levigo.jadice.server.Job.State; | |
import com.levigo.jadice.server.TimeLimit; | |
import com.levigo.jadice.server.client.jms.JMSJobFactory; | |
import com.levigo.jadice.server.misc.NullNode; | |
import com.levigo.jadice.server.nodes.ScriptNode; | |
import com.levigo.jadice.server.nodes.StreamInputNode; | |
import com.levigo.jadice.server.shared.types.StreamDescriptor; | |
import com.levigo.jadice.server.util.TraceListener; | |
public class SendMailDemo { | |
private static final String SERVER = "smtp.example.com"; | |
private static final String LOGIN = "SECRET"; | |
private static final String PASSWORD = "SECRET"; | |
public static void main(String[] args) { | |
try { | |
final JFileChooser chooser = new JFileChooser(); | |
chooser.setMultiSelectionEnabled(true); | |
chooser.setCurrentDirectory(new File(".")); | |
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { | |
sendFiles(chooser.getSelectedFiles()); | |
} | |
} catch (Throwable e) { | |
e.printStackTrace(); | |
JOptionPane.showMessageDialog(null, e.getMessage(), "Transmission failed", JOptionPane.ERROR_MESSAGE); | |
} | |
} | |
private static void sendFiles(File[] attachments) throws Throwable { | |
// Create a JobFactory that connect to the messaging system | |
final JMSJobFactory jobFactory = new JMSJobFactory( | |
new ActiveMQConnectionFactory("tcp://127.0.0.1:61616"), | |
JMSJobFactory.DEFAULT_QUEUE_NAME); | |
// Create and configure job | |
final Job job = jobFactory.createJob(); | |
job.setType("send mail demo"); | |
final StreamInputNode si = new StreamInputNode(); | |
final ScriptNode scriptNode = new ScriptNode(); | |
scriptNode.setScript(new URI("resource:/<PathTo>/SendMail.groovy")); | |
scriptNode.getParameters().put("host", SERVER); | |
scriptNode.getParameters().put("username", LOGIN); | |
scriptNode.getParameters().put("password", PASSWORD); | |
scriptNode.getParameters().put("from", "jadice-server@example.com"); | |
scriptNode.getParameters().put("to", "john.doe@example.com"); | |
job.attach( | |
si | |
.appendSuccessor(scriptNode) | |
// ScriptNode requires a successor even if there will be no data in this case | |
.appendSuccessor(new NullNode()) | |
); | |
job.addJobListener(new TraceListener()); | |
job.apply(new TimeLimit(120, TimeUnit.SECONDS)); | |
job.submit(); | |
try { | |
// Send the attachments | |
for (File f : attachments) { | |
System.out.println("Adding file " + f.getName()); | |
final StreamDescriptor sd = new StreamDescriptor(); | |
// Important: Set filename here so that the attachment will have a filename too | |
sd.setFileName(f.getName()); | |
si.addStream(new FileInputStream(f), sd); | |
} | |
} catch (Throwable th) { | |
job.abort(); | |
throw th; | |
} | |
si.complete(); | |
job.waitForTermination(-1); | |
if (job.getState() == State.FINISHED) { | |
System.out.println("Sent mail successfully"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment