0

SendGrid provides a Java example of using their service to send email. I'd like to use this in a little CF application I'm working on but not sure how to make a Coldfusion reference to a Java Library (https://sendgrid.com/docs/Code_Examples/java.html).

If someone has a code snippet I could review or better yet a reference link on calling a java code snippet (assuming that is what it is called), I'd appreciate it. Otherwise, I have no idea on how to get started with this. I do not know how to compile a class file so please be patient.

UPDATED 12/23:
Recap of some of the comments below that I've done since posting the question

Error when calling the cfm page:

Object Instantiation Exception.  
Class not found: com.sendgrid.SendGrid.Email
The error occurred in C:/ColdFusion9/wwwroot/SendGrid/index.cfm: line 4
2 : 
3 : oSendGrid = createObject("java", "com.sendgrid.SendGrid").init("fakeusername", "fakepassword");
4 : oEmail = createObject("java", "com.sendgrid.SendGrid.Email").init();
5 : 
6 : // e-mail details`
HPWD
  • 2,232
  • 4
  • 31
  • 61
  • Tristan already figured out the problem, but for next time - the *"Object Instantiation Exception."* message is just boilerplate stuff. With java objects, you have to look at the stack trace to find the true "cause" of the error. – Leigh Dec 23 '14 at 19:54

1 Answers1

2

Going based on what's available from their API, here's a good starting point. Notice that you will be required to put their JAR file into the classpath of your CF instance. Optionally, you can use a JavaLoader if putting a JAR if CF's classpath isn't a possibility:

oSendGrid = createObject("java", "com.sendgrid.SendGrid").init("sendgrid_username", "sendgrid_password");
oEmail = createObject("java", "com.sendgrid.SendGrid$Email").init();

// e-mail details
oEmail.addTo("example@example.com");
oEmail.addToName("Example Guy");
oEmail.setFrom("other@example.com");
oEmail.setSubject("Hello World");
oEmail.setText("My first email through SendGrid");

// send the e-mail
oSendGrid.send(oEmail);
Tristan Lee
  • 1,210
  • 10
  • 17
  • I'd be able to add the JAR to CF. In my mind, I'm probably over complicating this. Basically, (1) I'd drop the jar file in my cf folder, (2) then wrap the code like what you have above in a cfscript, (3) save the file in my components folder, (4) than they just make the appropriate call to the method passing in the required attributes. Does that about sum it up? – HPWD Dec 22 '14 at 20:23
  • Sounds like something worth trying. – Dan Bracuk Dec 22 '14 at 20:39
  • CF10 also allows for dynamic library loading within your Application.cfc. So if you can't (or don't want to) place the JAR in CF's `lib/` directory, you can place is where you please and load it dynamically: http://help.adobe.com/en_US/ColdFusion/10.0/Developing/WSe61e35da8d318518-106e125d1353e804331-7ffd.html – Tristan Lee Dec 22 '14 at 20:45
  • @TristanLee are the steps I listed in my comment under your solution about right? – HPWD Dec 22 '14 at 22:09
  • Yes, that should get your fairly close at least :) – Tristan Lee Dec 22 '14 at 22:12
  • 1
    @dlackey - NB: If *physically* copy the jar file into your CF class path, ie to `lib/` or preferably `WEB-INF\lib`, be sure to restart the CF server afterward, so the new jar is detected. If you skip that step, it will not work. – Leigh Dec 23 '14 at 00:29
  • I've updated the sample code Tristan Lee posted to keep it simple for now using hard coded values. I then downloaded the java file from sendgrid and saved it in my CF9\wwwroot\web-inf\lib directory. Next, I restarted CF Service. After doing these steps, I'm getting `Object Instantiation Exception. Class not found: com.sendgrid.Email` error message. I'm not sure what to do next. Googling now but hope to hear back from someone on the next step. – HPWD Dec 23 '14 at 14:00
  • I'm thinking maybe the 'com.sendgrid.xxx' is the wrong path if this works like CFCs. – HPWD Dec 23 '14 at 14:01
  • I updated my answer, but try this: `com.sendgrid.SendGrid.Email` instead. – Tristan Lee Dec 23 '14 at 14:15
  • Just noting another observation: I've done a writeDump(oSendGrid) and do not see an Email method. – HPWD Dec 23 '14 at 16:42
  • Sorry for the updates. I had to inspect the JAR myself to understand how the Email class is packaged as I couldn't find their Javadocs on the library. I thought it was in the `com.sendgrid` package. `Email` is actually an inner class of `SendGrid`. I have updated my answer once again. To reference inner classes, you have to use the dollar sign ($) instead of a dot (.). Like so: `createObject("java", "com.sendgrid.SendGrid$Email").init()` – Tristan Lee Dec 23 '14 at 16:56
  • @TristanLee Please don't apologize, I'm learning so much! I'm making the code changes now and will update soon. – HPWD Dec 23 '14 at 18:05
  • I got further but now getting the following error: `java.net.UnknownHostException: api.sendgrid.com The error occurred in C:/ColdFusion9/wwwroot/SendGrid/index.cfm: line 18 16 : 17 : // send the e-mail 18 : oSendGrid.send(oEmail); 19 : 20 : ` – HPWD Dec 23 '14 at 20:02
  • That exception typically indicates that the IP of the host couldn't be determined (http://docs.oracle.com/javase/7/docs/api/java/net/UnknownHostException.html). Is your CF server behind a proxy by chance, or on an internal network unable to access `api.sendgrid.com`? – Tristan Lee Dec 23 '14 at 20:14
  • Darnit, yes. I can browse the URL but I cannot ping the url. I literally just found this other SO post: http://stackoverflow.com/questions/6484275/what-causes-the-error-java-net-unknownhostexception. I'll need to try this later when I'm not behind the blasted firewall. – HPWD Dec 23 '14 at 20:44
  • If it is a proxy issue, try using their [Proxy Server Setup](https://github.com/sendgrid/sendgrid-java) example. – Leigh Dec 24 '14 at 01:36
  • @Leigh The proxy server setup is a great idea but it shouldn't be a problem normally. I'm doing some work on the side during my lunch break at work. – HPWD Dec 26 '14 at 15:55
  • 1
    @TristanLee Thank you so much for your help. I'm not 100% done but close enough to mark this as resolved. Thank you again and happy holidays. – HPWD Dec 26 '14 at 15:55