2010-11-21 79 views
1

我只想問如何設置一個簡單的郵件服務器,並能夠發送電子郵件。我使用Apache Tomcat 6.0作爲我的本地主機服務器和Spring框架+ jsp。我對此很新穎。所以如果有人能給出一個很好的教程,這將會有很大的幫助。謝謝如何使用spring mvc和jsp配置郵件服務器?

+0

所以,你不必在一個所有(第三方)SMTP服務器和要承載您自己的SMTP服務器連同Web服務器? – BalusC 2010-11-21 15:50:20

+0

我還沒決定。但我會用更容易的。 :) – randy 2010-11-21 15:52:14

回答

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="[email protected]" /> 
    <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(); 
    } 
} 

}

相關問題