业务场景邮件服务器与传输协议Java发送邮件

javamail邮件系统(基于JavaMail实现向QQ邮箱发送邮件)(1)

好了,基本原理和业务场景搞清楚了,下来以QQ邮箱作为案例(163等其他邮箱也是类似的),基于SSM框架的,SpringBoot同理,当然SSM框架都会配置了SpringBoot还不是手到擒来

开启SMTP服务

javamail邮件系统(基于JavaMail实现向QQ邮箱发送邮件)(2)

点击设置------ 账户

javamail邮件系统(基于JavaMail实现向QQ邮箱发送邮件)(3)

请记住这串编号,后面的配置会用到

javamail邮件系统(基于JavaMail实现向QQ邮箱发送邮件)(4)

导入依赖 关于spring的依赖自行删减

<!--邮件发送--> <dependency> <groupId>com.sun.mail</groupId> <artifactId>javax.mail</artifactId> <version>1.6.1</version> </dependency> <!--引入spring的上下文jar--> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.22.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context-support --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>4.3.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.4.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-tx --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>4.3.13.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency>

工程目录结构

javamail邮件系统(基于JavaMail实现向QQ邮箱发送邮件)(5)

设置配置文件 mail.properties

在163邮箱中同上的申请配置,可能和QQ页面所在位置不一样,请自行查找(基本都是一样的)

#服务器主机名QQ邮箱 smtp.xx.com 根据自己邮箱的使用自行设置 163邮箱: smtp.163.com mail.smtp.host=smtp.qq.com #自己的邮箱 mail.smtp.username=********@qq.com #密码/客户端授权码 这里的授权码就是刚才在邮箱中生成的 mail.smtp.password=******** #编码字符 mail.smtp.defaultEncoding=utf-8 #是否进行用户名密码校验 mail.smtp.auth=true #设置超时时间 mail.smtp.timeout=20000

spring-.xml 有些内容自行删减 这是我copy过来的

spring-core.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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--读取属性文件--> <context:property-placeholder location="classpath:mail.properties"/> <!--配置邮件接口--> <bean id="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> <property name="host" value="${mail.smtp.host}"/> <property name="username" value="${mail.smtp.username}"/> <property name="password" value="${mail.smtp.password}"/> <property name="defaultEncoding" value="${mail.smtp.defaultEncoding}"/> <property name="javaMailProperties"> <props> <prop key="mail.smtp.auth">${mail.smtp.auth}</prop> <prop key="mail.smtp.timeout">${mail.smtp.timeout}</prop> </props> </property> </bean> <context:component-scan base-package="com.*"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> </beans>

SendEmailController

import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.FileSystemResource; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import javax.mail.MessagingException; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import java.io.File; import java.io.IOException; import java.util.Properties; /** * @Description: 发送邮件 * @Author: 张楚涵 * @Date: 2019/8/14 0014 15:58 * @version:1.0.0 */ @RestController public class SenEmailController { @Autowired private JavaMailSender javaMailSender;//在spring中配置的邮件发送的bean @RequestMapping(value = "/send",method = RequestMethod.GET,produces = "text/html; charset=utf-8") public Object sendMail03(){ MimeMessage mMessage=javaMailSender.createMimeMessage();//创建邮件对象 MimeMessageHelper mMessageHelper; Properties prop = new Properties(); String from; try { //从配置文件中拿到发件人邮箱地址 //根据自己的目录设置 prop.load(this.getClass().getClassLoader().getResourceAsStream("mail.properties")); from = prop.get("mail.smtp.username") ""; mMessageHelper=new MimeMessageHelper(mMessage,true); // mMessageHelper.setFrom(from);//发件人邮箱 // 第二个参数是你想发送邮件时想用的名字 mMessageHelper.setFrom(new InternetAddress(from, "###", "UTF-8")); mMessageHelper.setTo("*******@qq.com");//收件人邮箱 mMessageHelper.setSubject("******");//邮件的主题 mMessageHelper.setSubject("Spring的邮件发送");//邮件的主题 //邮件的文本内容,true表示文本以html格式打开 mMessageHelper.setText("<p>这是使用spring的邮件功能发送的一封邮件</p><br/>" "<a href='https://blog.csdn.net/qq_41840847'>打开我的博客主页</a><br/>" "<img src='cid:fengye'>",true); /* File file=new File("F:/img/mr.png");//在邮件中添加一张图片 FileSystemResource resource=new FileSystemResource(file); mMessageHelper.addInline("fengye", resource);//这里指定一个id,在上面引用 mMessageHelper.addAttachment("mr.png", resource);//在邮件中添加一个附件 */ javaMailSender.send(mMessage);//发送邮件 } catch (MessagingException e) { e.printStackTrace(); return "发送失败"; } catch (IOException e) { e.printStackTrace(); } return "发送成功"; } }

测试

javamail邮件系统(基于JavaMail实现向QQ邮箱发送邮件)(6)

哈哈,终于总结完啦,如果对小涵的总结有不同的看法,直接评论就好啦!别忘了点波关注、转发哦!

,