我的java历程

在springframework框架下发送email

在springframework框架下发送email

(1)先请看我的目录结构:



(2)EmailUtil.java

package com.email;

import org.springframework.mail.MailException;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;

public class EmailUtil {

 private MailSender mailSender;

 private SimpleMailMessage message;

 public void sendEmail(String emailAddress, String content) {
  SimpleMailMessage msg = new SimpleMailMessage(this.message);
  msg.setTo(emailAddress);
  msg.setText(content);
  try {
   mailSender.send(msg);
   System.out.println("Send Email successfully!");
  } catch (MailException ex) {
   System.err.println("Send Email failure: " + ex.getMessage());
  }
 }

 public void setMailSender(MailSender mailSender) {
  this.mailSender = mailSender;
 }

 public void setMessage(SimpleMailMessage message) {
  this.message = message;
 }
}


MailProperties.java

package com.email;

import java.util.Properties;

public class MailProperties extends Properties {
 
 private String auth;

 public MailProperties(String auth) {
  super.setProperty("mail.smtp.auth", auth);
 }

}


EmailUtilTest.java

package com.email.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import junit.framework.TestCase;

import com.email.EmailUtil;

public class EmailUtilTest extends TestCase{
 
 private static ApplicationContext applicationContext;
 private static EmailUtil emailUtil;
 
 static {
  try {
   if(applicationContext == null)
    applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
   emailUtil = (EmailUtil) applicationContext.getBean("emailUtil");
  } catch (Throwable ex) {
   System.out.println("Initialize ApplicationContext failed:");
   ex.printStackTrace();
  }
 }
 
 public void testSendEmail(){
  emailUtil.sendEmail("pppp@163.com", "hello,this is a test");
 }
 
}


applicationContext.xml

<?xml version="1.0"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/DTD/spring-beans.dtd">
<beans>
 
 <bean id="emailUtil" class="com.email.EmailUtil">
  <property name="mailSender" ref="mailSender"/>
  <property name="message" ref="mailMessage"/>
 </bean>
 
 <bean id="mailSender"
  class="org.springframework.mail.javamail.JavaMailSenderImpl">
  <property name="host" value="smtp.163.com" />  <!-- 邮件服务器 -->
  <property name="port" value="25"/>
  <property name="password" value="1111" />
  <property name="username" value=aaa@163.com />
  <property name="javaMailProperties">   <!-- 如果你的邮箱需要验证则加上此 -->
   <ref local="mailProperties"/>
  </property>
 </bean>
 
 <bean id="mailProperties" class="com.email.MailProperties">
  <constructor-arg index="0">
   <value>true</value>
  </constructor-arg>
 </bean>

 <bean id="mailMessage"
  class="org.springframework.mail.SimpleMailMessage">
  <property name="from" value=aaa@163.com />
  <property name="subject" value="Spring Mail Test" />
 </bean>

</beans>

(3)说明:首先保证能ping通邮件服务器

posted on 2006-11-24 16:31 landril 阅读(477) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: