randy
randy

Reputation: 283

how to configure a mail server using spring mvc and jsp?

I would just like to ask how can i setup a simple mail server and be able to send an email. Im using apache tomcat 6.0 as my localhost server and spring framework+jsp as well. I'm quite new on this. So if someone can give a nice tutorial, it will be of great help. thanks

Upvotes: 1

Views: 1999

Answers (1)

Jinesh Parekh
Jinesh Parekh

Reputation: 2141

Below is how you would get the spring configuration. probably applicationContext-mail.xml. Import that into 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="[email protected]" />
    <property name="subject" value="Your account information" />
</bean>

Below is the 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();
    }
}

}

Upvotes: 1

Related Questions