2010-11-21 21 views
1

シンプルなメールサーバーをセットアップして電子メールを送信するにはどうすればいいのですか?私のlocalhostサーバーとspring framework + jspとしてapache tomcat 6.0を使用しています。私はこれでかなり新しいです。だから誰かが良いチュートリアルを与えることができれば、大きな助けになるでしょう。ありがとうspring mvcとjspを使用してメールサーバを設定する方法は?

+0

で、あなたはすべての(サードパーティ)のSMTPサーバーを持っていないと、あなたをホストしたいのインポートWebサーバーと一緒に独自のSMTPサーバー? – BalusC

+0

まだ決定していません。しかし、私は簡単なものと一緒に行くつもりです。 :) – randy

答えて

1

以下は、どのようにバネ構成になりますか?おそらくapplicationContext-mail.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> 

applicationContext.xmlを

にはそう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(); 
    } 
} 

}

関連する問題