美文网首页
第一章:实现简单得邮件发送

第一章:实现简单得邮件发送

作者: 刘书生 | 来源:发表于2019-08-03 15:47 被阅读0次

一切得基础就是邮件发送,ok,我们先来搭建一个简单邮件发送

我用的是spring boot项目,所以这里只需要引入邮件发送得依赖即可

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

接下来,就先来进行下邮件发送得简单配置,我得配置文件格式是yml格式

image.png

解释下,这里几个核心配置:
1、host是你邮箱得stmp主机
2、port就是端口号
3、username是你邮箱账号
4、password这里QQ邮箱开通POP3/SMTP提供的授权码,如果邮箱服务商没有授权码,可以使用密码代替

看到这里,如果没有进行邮箱发送得小伙伴可能会问,hostport我去哪里取呢(有关发送经验得可以跳过这段)?

我这里以qq邮箱为例子

1.第一步,打开我们得qq邮箱,根据图片得1,2步骤,找到账户设置


image.png

2.翻到账户设置最下面


image.png image.png

3.打开stmp,你在这里就能拿到授权码

接下来就是上代码了,因为我考虑到后面要涉及到多人发送,这里就没有用JavaMailder这个类,而是用了MimeMessage

       Properties props = new Properties(){{
            // 设置邮件服务器主机名
            setProperty("spring.mail.host", "smtp.qq.com");
            // 发送邮件协议名称
            setProperty("spring.mail.protocol", "stmp");
        }};
        //根据环境配置设置session
        Session session = Session.getInstance(props);
        //消息发送主体
        MimeMessage message = new MimeMessage(session);
        message.setSubject("我是标题");
        message.setText("我是内容");
        message.setFrom(new InternetAddress("发送人邮箱"));
        message.setRecipients(Message.RecipientType.TO, "接收人邮箱");
        message.saveChanges();
        Transport trans = session.getTransport();
        trans.connect("smtp.qq.com", 587, "你的邮箱账号", "你的邮箱密码");
        trans.sendMessage(message, message.getAllRecipients());
        trans.close();

把上面得邮箱替换成你自己得就行,让我们来看一下执行效果


image.png

第二章:https://www.jianshu.com/p/ec06f2f24b1d

相关文章

网友评论

      本文标题:第一章:实现简单得邮件发送

      本文链接:https://www.haomeiwen.com/subject/bgmjdctx.html