Spring开发发送邮件验证激活

 Spring开发发送邮件验证激活:

spring.xml

 <!-- 配置SimpleMailMessage发送文本邮件 -->
    <bean id="mailMessage" class="org.springframework.mail.SimpleMailMessage">
    	<property name="from" value="${mail.from}"/>
    </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.user}"/>
		<property name="password" value="${mail.pwd}"/>
		<property name="javaMailProperties">  
            <props>  
                <!-- 设置认证开关 -->  
                <prop key="mail.smtp.auth">true</prop>  
                <!-- 启动调试开关 -->  
                <prop key="mail.debug">true</prop>  
            </props>  
        </property>  
    </bean>


EmailController.java

package org.jun.controller.email;

import java.io.File;
import java.util.Date;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 发送邮件
 * 
 * @author xiejunbo
 *
 */

@Controller
@RequestMapping("email")
public class EmailController {  
    
	@Autowired
	private JavaMailSender mailSender;
	@Autowired
	private SimpleMailMessage simpleMsg;
	
	/**
	 * 带附件和链接的邮件
	 * @throws MessagingException 
	 */
	@ResponseBody
	@RequestMapping("sendMime")
	public String sendMime() throws MessagingException{
		
		MimeMessage msg = mailSender.createMimeMessage();
		MimeMessageHelper helper = new MimeMessageHelper(msg,true);
		helper.setFrom("[email protected]");
		helper.setTo("[email protected]");
		helper.setText("您刚刚在xx科技公司注册了账号,注册邮箱:[email protected],请点击以面链接进行验证<br><a href='http://localhost:8080/parser/email/checkEmail.do?username=xiejunbo&pwd=123456'><h4>验证链接</h4></a><br>"
				+ "<img src='http://xxxxxxxx/group1/M00/00/1C/oYYBAFS133CAIWJJAAOmte_4nRg167.jpg'>", true);
		helper.setSubject("xx科技公司账号激活验证链接");
		helper.setSentDate(new Date());
		FileSystemResource file = new FileSystemResource(new File("C:\\Users\\Administrator\\Desktop\\jun.jpg"));
		helper.addAttachment("jun.jpg", file);
		
		mailSender.send(msg);
		return  "send successfully";
	}

	/**
	 * 纯文本邮件
	 */
	@ResponseBody
	@RequestMapping("sendText")
	public String sendText() {  
		
		simpleMsg.setText("测试spring发送文本邮件");//邮件内容
        //   msg.setBcc("");//密送人,可以是一个数组
       // msg.setCc("");//抄送人,可以是一个数组
		simpleMsg.setSentDate(new Date());//发送时间
		simpleMsg.setSubject("这是发送主题");
		simpleMsg.setTo("[email protected]");//接收方,可以是一个数组
        mailSender.send(simpleMsg);
        
        return  "send successfully";
    }  
	
	/**
	 * 邮件激活验证
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping("checkEmail")
	public String checkEmail(@RequestParam String username, @RequestParam String pwd){
		if("xiejunbo".equals(username) && "123456".equals(pwd)){
			System.out.println("账号激活成功!");
			return "Verify account successfully!";	
		}
		System.out.println("账号激活失败!请重新激活!");
		return "Verify account fail! Please try again!";
	}
	
	 
}


mail.properties配置:

#mail sender
mail.host=smtp.qq.com
[email protected]
[email protected]
mail.pwd=xxxxxxxxxxxxxxx


你可能感兴趣的:(Spring开发发送邮件验证激活)