利用Spring 发送邮件

  1. 1.导入spring-support.jar  mail.jar activtion.jar   
  2. 2.配置Spring配置文件   
  3. <beans>   
  4.     <bean id="mailSend" class="org.springframework.mail.javamail.JavaMailSenderImpl">   
  5.         <property name="host" value="smtp.163.com"></property>   
  6.         <property name="javaMailProperties">   
  7.             <props>   
  8.                 <prop key="mail.smtp.auth">true</prop>   
  9.                 <prop key="mail.smtp.timeout">10000</prop>   
  10.             </props>   
  11.         </property>   
  12.         <property name="username" value="yu_zhang430@163.com"></property>   
  13.         <property name="password" value="25257758"></property>   
  14. </bean>   
  15. 3.编写发送类   
  16.   public class test {   
  17.     private  ApplicationContext ac=null;   
  18.     public test()   
  19.     {   
  20.         ac=new ClassPathXmlApplicationContext("applicationContext.xml");   
  21.     }   
  22.     public  void sendEmail(String to,String title,String text) {   
  23.            
  24.         JavaMailSender jms=(JavaMailSender)ac.getBean("mailSend");   
  25.         SimpleMailMessage message=new SimpleMailMessage();   
  26.         message.setTo(to);   
  27.         message.setFrom("yu_zhang430@163.com");   
  28.         message.setText("测试邮件");   
  29.         message.setSubject(title);   
  30.         jms.send(message);   
  31.            
  32.     }   
  33.     public static void main(String[] args) {   
  34.         test t=new test();   
  35.         t.sendEmail("aixinxiaodian123@163.com""测试邮件""测试邮件");   
  36.     }   
  37.        
  38. }   
  39. 4.带附件的邮箱   
  40.   File file = new File("d:/中文文件.rar");   
  41.   messageHelper.addAttachment(MimeUtility.encodeWord(file.getName()), file);  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值