java之mail发送
一、邮箱的应用在实际的Java开发过程中,也是非常常见的。我这里就是写了一个简单的邮箱发送功能。
二、邮箱的基本配置,使用的是JavaMailSender的提供的方式来实现邮件的发送
1)qq邮箱的配置(说明一点目前qq邮箱的配置方式都是采用ssl的方式来实现的,非ssl的方式没有测试成功)
package com.troy.jpa.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.JavaMailSenderImpl; import java.util.Properties; @Configuration public class MailConfiguration { @Bean public JavaMailSender javaMailSender() { //设置JavaMail的基本数据 JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl(); //host官方查询 javaMailSender.setHost("smtp.qq.com"); //端口465ssl javaMailSender.setPort(465); //设置账户,密码 javaMailSender.setUsername("409546297@qq.com"); javaMailSender.setPassword("密码"); //认证数据配置,ssl方式需要加入认证方式 Properties properties = new Properties(); properties.setProperty("mail.host", "smtp.qq.com"); properties.setProperty("mail.transport.protocol", "smtp"); properties.setProperty("mail.smtp.auth", "true"); properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); properties.setProperty("mail.smtp.port", "465"); properties.setProperty("mail.smtp.socketFactory.port", "465"); javaMailSender.setJavaMailProperties(properties); return javaMailSender; } }
注意:这里的密码不是我们一般登录的密码,而是授权码
2)163邮箱有2中配置方式
a、ssl的的连接方式
package com.troy.jpa.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.JavaMailSenderImpl; import java.util.Properties; @Configuration public class MailConfiguration { @Bean public JavaMailSender javaMailSender() { JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl(); javaMailSender.setHost("smtp.163.com"); javaMailSender.setPort(465); javaMailSender.setUsername("账号"); javaMailSender.setPassword("密码"); Properties properties = new Properties(); properties.setProperty("mail.host", "smtp.163.com"); properties.setProperty("mail.transport.protocol", "smtp"); properties.setProperty("mail.smtp.auth", "true"); properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); properties.setProperty("mail.smtp.port", "465"); properties.setProperty("mail.smtp.socketFactory.port", "465"); javaMailSender.setJavaMailProperties(properties); return javaMailSender; } }
b、非ssl的连接方式
package com.troy.jpa.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.JavaMailSenderImpl; @Configuration public class MailConfiguration { @Bean public JavaMailSender javaMailSender() { JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl(); javaMailSender.setHost("smtp.163.com"); javaMailSender.setPort(25); javaMailSender.setUsername("账号"); javaMailSender.setPassword("密码"); return javaMailSender; } }
3)这个是采用注解的方式,自己也可以采用xml的配置方式,自己随意
4)应用,邮件的发送
package com.troy.jpa.util; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; public class MailUtil { public static void main(String[] args) { sendMail("收件人邮箱","test","test"); } public static void sendMail(String mail, String title, String text) { //加载容器 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/spring-mvc.xml"); //获取bean JavaMailSender javaMailSender = (JavaMailSender) context.getBean("javaMailSender"); //建立邮箱模板 SimpleMailMessage mailMessage = new SimpleMailMessage(); //设置发送人的账号 mailMessage.setFrom("发送人账号(和登录的账号一样)"); //设置接收人的账号 mailMessage.setTo(mail); //设置主题 mailMessage.setSubject(title); //设置内容 mailMessage.setText(text); //发送邮件 javaMailSender.send(mailMessage); } }