使用Spring从Web应用程序发送邮件

时间:2011-10-18 14:07:29

标签: java email spring-mvc

在我的Web应用程序中,我想发送邮件。有没有办法用Spring MVC做到这一点?什么是最好的方法呢?

谢谢

3 个答案:

答案 0 :(得分:5)

您可以使用Spring的邮件抽象层轻松发送电子邮件。在applicationContext.xml中定义以下bean

<!-- Mail sender bean -->
<bean id="mailSender" 
        class="org.springframework.mail.javamail.JavaMailSenderImpl">          
<property name="host" value="my.smtp.host" />
<property name="username" value="my_username" />
<property name="password" value="my_password" />
</bean>

<!-- Simple mail template -->
<bean id="basicEmailMessage" 
        class="org.springframework.mail.SimpleMailMessage">
    <property name="from">
        <value>whateverSenderAddress</value>
     </property>
 </bean>

 <!-- Your service with sender and template injected -->
 <bean id="mySendMailService" 
        class="mypackage.MySendMailService">
    <property name="mailSender">
    <ref bean="mailSender" />
    </property>
    <property name="emailTemplate">
        <ref bean="basicEmailMessage" />
    </property>
 </bean>

然后,在mypackage.MySendMailService

public class SendMailService {
    private MailSender mailSender;
    private SimpleMailMessage emailTemplate;
    public void sendEmail(String to, String from, String subject, String body) 
                         throws MailException {
        SimpleMailMessage message = new SimpleMailMessage(this.emailTemplate);
        message.setTo(to);
        message.setFrom(from);
        message.setSubject(subject);
        message.setText(body);
        mailSender.send(message);
    }

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

答案 1 :(得分:3)

我正在使用Apache Velocity作为电子邮件模板系统。您可以将“VelocityEngine”的实例定义为spring bean并将其注入控制器。然而,更清洁的解决方案是将邮件发送代码放入服务中并将服务注入您的控制器。

 @Autowired private VelocityEngine velocityEngine;
     @Autowired private JavaMailSender mailSender;

        MimeMessagePreparator preparator = new MimeMessagePreparator() {
                    @Override
                    public void prepare(MimeMessage mimeMessage) throws Exception {
                        MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
                        message.setTo("customer@mail.com");
                        message.setFrom("noreply@mail.com");
                        message.setSubject("You got mail!");

                                Map<String, Object> model = new HashMap<String, Object>();
                        model.put("param1", new Date());

                        String text = 
                            VelocityEngineUtils.mergeTemplateIntoString(
                                    velocityEngine, 
                                    "com/myapp/mailtemplates/email.vm", 
                                    model
                            );

                        mimeMessage.setText(text,"utf-8", "html");
                        mimeMessage.setHeader("Content-Type", "text/html; charset=utf-8");
                    }
                };

mailSender.send(preparator);

HashMap可用于传递您可以在速度模板中使用的参数。 然后,您可以使用“JavaMailSender”发送电子邮件,“JavaMailSender”也可以定义为弹簧bean。

您可以定义与此类似的mailSender和velocityEngine bean:

    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host" value="smtp.mail.com" />
        <property name="username" value="sender" />
        <property name="password" value="password" />
        <property name="javaMailProperties">
            <props>
                <!-- Use SMTP-AUTH to authenticate to SMTP server -->
                <prop key="mail.smtp.auth">true</prop>
                <!-- Use TLS to encrypt communication with SMTP server -->
                <!-- <prop key="mail.smtp.starttls.enable">true</prop> -->
            </props>
        </property>
    </bean>

    <!-- Apache Velocity Email Template Engine -->
    <bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
        <property name="velocityProperties">
            <value>
                resource.loader=class
                class.resource.loader.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
            </value>
        </property>
    </bean>

答案 2 :(得分:1)

Spring MVC是一个Web框架,它与电子邮件无关。 Spring框架的另一部分确实支持邮件发送。看看chapter 24 of the ref guide