使用SpringEMail API发送Email

         这里使用SpringEmail发送Email,这里采用的是Java显示配置Bean,不是采用很多人使用的XML配置Bean,顺带我选择的是smtp协议的qq邮箱,并且qq邮箱要用SSL认证,所以端口不在之前的25,而应该是465

首先的是配置文件 mail.properties

#-------------------------
#-------------------------
#-------------------------
#Server 要发送到qq邮箱的并且采用smtp的地址
mail.smtp.host = smtp.qq.com
#Port   端口号
mail.smtp.port = 465
#whether password 是否要验证账号密码
mail.smtp.auth = true
#whether time out  时间
mail.smtp.timeout = 50000
#whether SSL    是否要SSL
mail.smtp.ssl.enable = true
#which transport.protocol 这个不懂 反正网上的人都写smtp估计是要采取的传输邮件的协议
mail.transport.protocol = smtp
#which socket    
mail.smtp.socketFactory.class = javax.net.ssl.SSLSocketFactory
#-------------------------
#Auth username and password
#-------------------------
#username 发送者的qq邮箱账号注意:是qq账号+@qq.com形式
mail.smtp.username = xxxx
#password 授权码,不懂授权码的自己百度吧,网上一大堆教怎么用授权码
mail.smtp.password = xxxx
#Encoding 默认的编码
mail.encoding = gbk
接下来的是Spring的Bean配置类,采用的是Java配置

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.mail.MailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;

import java.util.Properties;
import java.util.logging.Logger;

@Configuration
@PropertySource(value="classpath:mail.properties")//加载properties文件,但相对的下面会有很多@Value注释的域
public class AllConfiguration {
	
	@Value("${mail.smtp.host}")
	public String mail_smtp_host;
	
	@Value("${mail.smtp.port}")
	public String mail_smtp_port;
	
	@Value("${mail.smtp.username}")
	public String mail_smtp_username;
	
	@Value("${mail.smtp.password}")
	public String mail_smtp_password;
	
	@Value("${mail.encoding}")
	public String mail_encoding;
	
	@Value("${mail.smtp.auth}")
	public String mail_smtp_auth;
	
	@Value("${mail.smtp.timeout}")
	public String mail_smtp_timeout;
	
	@Value("${mail.smtp.ssl.enable}")
	public String mail_smtp_ssl_enable;
	
	@Value("${mail.transport.protocol}")
	public String mail_transport_protocol;
	
	@Value("${mail.smtp.socketFactory.class}")
	public String mail_smtp_socketFactory_class;
	
	//要有下面的bean才能解读properties中的属性
	@Bean 
	public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() { 
		return new PropertySourcesPlaceholderConfigurer(); 
	} 
	
	
	@Bean
	public MailSender createMailSender(){
		//MailSender这个是来配置发送的是哪个邮箱,密码,端口等等,是否要SSL认证
		JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
//		PropertiesUtil util=new PropertiesUtil("classpath:mail.properties");  
		Logger log = Logger.getLogger("log");
		log.info("创建smtp中");
		//设置服务器验证信息,现在像QQ邮箱都必须要SSL认证
		//put的话第二个参数支持各种类型,但是setProperty第二个参数只能是String型
		Properties properties = new Properties();
		System.out.println(mail_smtp_auth);
		properties.put("mail.smtp.auth", mail_smtp_auth);
		properties.put("mail.smtp.ssl.enable", mail_smtp_ssl_enable);
		properties.put("mail.transport.protocol", mail_transport_protocol);
		properties.put("mail.smtp.socketFactory.class",mail_smtp_socketFactory_class);
		properties.put("mail.smtp.timeout", mail_smtp_timeout);
		//设置邮件发到哪里去
		mailSender.setHost(mail_smtp_host);
		mailSender.setPort(Integer.valueOf(mail_smtp_port));
		mailSender.setUsername(mail_smtp_username);
		mailSender.setPassword(mail_smtp_password);
		mailSender.setDefaultEncoding(mail_encoding);
		mailSender.setProtocol("smtp");
		mailSender.setJavaMailProperties(properties);
		log.info("创建完毕");
		return mailSender;
	}
}
接下来的是测试类

import java.util.logging.Level;
import java.util.logging.Logger;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=AllConfiguration.class)
public class SpitterMailImpl implements SpitterMail{
	private static Logger log = Logger.getLogger("C://Users/Administrator/Desktop/Log.txt");
	
	@Autowired
	private JavaMailSender mailSender;
	
	
	@Override
	public void sendMail(String to) {
		SimpleMailMessage message = new SimpleMailMessage();//这个只是写邮件的内容,包括发件人,收件人,内容等等
		try{
			log.info("创建邮件中...");
			message.setFrom("xxxxxx@qq.com");//注意setForm的参数是发送者的qq邮箱账号,这里与mail.smtp.username相同
			message.setTo(to);//发送到哪里去
			message.setSubject("邮件主题");//邮件主题
			message.setText("邮件内容");//邮件内容
			log.log(Level.INFO, "邮件正在发送");
			mailSender.send(message);
			log.log(Level.INFO, "邮件发送成功");
		}catch(Exception e){
			e.printStackTrace();
			log.log(Level.WARNING, "发送异常");
		}
	}
	
	@Test
	public void test(){
		sendMail("xxxxxxx@qq.com");//接收者的邮箱
	}

}


最后说一句,因为要使用smtp,所以自己的邮箱必须要先开通smtp协议

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值