GlassFish上的JavaMail和Mandril

时间:2012-12-11 18:29:49

标签: java glassfish javamail

这是我的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>

发送电子邮件的方法:

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

当我调用上述方法时,似乎什么都没发生。它没有抛出异常就结束了,但我没有收到来自它的电子邮件。

显然我错误配置了一些东西,因为我能够通过Mandrill使用电子邮件客户端发送电子邮件。

1 个答案:

答案 0 :(得分:4)

我设法使用JavaMail和Mandrill发送消息。我使用的方法略有不同,暴露了JavaMail中的一个错误:也许你遇到了同样的错误?以下代码适用于我

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

您可以在my blog post about smtp authentication

中详细了解该错误