2010-11-21 2 views
1

간단한 메일 서버를 설치하고 이메일을 보낼 수있는 방법을 물어보고 싶습니다. 내 localhost 서버 및 스프링 프레임 워크 + jsp뿐만 아니라 아파치 바람둥이 6.0을 사용하여. 나는 이것에 아주 새롭다. 누군가가 훌륭한 튜토리얼을 줄 수 있다면 큰 도움이 될 것입니다. 감사합니다spring mvc 및 jsp를 사용하여 메일 서버를 구성하는 방법은 무엇입니까?

+0

되고 가져 오기 당신은 당신을 호스팅 할 웹 서버와 함께 자신의 SMTP 서버? – BalusC

+0

아직 결정되지 않았습니다. 그러나 나는 더 쉬운 것으로 갈 것이다. :) – randy

답변

1

다음은 스프링 구성을 얻는 방법입니다. 아마 applicationContext-mail.xml. 그래서, 당신은 모두에서 (제 3 자) SMTP 서버가없는 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(); 
    } 
} 

}

관련 문제