title: JavaMail and Mandril on GlassFish
body: <p>Here's my glassfish-resources.xml:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!DOCTYPE resources PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Resource Definitions//EN" "http://glassfish.org/dtds/glassfish-resources_1_5.dtd"&gt;
  &lt;resources&gt;
      &lt;mail-resource debug="true"
                     enabled="true"
                     from="foo@bar.com"
                     host="smtp.mandrillapp.com"
                     jndi-name="java:app/mail/mySession"
                     object-type="user"
                     store-protocol="imap"
                     store-protocol-class="com.sun.mail.imap.IMAPStore"
                     transport-protocol="smtp"
                     transport-protocol-class="com.sun.mail.smtp.SMTPTransport"
                     user="foo@bar.com"&gt;
          &lt;description/&gt;
          &lt;property name="mail.user" value="foo@bar.com"/&gt;
          &lt;property name="mail.password" value="password"/&gt;
          &lt;property name="mail.host" value="smtp.mandrillapp.com"/&gt;
          &lt;property name="mail.port" value="587"/&gt;
          &lt;property name="mail.smtp.socketFactory.port" value="587"/&gt;
          &lt;property name="mail.smtp.socketFactory.fallback" value="false"/&gt;
          &lt;property name="mail.smtp.auth" value="true"/&gt;
          &lt;property name="mail.smtp.starttls.enable" value="true"/&gt;
      &lt;/mail-resource&gt;
  &lt;/resources&gt;
  </code></pre>
  
  <p>And a method to send email:</p>
  
  <pre class="lang-java prettyprint-override"><code>public static void sendEmail(final Session session,
                               final String from,
                               final String to,
                               final String subject,
                               final String htmlPart,
                               final String txtPart)
          throws AddressException,
                 MessagingException
  {
      MimeMessage message = new MimeMessage(session);
      message.setSender(new InternetAddress(from));
      InternetAddress toAddress = new InternetAddress(to);
      message.setRecipient(RecipientType.TO, toAddress);
      message.setSubject(subject);
  
      // Create a multipart message consisting of a HTML body with an alternate plain text version.
      MimeMultipart mp = new MimeMultipart("alternative");
  
      // Plain text part.
      MimeBodyPart textPlainPart = new MimeBodyPart();
      textPlainPart.setContent(txtPart, "text/plain");
      mp.addBodyPart(textPlainPart);
  
      // HTML part.
      MimeBodyPart textHtmlPart = new MimeBodyPart();
      textHtmlPart.setContent(htmlPart, "text/html");
      mp.addBodyPart(textHtmlPart);
  
      // Put it all together.
      message.setContent(mp);
      message.saveChanges();
  
      // Send the email.
      Transport tr = session.getTransport("smtp");
  
      String user = session.getProperty("mail.user");
      String smtpPassword = session.getProperty("mail.password");
      String smtpHost = session.getProperty("mail.host");
      int port = Integer.parseInt(session.getProperty("mail.port"));
  
      tr.connect(smtpHost, port, user, smtpPassword);
      tr.sendMessage(message, new Address[] { toAddress });
      tr.close();
  }
  </code></pre>
  
  <p>When I invoke the above method, nothing seems to happen. It gets to the end without throwing exceptions, but I don't receive emails from it.</p>
  
  <p>Obviously I've misconfigured something because I have been able to send emails via Mandrill using an email client.</p>