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`
1
  • 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. Commented Dec 23, 2014 at 19:54

1 Answer 1

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("[email protected]");
oEmail.addToName("Example Guy");
oEmail.setFrom("[email protected]");
oEmail.setSubject("Hello World");
oEmail.setText("My first email through SendGrid");

// send the e-mail
oSendGrid.send(oEmail);
Sign up to request clarification or add additional context in comments.

18 Comments

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?
Sounds like something worth trying.
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: help.adobe.com/en_US/ColdFusion/10.0/Developing/…
@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.
@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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.