Spring實現發送郵件功能(簡易篇)

          1.Spring配制文件(bean.xml):

           1<?xml version="1.0" encoding="UTF-8"?>
           2<beans xmlns="http://www.springframework.org/schema/beans"
           3    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           4    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
           5
           6    <bean id="mailSender"
           7        class="org.springframework.mail.javamail.JavaMailSenderImpl">
           8        <property name="host">
           9            <!-- 公司的郵件服務器 -->
          10            <value>mail.xxx.com</value>
          11        </property>
          12    </bean>
          13    
          14</beans>

          2.測試類(SendMail.java):

           1import org.springframework.context.ConfigurableApplicationContext;
           2import org.springframework.context.support.ClassPathXmlApplicationContext;
           3import org.springframework.mail.MailSender;
           4import org.springframework.mail.SimpleMailMessage;
           5
           6public class SendMail {
           7
           8    public static void main(String[] args) {
           9        SimpleMailMessage message = new SimpleMailMessage();
          10
          11        message.setFrom("zzk@gmail.com");  //髮送者郵件地阯
          12        message.setTo("jakin@xxx.com");            //接收者郵件地阯
          13        message.setSubject("Test SendMail");    //郵件主題
          14        message.setText("Success");                //郵件內容
          15
          16        ConfigurableApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
          17        ((MailSender) applicationContext.getBean("mailSender")).send(message);
          18    }

          19}

          posted on 2008-01-08 19:00 靈! 閱讀(270) 評論(0)  編輯  收藏 所屬分類: Java專題-郵件/消息


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          <2008年1月>
          303112345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          導航

          統計

          隨筆分類

          隨筆檔案

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 西平县| 高平市| 雅安市| 江陵县| 天祝| 繁峙县| 马边| 临夏市| 岳普湖县| 嘉义县| 通海县| 邹城市| 惠安县| 平定县| 囊谦县| 来安县| 灵丘县| 永州市| 新绛县| 东莞市| 普陀区| 临潭县| 大余县| 绥江县| 通化县| 潜山县| 崇左市| 清河县| 区。| 巴里| 惠安县| 双牌县| 昆明市| 高陵县| 海丰县| 通城县| 启东市| 满城县| 宕昌县| 南川市| 井冈山市|