阿里云禁用25端口,如何实现邮件的发送?

阿里云禁用25端口,如何实现邮件的发送?

一、导入依赖

本文是基于 SpringBoot 环境实现的邮件发送案例

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>

二、邮件参数配置

发送邮件时,需要遵循 SMTP 邮件协议。

之前购买了阿里云服务器,在本地做邮件发送功能时,是能够正常发送的,但部署到服务器上时,发送邮件总是连接超时,后来发现是阿里云把25端口禁用了,而我本地开发时使用的是25端口,所以连接超时!

既然25端口被禁用,那么我们就不使用25端口来发送邮件。这里以163邮箱为例:

SMTP的服务器地址是:smtp.163.com

SSL协议 端口号:465/994

非SSL协议 端口号:25

由于阿里云服务器禁用了25端口,所以我们选择使用基于SSL协议 的端口号 465或者994 端口

spring:
  mail:
  	# 服务器地址
    host: smtp.163.com
    # 所使用的协议,默认协议是smtp,其对应的是25端口;但这里由于使用465端口,所以需要更改协议为smtps
    protocol: smtps
    # 465端口是基于SSL协议的端口号,而25端口是基于非SSL协议的端口号
    port: 465
    # 163邮箱的用户名
    username: l18879676724@163.com
    # 163邮箱的密钥
    password: YTFEPMDQXMGOSAAP

三、编写邮件发送服务

  • 接口类
public interface EmailService {

    /**
     * 发送普通邮件
     * @param to 接收者邮箱
     * @param subject 主题
     * @param content 内容
     */
    void sendCustomEmail(String to, String subject, String content);

    /**
     * 发送复杂邮件
     * @param to 接收者邮箱
     * @param subject 主题
     * @param content 内容
     * @param isHtml 是否发送html格式邮件
     * @param attachs 需要发送的附件
     */
    void sendMimeEmail(String to, String subject, String content, Boolean isHtml, MultipartFile[] attachs) throws MessagingException;

}
  • 实现类
@Service
public class DefaultEmailService implements EmailService {

    private final JavaMailSender sender;

    @Value("${spring.mail.username}")
    private String username;

    public DefaultEmailService(JavaMailSender sender) {
        this.sender = sender;
    }

    @Override
    public void sendCustomEmail(String to, String subject, String content) {
        // 创建简单邮件消息对象
        SimpleMailMessage message = new SimpleMailMessage();
        // 设置发送者邮箱
        message.setFrom(username);
        // 设置接收者邮箱
        message.setTo(to);
        // 设置主题
        message.setSubject(subject);
        // 设置文本内容
        message.setText(content);
        // 发送邮件
        sender.send(message);
    }

    @Override
    public void sendMimeEmail(String to, String subject, String content, Boolean isHtml, MultipartFile[] attachs) throws MessagingException {
        // 创建复杂邮件消息对象
        MimeMessage mimeMessage = sender.createMimeMessage();
        // 第二个参数:是否支持多个附件的发送
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        // 设置发送者邮箱
        helper.setFrom(username);
        // 设置接收者邮箱
        helper.setTo(to);
        // 设置主题
        helper.setSubject(subject);
        // 设置内容(第二个参数表示是否是html内容,默认为false)
        helper.setText(content, isHtml);
        // 有附件需要发送
        if (attachs.length > 0){
            for (int i = 0; i < attachs.length; i++) {
                MultipartFile attach = attachs[i];
                // 添加附件(附件名称、附件)
                helper.addAttachment(Objects.requireNonNull(attach.getOriginalFilename()), attach);
            }
        }
        // 发送邮件
        sender.send(mimeMessage);
    }
}

四、提供对外访问的接口

@RestController
@RequestMapping("/email")
public class EmailController {

    private final EmailService emailService;

    public EmailController(EmailService emailService) {
        this.emailService = emailService;
    }

    @PostMapping("/sendCustom")
    public String sendCustomEmail(String to, String subject, String content){
        emailService.sendCustomEmail(to, subject, content);
        return "ok";
    }

    @PostMapping("/sendMime")
    public String sendMimeEmail(String to, String subject, String content, Boolean isHtml, MultipartFile[] attachs){
        try {
            emailService.sendMimeEmail(to, subject, content, isHtml, attachs);
            return "ok";
        } catch (MessagingException e) {
            e.printStackTrace();
            return "fail";
        }
    }
}

五、测试邮件发送

1. 发送普通邮件

使用 ApiPost 工具调用接口发送普通邮件

在这里插入图片描述

测试结果

在这里插入图片描述

2. 发送复杂邮件

使用 ApiPost 工具调用接口发送复杂邮件

在这里插入图片描述

测试结果

在这里插入图片描述

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

总是提示已注册

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值