CloudBees Makes TomCat 7 Email Easy


Posted on

The following is a guest post from Cyrille Le Clerc, Solutions Architect at CloudBees

As a longtime SendGrid partner, we’ve helped bring SendGrid’s email solution to our Java platform-as-a-service. With our TomCat 7 container, now it’s just as easy to get SendGrid into Apache’s JavaServer/JavaServlet project, hosted using CloudBees.

Get Started

To begin, use this ClickStart as a starting point. This will set up a continuous deployment pipeline – a CloudBees Git repository, a Jenkins build compiling and running the test suite (on each commit). Should the build succeed, this seed app is deployed on a Tomcat 7 container.

Tomcat 7 container is available on CloudBees thanks to the tomcat7-clickstack. Documentation is available here.

Deploy a Web App on TomCat 7

You can deploy your web application on the tomcat7 clickstack using the CloudBees SDKapp:deploy” command.

bees app:deploy -a myapp -t tomcat7 ./target/tomcat7-sendgrid-clickstart-1.0-SNAPSHOT.war
  • -a myapp“: name of the CloudBees account and of the application. The application will be accessible on the URL http://tomcat7-sendgrid-clickstart.cyrille-leclerc.cloudbees.net/
  • -t tomcat7“: identifier of the tomcat7 clickstack
  • ./target/tomcat7-sendgrid-clickstart-1.0-SNAPSHOT.war“: path to the war file.
    You only need to set the “-R“, “-t” and “-D” settings once – they will be remembered for subsequent deploys.

Use SendGrid in Your Application

You can now use your java:comp/env/mail/SendGrid JNDI Mail Session in your application. Please note that jdbc/SendGrid is also available.

@WebServlet(...)
public class MyServlet extends HttpServlet {
@Resource(name = "mail/SendGrid", lookup = "mail/SendGrid")
private Session session;

protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(“no-reply@example.com”));
message.setRecipients(Message.RecipientType.TO, new InternetAddress[]{new InternetAddress(“you@example.com”)});
message.setSubject(“My Subject”);
message.setContent(“My Content”, “text/plain”);
Transport.send(message);

}
}

The ClickStart assumes a free SendGrid account with up to 200 emails per day. You can see other plans and add SendGrid to CloudBees from our site.


Adam DuVander speaks fluent "developer" while serving as Developer Communications Director. He helps SendGrid connect to coders of all stripes. Previously Adam wrote for Wired, Webmonkey and edited ProgrammableWeb, the leading resource for APIs.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>