当前位置:   article > 正文

发送邮件功能_bugzilla 502 invalid input from 112.24.197.217 to

bugzilla 502 invalid input from 112.24.197.217 to newxmesmtplogicsvrszb9-0.q

邮件功能

开启服务
开启自己邮箱的SMTP服务

设置 ——> 账户 ——> 开启服务
在这里插入图片描述

引入依赖
选用与Spring Boot整合的

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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

配置邮箱

分别配置邮箱的 域名/主机、端口、邮箱账号、邮箱密码、发送协议(加密)、发送时采用ssl连接的详细配置

注意:
username为自己的邮箱账号
password为邮箱的授权码
在这里插入图片描述

spring.mail.host=smtp.qq.com
spring.mail.port=465
spring.mail.username=******
spring.mail.password=******
spring.mail.protocol=smtps
spring.mail.properties.mail.smpt.ssl.enable=true
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

封装发送邮件的实体类,可以重复使用

注意依赖注入值:@Value("${配置文件的key}")

@Component
public class MailClient{
    /** 发送邮件的核心组件 Spring容器管理 */
    @Autowired
    private JavaMailSender mailSender;

    /** 发件人:properties文件中已经配置,可直接使用 */
    @Value("${spring.mail.username}")
    private String from;

    /**
     * 发送邮件的方法,外部调用
     *
     * @param to 发送目的地
     * @param subject 发送主题
     * @param context 发送内容
     */
    public void sendMail(String to, String subject, String context) {
        try {
            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message);
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            // 支持html文本
            helper.setText(context, true);
            mailSender.send(helper.getMimeMessage());
        } catch (MessagingException e) {
        	e.printStackTrace();
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

测试

测试中调用MailClient中的sendMail方法,即可发送成功

实现过程中的问题

  1. javax.mail.NoSuchProviderException: No provider for smpts
    org.springframework.mail.MailSendException: Mail server connection failed; nested exception is javax.mail.NoSuchProviderException: No provider for smpts. Failed messages: javax.mail.NoSuchProviderException: No provider for smpts
    ; message exception details (1) are:
    Failed message 1:

没有提供smpt
properties文件中的加密协议出错
应为 smtps

  1. 535 Login Fail.
    javax.mail.AuthenticationFailedException: 535 Login Fail. Please enter your authorization code to login. More information in http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256

535登录失败
properties文件spring.mail.password值出错,应为授权码

  1. 502 Invalid input from 118.74.26.137 to newxmesmtplogicsvrsza7.qq.com.

无效的输入,从 from 到 to
是 MailClient 的 String from 依赖注入的值错误
注意取properties文件中的值:${}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/300035
推荐阅读
相关标签
  

闽ICP备14008679号