1

我有一个像这样的 EmailService 类:

package com.helios.service.mail;

import java.util.ArrayList;
import java.util.List;

import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.internet.AddressException;
import javax.mail.internet.MimeMessage;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailException;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.JavaMailSender;

import scala.actors.threadpool.Arrays;
import com.helios.util.PropertiesUtil;

/**
 * The e-mail service contains all you need in order to send E-Mails 
 * successfully
 */
@Service
public class EmailService
{
    // ------------------------------------------------------------------------
    // members
// ------------------------------------------------------------------------

// log4j...
static final Logger logger = Logger.getLogger(EmailService.class);

@Autowired
private JavaMailSender mailSender;

// ------------------------------------------------------------------------
// public usage
// ------------------------------------------------------------------------

/**
 * Send an Email the cool way!
 * @param recipients
 * @param subject
 * @param text
 * @param html
 */
public void sendEmail(List<String> recipients,String subject,
        String text,boolean html)
    throws NoSuchProviderException,AddressException,MessagingException {

    try {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage);
        messageHelper.setFrom(PropertiesUtil.getProperty("mail.defaultsender"));
        messageHelper.setTo(recipients.toArray(new String[recipients.size()]));
        messageHelper.setSubject(subject);
        messageHelper.setText(text, html);
        mailSender.send(mimeMessage);
    }
    catch (MailException ex) {
        logger.error("Fail: "+ex.getMessage());
    }

}
// ------------------------------------------------------------------------
// private usage
// ------------------------------------------------------------------------


// ------------------------------------------------------------------------
// GETTER & SETTER
// ------------------------------------------------------------------------
}

一个 bean 用于配置 mailSender 以通过 Amazon SES 发送

  <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    <property name="host" value="email-smtp.us-east-1.amazonaws.com" />
    <property name="port" value="465" />
    <property name="protocol" value="smtps" />
    <property name="username" value="..." />
    <property name="password" value="..." />
    <property name="javaMailProperties">
        <props>
            <prop key="mail.smtps.auth">true</prop>
            <prop key="mail.smtp.ssl.enable">true</prop>
            <prop key="mail.transport.protocol">smtps</prop>
            <prop key="mail.debug">true</prop>
        </props>
    </property>
</bean>

现在,如果我在 JDK1.6 下运行 sendEmail 方法,它将正常工作。如果我在 1.7 上运行绝对相同的代码,它将无法工作。

我找不到问题出在哪里。你有什么主意吗?

谢谢!

4

0 回答 0