用Spring来发邮件

很简单的,使用Spring提高的MailSender和MailMessage就可以了,配置代码如下:  TestMail.xml

// 下面的我测试可以通过的 要变就是下面红色的地方

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "spring-beans.dtd" >
    <beans>
    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">

    <property name="host">

      <value> mail.logicsolutions.com</value>
    </property>
    <property name="javaMailProperties">

      <props>

<!-- 如果要使用用户名和密码验证,这一步需要 -->

        <prop key="mail.smtp.auth">true</prop>
      </props>
    </property>

    <property name="username">

<!-- 你的Email地址 是去掉了@logicsolutions.com   完整 [email protected] -->

    <value> ale.hao</value>
    </property>

    <property name="password">

<!--你的Email密码 -->

    <value> logic123</value>
    </property>

    </bean>

<!-- 简单的message -->

    <bean id="mailMessage" class="org.springframework.mail.SimpleMailMessage">

    <property name="to">

<!-- 收件人地址-->

    <value> [email protected]</value>
    </property>

    <property name="from">

<!-- 你的地址-->

    <value> [email protected]</value>
    </property>
    <property name="subject">
    <value> Test Spring Mail sender</value>
    </property>
    </bean>
<!-- java class 路径 -->
    <bean id="testMailSender" class="com.budco.bmp.core.email.TestSenderMail">
    <property name="mailMessage">
    <ref bean="mailMessage"/>
    </property>
    <property name="mailSender">
    <ref bean="mailSender"/>
    </property>
    </bean>

    </beans>


////////////////////

上面的配置好以后就可以直接发送了, 看看TestSenderMail.java的代码:

package com.budco.bmp.core.email;

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

public class TestSenderMail {

    private MailSender mailSender;
    private SimpleMailMessage mailMessage;

    public TestSenderMail() {
    }

    public SimpleMailMessage getMailMessage() {
        return mailMessage;
    }

    public void setMailMessage(final SimpleMailMessage mailMessage) {
        this.mailMessage = mailMessage;
    }

    public MailSender getMailSender() {
        return mailSender;
    }

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

    public void sendMail() {
        // set up email contents,
        final SimpleMailMessage message = new SimpleMailMessage(mailMessage);
        message.setText("Test Spring Send Email...............");
        try {
            mailSender.send(message);
        } catch (final MailException e) {
            System.out.println(e.getMessage() + ",O . Send Email failure....");
            e.printStackTrace();
        }
    }
}

//
    很简单吧. 下面是测试类: TestApp.java

package com.budco.bmp.core.email;

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

public class TestApp {

    // activation.jar AND mail.jar import

    public static void main(final String[] args) {
        final ApplicationContext context = new ClassPathXmlApplicationContext("com/budco/bmp/core/email/TestMail.xml");
        final TestSenderMail sender = (TestSenderMail) context.getBean("testMailSender");
        sender.sendMail();
    }
}

最后提醒一下,记得要将:activation.jar 和 mail.jar 两个包导入到工程


你可能感兴趣的:(spring,bean,测试,Class,email,encoding)