【问题标题】:how to configure a mail server using spring mvc and jsp?如何使用spring mvc和jsp配置邮件服务器?
【发布时间】:2011-05-13 10:16:23
【问题描述】:

我想问一下如何设置一个简单的邮件服务器并能够发送电子邮件。我使用 apache tomcat 6.0 作为我的本地主机服务器和 spring framework+jsp。我对此很陌生。因此,如果有人可以提供一个很好的教程,那将有很大的帮助。谢谢

【问题讨论】:

  • 那么,您根本没有(第 3 方)SMTP 服务器,您想将自己的 SMTP 服务器与 Web 服务器一起托管?
  • 我还没有决定。但我会选择更容易的。 :)

标签: java email jsp tomcat spring-mvc


【解决方案1】:

以下是获取弹簧配置的方法。可能是 applicationContext-mail.xml。将其导入 applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"autowire="byName">

default-autowire="byName">

<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}" />
</bean>


<bean id="freemarkerConfiguration"
    class="org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean">
    <property name="templateLoaderPath" value="/WEB-INF/templates" />
</bean>

<!-- KINDLY MAINTAIN ALPHABETICAL ORDER THIS LINE ONWARDS -->
<bean id="notificationService" class="com.isavera.service.NotificationServiceImpl"
    scope="prototype">
    <property name="mailSender" ref="mailSender" />
    <property name="freemarkerConfiguration" ref="freemarkerConfiguration" />
    <property name="freemarkerTemplate" value="accountInformation.ftl" />
    <property name="fromAddress" value="info@apnagenie.com" />
    <property name="subject" value="Your account information" />
</bean>

下面是 NotificationServiceImpl

public class NotificationServiceImpl implements NotificationService, Runnable {
private boolean asynchronous = true;

private JavaMailSender mailSender;

private Configuration freemarkerConfiguration;

private String freemarkerTemplate;

private Map<String, Object> attributes;

private String deliveryAddress;

private String[] deliveryAddresses;

private String fromAddress;

private String subject;

private SimpleMailMessage message;

private MimeMessage mimeMessage;

public void deliver() {
    message = new SimpleMailMessage();

    if (getDeliveryAddresses() == null) {
        message.setTo(getDeliveryAddress());
    } else {
        message.setTo(getDeliveryAddresses());
    }

    message.setSubject(subject);
    message.setFrom(fromAddress);

    // Merge the model into the template
    final String result;
    try {

        result = FreeMarkerTemplateUtils.processTemplateIntoString(freemarkerConfiguration.getTemplate(appendApplicationName(freemarkerTemplate)), attributes);
        message.setText(result);
        if (asynchronous) {
            Thread emailThread = new Thread(this);
            emailThread.start();
        } else {
            run();
        }
    } catch (IOException e) {
        e.printStackTrace();
    } catch (TemplateException e) {
        e.printStackTrace();
    }
}

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-12
    • 2018-04-14
    • 2012-03-03
    • 1970-01-01
    • 2011-02-26
    • 2011-05-04
    相关资源
    最近更新 更多