4

这是我的 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 发送电子邮件。

4

1 回答 1

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

您可以在我关于 smtp 身份验证的博客文章中阅读有关该错误的更多信息

于 2012-12-19T15:25:15.103 回答