JavaMail邮件发送

导入JavaMail包

测试方法:

public class MailTest01 {
	@Test
	public void test() throws Exception{
		Properties properties = new Properties();
		//发送服务器地址
		properties.put("mail.smtp.host", "smtp.qq.com");
		//是否验证用户身份信息
		properties.put("mail.smtp.auth", "true");
		
		Session session = Session.getInstance(properties);
		//启用调试,可以在控制台输出SMTP协议应答过程
		session.setDebug(true);
		
		//创建MimeMessage格式的邮件
		MimeMessage message = new MimeMessage(session);
		//设置发送者
		Address fromaddress = new InternetAddress("******@qq.com");
		message.setFrom(fromaddress);
		
		Address toaddress = new InternetAddress("******@qq.com");
		message.setRecipient(RecipientType.TO, toaddress);
		
		message.setSubject("你好");
		
		message.setText("123456");
		
		//保存邮件
		message.saveChanges();
		
		//得到发送邮件
		Transport transport = session.getTransport("smtp");
		
		//连接服务器
		
		transport.connect("smtp.qq.com", "******@qq.com","prigyxdkpybqebji");
		
		//发送
		transport.sendMessage(message,message.getAllRecipients());
		//关闭
		transport.close();
	}

}

提取成工具类,然后再serviceimpl中完善新员工入职的代码:开启子线程处理,并且发送失败仍然要插入数据。

if (UtilFuns.isEmpty(entity.getUserId())) {
			String idString = UUID.randomUUID().toString();
			entity.setUserId(idString);
			entity.getUserinfo().setId(idString);
			//初始密码
			entity.setPassword(Encrypt.md5(SysConstant.DEFAULT_PASS, entity.getUserName()));

			baseDao.saveOrUpdate(entity);
			Thread thread = new Thread(new Runnable() {
				
				@Override
				public void run() {
					// TODO Auto-generated method stub
					try {
						//MailUtil.sendMessage(entity.getUserinfo().getEmail(), "新员工入职", "用户名"+entity.getUserName()+"初始密码*****");
						
						mailMessage.setTo(entity.getUserinfo().getEmail());
						mailMessage.setSubject("新员工入职");
						mailMessage.setText("用户名"+entity.getUserName()+"初始密码******");
						
						mailSender.send(mailMessage);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			});
			thread.start();
			
		}else{
			baseDao.saveOrUpdate(entity);
		}
	}

 

与项目集成开发改为配置文件:mail.properties

mail.host=smtp.qq.com
mail.username=****
mail.password=**********
mail.from=**@qq.com

applicationContext-mail.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"       
	xmlns:p="http://www.springframework.org/schema/p"  
	xmlns:context="http://www.springframework.org/schema/context"   
	xmlns:tx="http://www.springframework.org/schema/tx"  
	xmlns:aop="http://www.springframework.org/schema/aop"  
	xsi:schemaLocation="http://www.springframework.org/schema/beans    
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    
	http://www.springframework.org/schema/aop    
	http://www.springframework.org/schema/aop/spring-aop-3.0.xsd    
	http://www.springframework.org/schema/tx    
	http://www.springframework.org/schema/tx/spring-tx-3.0.xsd    
	http://www.springframework.org/schema/context    
	http://www.springframework.org/schema/context/spring-context-3.0.xsd">
	
	<description>mail</description>

	<context:property-placeholder location="classpath:mail.properties"/>
	
	<bean id="mailMessage" class="org.springframework.mail.SimpleMailMessage">  
        <property name="from">  
            <value>${mail.from}</value>  
        </property>  
        <!-- 查看SimpleMailMessage源码还可以注入标题,内容等 -->  
    </bean>  
    <!-- 申明JavaMailSenderImpl对象 -->  
    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">  
        <property name="defaultEncoding" value="UTF-8" />  
        <property name="host" value="${mail.host}" />  
        <property name="username" value="${mail.username}" />  
        <property name="password" value="${mail.password}" />  
        <property name="javaMailProperties">  
            <props>  
                <!-- 设置认证开关 -->  
                <prop key="mail.smtp.auth">true</prop>  
                <!-- 启动调试开关 -->  
                <prop key="mail.debug">true</prop>  
                <!-- 设置发送延时 -->
                <prop key="mail.smtp.timeout">0</prop>
            </props>  
        </property>  
    </bean>  	
	
</beans>

 

猜你喜欢

转载自blog.csdn.net/qq_38930784/article/details/81156717