springcloud整合email真的是搞得我腦殼痛,因為我需要注冊的時候通過rabbitmq給用戶發一封郵件,因為這個報錯的原因導致我mq一直監聽失敗然后就開始了循環發消息,這就導致郵箱一直在不停的發。
話不多說直接給解決方案。
需要的依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--email配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
email服務郵箱的yml
其中 protocol: smtp的配置查了好多博客,一些說使用465需要smtps,經過我的測試好像都可以沒什么區別
spring:
# 郵箱配置
mail:
# 配置 SMTP 服務器地址
host: smtp.qq.com
# 發送者郵箱
username: xxxxxxx@qq.com
# 配置密碼,注意這不是真正的密碼,而是剛剛申請到的16位授權碼
password: nxarhendrqmlbddd
# 端口號465或587
port: 465
# 默認的郵件編碼為UTF-8
default-encoding: UTF-8
#這里完全可以直接用smtp
protocol: smtp
test-connection: false
properties:
mail:
debug: true
smtp:
ssl:
enable: true
auth: true
socketFactory:
#這個可要可不要
# class: javax.net.ssl.SSLSocketFactory
port: 465
starttls:
enable: true
required: true
郵件發送
import com.cn.me.result.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
/**
* @author Dshzs月
* @version 1.0.0
* @ClassName SendEmailController.java
* @Description TODO
* @createTime 2022年03月24日 10:36:00
*/
@RestController
public class SendEmailController {
@Autowired
private JavaMailSender javaMailSender;
@Value("${spring.mail.username}")
private String name;
// 上傳文件存儲目錄
@RequestMapping("/email")
public void email(@RequestParam("email") String email,@RequestParam("title") String title,@RequestParam("content") String content) {
// 構建一個郵件對象
SimpleMailMessage message = new SimpleMailMessage();
// 設置郵件主題
message.setSubject(title);
// 設置郵件發送者,這個跟application.yml中設置的要一致
message.setFrom(name);
// 設置郵件接收者,可以有多個接收者,中間用逗號隔開,以下類似
// message.setTo("10*****16@qq.com","12****32*qq.com");
message.setTo(email);
// 設置郵件抄送人,可以有多個抄送人
//message.setCc("12****32*qq.com");
// 設置隱秘抄送人,可以有多個
//message.setBcc("7******9@qq.com");
// 設置郵件發送日期
message.setSentDate(new Date());
// 設置郵件的正文
message.setText(content);
// 發送郵件
javaMailSender.send(message);
}
}
應為我是通過feign調用那么也貼一些feign
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
/**
* @author Dshzs月
* @version 1.0.0
* @ClassName SendEmailFeignClient.java
* @Description TODO
* @createTime 2022年03月25日 14:26:00
*/
@FeignClient(value = "email-server")
public interface SendEmailFeignClient {
@RequestMapping(value = "email", consumes = "application/json")
void email(@RequestParam("visitoremail") String email, @RequestParam("title") String title, @RequestParam("content") String content);
}
消費端調用
import com.cn.me.feign.SendEmailFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* @author Dshzs月
* @version 1.0.0
* @ClassName Tool.java
* @Description TODO
* @createTime 2022年03月25日 14:25:00
*/
@Component
public class Tool {
@Autowired
private SendEmailFeignClient sendEmailFeignClient;
/**
* 發送郵件的方法
*
* @param email
*/
public void sendEmails(String username, String email) {
//定義標題
String title = "歡迎加入我們!";
//定義內容
String content = "您好,您在婚紗殿的賬戶激活成功,您的登錄用戶名是: " +username+
",謝謝您的青睞 0^0!";
//通過sendEmailController調用email方法
sendEmailFeignClient.email(email, title, content);
}
mq就不貼了。
總結:導致(Unrecognized SSL message, plaintext connection?)的元凶就是ssl需要開啟,協議http和https是不同的,然后端口不能用587
