spring发送邮件demo

1.环境准备,所需jar包


2。在src目录下建立一个maisender.properties文件,里面写一些常用的配置

   如果邮箱服务器采用QQ邮箱则下面书写

mail.host=smtp.qq.com
mail.username=
XXXXXXXXXX@qq.com
mail.password=
XXXXXXXX

如果126邮箱服务器

mail.host=smtp.126.com
mail.username=xxxxxxxx@qq.com
mail.password=
xxxxxxxx

3.在src目录下建立一个applicationContext.xml文件,这个是使用spring框架的配置文件:

[html]  view plain  copy
  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" xmlns:tx="http://www.springframework.org/schema/tx"  
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans     
  7.     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd     
  8.     http://www.springframework.org/schema/tx     
  9.     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd    
  10.     http://www.springframework.org/schema/context    
  11.     http://www.springframework.org/schema/context/spring-context-3.0.xsd    
  12.     http://www.springframework.org/schema/mvc    
  13.     http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">  
  14.       
  15.     <context:component-scan base-package="com.*"></context:component-scan>  
  16.       
  17.     <mvc:annotation-driven/>  
  18.       
  19.     <bean id="propertyConfigurer"  
  20.     class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  21.         <property name="fileEncoding" value="utf-8" />  
  22.         <property name="locations">  
  23.          <list>  
  24.           <value>mailsender.properties</value>  
  25.          </list>  
  26.         </property>  
  27.     </bean>  
  28.       
  29.     <bean id="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">  
  30.      <property name="host">  
  31.       <value>${mail.host}</value>  
  32.      </property>  
  33.      <property name="javaMailProperties">  
  34.             <props>  
  35.               <prop key="mail.smtp.auth">true</prop>  
  36.               <prop key="mail.smtp.timeout">25000</prop>  
  37.             </props>  
  38.         </property>     
  39.      <property name="username">  
  40.       <value>${mail.username}</value>  
  41.      </property>  
  42.      <property name="password">  
  43.       <value>${mail.password}</value>  
  44.      </property>  
  45.     </bean>  
  46.       
  47.     </beans>  
4.在src目录下新建java文件

[java]  view plain  copy
  1. package com.email;  
  2.   
  3. import javax.mail.MessagingException;  
  4. import javax.mail.internet.MimeMessage;  
  5.   
  6. import org.springframework.context.ApplicationContext;  
  7. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  8. import org.springframework.mail.javamail.JavaMailSender;  
  9. import org.springframework.mail.javamail.MimeMessageHelper;  
  10.   
  11. public class SpringSimpleEmail {  
  12.     public static void main(String[] args) {  
  13.         try {  
  14.         ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");  
  15.         JavaMailSender sender = (JavaMailSender)ctx.getBean("javaMailSender");  
  16.         MimeMessage msg = sender.createMimeMessage();  
  17.             MimeMessageHelper helper = new MimeMessageHelper(msg,true,"utf-8");  
  18.             helper.setFrom("xxxxxxx@qq.com");  
  19.             helper.setTo("xxxxxx@qq.com");  
  20.             helper.setText("tets this is a spring mvc email");  
  21.             helper.setSubject("xxxxxxx");  
  22.             sender.send(msg);  
  23.             System.out.println("email send ok");  
  24.               
  25.         } catch (MessagingException e) {  
  26.             System.out.println("send fail");  
  27.             e.printStackTrace();  
  28.         }  
  29.           
  30.     }  
  31. }  
5.点击运行便可收到邮件。。笔者亲自测试的。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值