基于spring-boot的邮件发送的代码
来源:互联网 发布:it企业招人难 编辑:程序博客网 时间:2024/06/05 15:56
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
2.在application.properties里配置如下信息:
spring.mail.properties.mail.smtp.connecttimeout=5000
spring.mail.properties.mail.smtp.timeout=3000
spring.mail.properties.mail.smtp.writetimeout=5000
spring.mail.host=xxxxxxx
spring.mail.username=xxxx
spring.mail.password=xxx
spring.mail.port=25
spring.mail.properties.mail.smtp.auth=true
//---------------------------------------------------------------
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* 基于spring-boot的邮件发送功能测试
* @author Dabria_ly
* 2017年8月10日
*/
@Component("mailtest")
@Controller
@RequestMapping("/Mailtest")
public class TestEMailUtil {
@Autowired
private JavaMailSender mailSender;//JavaMailSender邮件发送类
@ResponseBody
@RequestMapping("/toSendMail")
public String testSendMail(){
//邮件接收者,多个邮件用;分割
String mailReceivers = "xxxx.com;xxxx@163.com";
String subject = "邮件主题";//邮件主题
String body = "邮件内容";//邮件内容
if(EMailUtilsWay.sendMail(mailSender, mailReceivers, subject, body)){
return "邮件发送成功";
}else{
return "邮件发送失败";
}
}
}
//-------------------------------------------------------------------------------------------
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
/**
* 发送邮件的工具类(基于spring-boot的邮件发送功能)
* @author Dabria_ly
* 2017年8月10日
*
*/
@Component
public class EMailUtilsWay {
private static final Logger LOG = LoggerFactory.getLogger(EMailUtilsWay.class);
/**
* 发送预警邮件
* @param mailSender : JavaMailSender邮件发送类
* @param mailReceivers : 邮件接收者
* @param subject : 邮件主题
* @param body : 邮件内容
* @return
*/
public static boolean sendMail(JavaMailSender mailSender,String mailReceivers,String subject,String body) {
if (StringUtils.hasText(mailReceivers)) {
String[] mails = mailReceivers.split(";");
return sendMail(mailSender,mails, subject, body);
}
return false;
}
public static boolean sendMail(JavaMailSender mailSender,String[] mails,String subject,String body) {
try {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom("nopo");//设置邮件发送者名称
message.setTo(mails);
message.setSubject(subject);
message.setText(body);
mailSender.send(message);
return true;
} catch (Exception ex) {
LOG.error(String.format("发送邮件失败,%s", mails), ex);
}
return false;
}
}
- 基于spring-boot的邮件发送的代码
- 基于spring-boot的邮件发送的代码
- spring boot 发送邮件
- spring boot 发送邮件
- spring boot 发送邮件
- spring boot 发送邮件
- spring boot发送邮件
- spring boot 发送邮件
- Spring Boot 发送邮件
- Spring boot发送邮件失败时候的解决办法
- spring的发送邮件
- 发送邮件的代码~
- 发送邮件的代码
- 发送邮件的代码
- 使用Spring发送基于freemarker模板的邮件
- 基于maven发送邮件系列(2)---用spring的timer实现定时发送邮件
- 47. Spring Boot发送邮件
- spring-boot mail 邮件发送
- Ubuntu下安装MySQL及简单操作
- BAT脚本编写教程(比较易懂和全面)
- 原来是在Tomcat服务器上发布,现在改为JBoss 发布 Struct2 +Spring+hibernate Maven项目出错解决方法
- 有了这个教程,就可以在Office中嵌入MathType
- 浅谈Java中的final关键字
- 基于spring-boot的邮件发送的代码
- Maven 实战1-10
- 从头开始学算法:考研机试题练习(C/C++)–算法初步
- 非常实用的Linux命令行技巧(转载)
- 对QQ、微信等第三方登录的几个思考
- 在Mac系统下配置PHP运行环境
- docker 命令
- 运用decorate装饰器模拟网页登陆(含代码)
- html,js 推箱子游戏