最后发送成功后,感受SpringBoot真的很强大.java
http://www.ykmimi.com/emailweb
↑待加入email输入的重载(能够不上传文件或能够不填写主内容)spring
↑待加入邮箱RegExp,title长度断定.(等琐碎工做)apache
首先建立SpringBoot项目浏览器
>>使用eclipse构建Spring项目 https://blog.csdn.net/fantasic_van/article/details/79309665 (eclipse未测试)
springboot
>>IDEA构建SpringBoot项目 https://blog.csdn.net/lom9357bye/article/details/69677120app
以后设置pom.xml文件 (红色是SpringBoot的邮件服务依赖)eclipse
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.ykmimi</groupId> <artifactId>springbootdemo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springbootdemo</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
这里我用的IDEA,等待它自动加载依赖完成.maven
在resource下的application.properties加入:spring-boot
spring.mail.host=smtp.126.com
spring.mail.username=发送邮箱@126.com
spring.mail.password=客户端受权码
spring.mail.default-encoding=UTF-8
#spring.mail.port=25
#spring.mail.protocol=smtp
#server.port=8080
126邮箱的受权密码开启方式:
建立一个Contoller
package com.ykmimi; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; @RestController @RequestMapping("mail") public class MailController { @Autowired JavaMailSender mailSender; @RequestMapping("sendmail") public Object sendEmail(){ try { final MimeMessage mimeMessage = this.mailSender.createMimeMessage(); final MimeMessageHelper message = new MimeMessageHelper(mimeMessage); message.setFrom("发送邮箱@126.com"); message.setTo("接收邮箱@qq.com"); message.setSubject("测试邮件主题"); message.setText("测试邮件内容"); this.mailSender.send(mimeMessage); } catch (MessagingException e) { e.printStackTrace(); } finally { return "sendEmail!"; } } }
以后执行测试类 SpringbootdemoApplication
浏览器执行 http://localhost:8080/mail/sendmail
---------------------------------------------------------------
通常不少网站上都是手机注册或邮箱注册,邮箱是获取一个随机的激活码,或激活连接(点开即激活帐号使用)