spring的发送邮件

spring框架自身集成了发邮件的功能,不需要再引入其他的功能就可以实现了。

首先需要将spring/lib/j2ee目录下的activation.jar和mail.jar加入到工程的classpath,本工程是加入的spring2.0.5的版本,为了简单将j2ee下的所有jar包都引入了。

同时,因为部分用到了spring的依赖注入,所以还需要spring的依赖包,如果不需要spring注入可以不用理会这步。

****************************************************************************************************

<!-- 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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd"
>
      <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host" value="smtp.163.com" />
        <property name="port" value="25" />
        <property name="username" value="[email protected]" />
        <property name="password" value="你的用户密码" />
        <property name="javaMailProperties">
             <props>
                 <prop key="mail.smtp.auth">true</prop>
             </props>
        </property>
    </bean>
</beans>

*****************************************************************************************************

//程序部分

package test;

import java.io.File;
import java.util.Properties;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;


public class Test {
   
    public void mailSimple() {
        // TODO Auto-generated method stub
        ApplicationContext context = new ClassPathXmlApplicationContext("config.xml");
        JavaMailSender mailSender= (JavaMailSender) context.getBean("mailSender");
        SimpleMailMessage mail = new SimpleMailMessage();
        mail.setFrom("[email protected]");
        mail.setTo("[email protected]");
        mail.setSubject(" 测试spring Mail");
        mail.setText("hello,java");
       
        mailSender.send(mail);
    }
   
    public void mailAttachment(){
        JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
       
        //SMTP验证时,需要用户名和密码
        senderImpl.setUsername("[email protected]");
        senderImpl.setPassword("你的密码");
        senderImpl.setHost("smtp.163.com");
        senderImpl.setPort(25);
        Properties javaMailProperties = new Properties();
        javaMailProperties.setProperty("mail.smtp.auth", "true");
        senderImpl.setJavaMailProperties(javaMailProperties);

        MimeMessage mailMessage = senderImpl.createMimeMessage();
        MimeMessageHelper messageHelper;
  try {
   messageHelper = new MimeMessageHelper(mailMessage, true);
   messageHelper.setTo("[email protected]");
   messageHelper.setFrom("[email protected]");
   messageHelper.setSubject(" 主题。。。 ");
   messageHelper.setText(
     "<html><head></head><body><h1>Hello! Spring!"
     + "</h1></body></html>", true);
   
   FileSystemResource file = new FileSystemResource(new File("C:\\Documents and Settings\\KenChow\\桌面\\test.xls"));
   messageHelper.addAttachment("test.xls", file);
   
   senderImpl.send(mailMessage);
   
   System.out.println("OK");
  } catch (MessagingException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
               
    }

}

你可能感兴趣的:(spring,bean,properties,File,Class,encoding)