发送邮件
发送邮件,配置
需要引入依赖:
<!-- 发送邮件 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
spring.mail.default-encoding=UTF-8 #ping smtp.qq.com spring.mail.host=14.18.245.164 #发送者的邮箱权限密码,// 授权密码,非登录密码,QQ个人邮箱是 开启SMTP时QQ给的授权码;开启stmp时,163的授权码,目前需要自己设定。阿里云登录邮箱 登录密码可以使用 spring.mail.password=pdmlxeelgttjiiai #端口 #spring.mail.port=25 #协议 spring.mail.protocol=smtp #发送者的邮箱账号 spring.mail.username=1593612833@qq.com spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.starttls.enable=true spring.mail.properties.mail.smtp.starttls.required=true #考虑到默认端口25可能在Linux上没有开通 spring.mail.port=465 spring.mail.properties.mail.smtp.socketFactory.port=465 spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory spring.mail.properties.mail.smtp.socketFactory.fallback=false
发送邮件方法:
package com.example.demo.rocketmq; import java.util.Properties; import org.apache.logging.log4j.util.PropertiesUtil; import org.java_websocket.WebSocket; import org.java_websocket.WebSocket.READYSTATE; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.stereotype.Service; import com.example.demo.configs.MsgWebSocketClient; /** * @author author:zel * @date 2019年3月27日 下午4:47:48 * */ @Service public class ConsumerService { private static final Logger logger = LoggerFactory.getLogger(ConsumerService.class); @Autowired private JavaMailSender mailSender; //框架自带的 /** * 发送邮件方法 * @param title * @param text * @param email * @return */ public boolean sendEmail(String title, String text, String email){ //建立邮件消息 SimpleMailMessage mainMessage = new SimpleMailMessage(); //发送者 配置里有 mainMessage.setFrom("1593612833@qq.com"); //接收者 mainMessage.setTo(email); //发送的标题 mainMessage.setSubject(title); //发送的内容 mainMessage.setText(text); mailSender.send(mainMessage); return true; } /** * 发送消息给买家接口 * @throws InterruptedException */ public void sendToBuyerMsg(String sendBuyerJson) throws Exception { MsgWebSocketClient instance = MsgWebSocketClient.getInstance(); logger.info("instance:"+instance+"发送json内容"+sendBuyerJson); // if (instance.getReadyState().equals(WebSocket.READYSTATE.OPEN)) { // logger.info("连接还没有断开"+instance.getReadyState()+","+WebSocket.READYSTATE.OPEN);//NOT_YET_CONNECTED // } // else { // logger.info("连接断开了mq监听jianting"); //instance = MsgWebSocketClient.getInstance(); // } // if (readyState.equals(WebSocket.READYSTATE.OPEN)) { // logger.info("连接还没有断开"+readyState+","+WebSocket.READYSTATE.OPEN);//NOT_YET_CONNECTED // }else { // logger.info("连接已断开!"); // String websocketUrl = (String) com.example.demo.utils.PropertiesUtil.getByKey("websocketUrl"); // instance = new MsgWebSocketClient(websocketUrl); // instance.connect(); // } logger.info("发送旺旺消息给买家"); //回复消息内容格式{"loginuser":"卖家用户名","buyernick":"买家用户名","content":"消息内容","type":"MQHandAnswer"} instance.send(sendBuyerJson); } public void sendSSMToBuyerMsg(String string) { logger.info("发送短信消息给买家消息json:"+string); } }
参考:https://www.cnblogs.com/yuhuashang-edward/p/10076217.html
源码,是痛苦的,又是快乐的,如果没有这痛苦,也就没有了这快乐!