JavaMail and Mandril on GlassFish

Here is my glassfish-resources.xml:

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE resources PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Resource Definitions//EN" "http://glassfish.org/dtds/glassfish-resources_1_5.dtd"> <resources> <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 "> <description/> <property name="mail.user" value=" foo@bar.com "/> <property name="mail.password" value="password"/> <property name="mail.host" value="smtp.mandrillapp.com"/> <property name="mail.port" value="587"/> <property name="mail.smtp.socketFactory.port" value="587"/> <property name="mail.smtp.socketFactory.fallback" value="false"/> <property name="mail.smtp.auth" value="true"/> <property name="mail.smtp.starttls.enable" value="true"/> </mail-resource> </resources> 

And email sending method:

 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(); } 

When I call the above method, nothing happens. He comes to the end, with the exception of exceptions, but I do not receive emails from him.

Obviously, I configured it incorrectly because I was able to send emails via Mandrill using the mail client.

+4
source share
1 answer

I was able to send messages using JavaMail and Mandrill. I used a slightly different approach, which revealed an error in JavaMail: maybe you got the same error? The following code works for me

 mailProperties.setProperty("mail.transport.protocol", "smtp"); mailProperties.setProperty("mail.smtp.host", host); mailProperties.setProperty("mail.smtp.port", String.valueOf(port)); mailProperties.setProperty("mail.smtp.user", username); final Session session = Session.getInstance(mailProperties, null); session.setPasswordAuthentication( new URLName("smtp", host, -1, null, username, null), new PasswordAuthentication(username, password) ); final MimeMessage msg = new MimeMessage(session); // set required message properties Transport.send(msg); 

You can read more about the error in my blog post about SMTP authentication

+4
source

All Articles