从bean执行时,Java Mail不起作用

时间:2014-08-05 14:58:19

标签: spring tomcat sendmail

这是我的@Service类,从前端调用remind()方法。

@Service
public class EmailManagerImpl implements EmailManager {


    private MailSender mailSender;
    private SimpleMailMessage templateMessage;

    public void setMailSender(MailSender mailSender) {
        this.mailSender = mailSender;
    }

    public void setTemplateMessage(SimpleMailMessage templateMessage) {
        this.templateMessage = templateMessage;
    }

    @Override
    public void remind(Appointment appointment) {
        Person patient = appointment.getPatient();

    SimpleMailMessage msg = new SimpleMailMessage(this.templateMessage);


        msg.setTo(patient.getMail());
        msg.setText("Szanowny pacjencie " + patient.getFirstName() + " "
                + patient.getSurname() + " "
                + appointment.toString());
        try {
            mailSender.send(msg);
        } catch (MailException ex) {
            // log it and go on
            System.err.println(ex.getMessage());
        }
    }

我确信templeMessage和mailSender是正确注入的。执行整个过程后我没有得到任何错误,没有异常没有调试信息来自true。当我尝试运行其中一个演示时,它的工作正常。唯一的不同是我的应用程序是在Tomcat + i调用bean的方法。有什么想法吗?

我可以从SimpleMailMessage行跟进调试到以下方法,稍后我会收到找不到源的消息:

//---------------------------------------------------------------------
// Implementation of MailSender
//---------------------------------------------------------------------

public void send(SimpleMailMessage simpleMessage) throws MailException {
    send(new SimpleMailMessage[] { simpleMessage });
}

我的applicationConfig:     

 <!--     Wysylanie maili -->
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    <property name="host" value="${mail.host}" />
    <property name="port" value="${mail.port}" />
    <property name="username" value="${mail.username}" />
    <property name="password" value="${mail.password}" />
     <property name="protocol" value="smtp" />
    <property name="javaMailProperties">

       <props>
           <prop key="mail.smtp.ssl.trust">smtp.gmail.com</prop>
              <prop key="mail.smtp.auth">true</prop>
              <prop key="mail.smtp.starttls.enable">true</prop>
              <prop key="mail.smtps.debug">true</prop>
           </props>
    </property>

</bean>

<!-- this is a template message that we can pre-load with default state -->
<bean id="templateMessage" class="org.springframework.mail.SimpleMailMessage">
    <property name="from" value="${mail.username}" />
  <property name="subject" value="${mail.subject}" />
</bean>

<bean id="emailManagerImpl" class="com.przychodnia.rejestracja.integration.EmailManagerImpl">
  <property name="mailSender"><ref bean="mailSender"/></property>
  <property name="templateMessage"><ref bean="templateMessage"/></property>
</bean>

0 个答案:

没有答案