栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot组件(基于springboot的项目)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot组件(基于springboot的项目)

目录

一、集成JavaMail意义二、springboot集成JavaMail

2.1、集成JavaMail代码2.2、模拟结果

一、集成JavaMail意义

在一些重要业务功能上,当某些场景触发需要通知,那么邮件通知功能必不可少。下面会介绍JavaMail邮件。

二、springboot集成JavaMail 2.1、集成JavaMail代码

引用jar包

    
        
            org.springframework.boot
            spring-boot-starter-mail
            2.1.0.RELEASE
        

具体Socket代码实现

public class JavaSocket {

    private static final String emailKey = "****"; //发件人邮箱的授权码
    private static final String emailId_send = "***@qq.com";
    private static final String emailId_receicve = "***@qq.com";

    public static Properties getProperties() throws Exception{
        Properties prop = new Properties();
        prop.setProperty("mail.host", "smtp.qq.com");
//        设置QQ邮件服务器
        prop.setProperty("mail.transport.protocol", "smtp"); // 邮件发送协议
        prop.setProperty("mail.smtp.auth", "true"); // 需要验证用户名密码

        // 关于QQ邮箱,还要设置SSL加密,加上以下代码即可
        MailSSLSocketFactory sf = new MailSSLSocketFactory();
        sf.setTrustAllHosts(true);
        prop.put("mail.smtp.ssl.enable", "true");
        prop.put("mail.smtp.ssl.socketFactory", sf);
        return prop;
    }

    
    public static void sendMail(EmailModel emailModel,int type) throws Exception{

        //使用JavaMail发送邮件的5个步骤
        //1、创建定义整个应用程序所需的环境信息的 Session 对象
        Session session = Session.getDefaultInstance(getProperties(), new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication() {
                //发件人邮件用户名、授权码
                return new PasswordAuthentication(emailId_send, emailKey);
            }
        });

        //开启Session的debug模式,这样就可以查看到程序发送Email的运行状态
        session.setDebug(true);
        //2、通过session得到transport对象
        Transport ts = session.getTransport();
        //3、使用邮箱的用户名和授权码连上邮件服务器
        ts.connect("smtp.qq.com", emailId_send, emailKey);
        //4、创建邮件
           //创建邮件对象
        MimeMessage message = new MimeMessage(session);
           //指明邮件的发件人
        message.setFrom(new InternetAddress(emailId_send));
          //指明邮件的收件人,现在发件人和收件人是一样的,那就是自己给自己发
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(emailId_receicve));
         //邮件的封装 【纯文本邮件、图片邮件等】
//        messageWord(message,emailModel);
        if(type == 1) messageWord(message,emailModel);
        else if(type ==2) messagePic(message,emailModel);
        //5、发送邮件
        ts.sendMessage(message, message.getAllRecipients());
        // re=ts
        ts.close();
    }

    //纯文本邮件
    public static void messageWord(MimeMessage message,EmailModel emailModel) throws Exception {
        //邮件的标题
        message.setSubject(emailModel.getTitle());
        //邮件的文本内容
        message.setContent(emailModel.getMessage(), "text/html;charset=UTF-8");
    }
    //图片邮件
    public static void messagePic(MimeMessage message,EmailModel emailModel) throws Exception {
        //邮件标题
        message.setSubject(emailModel.getTitle());
        // 准备图片数据
        MimeBodyPart image = new MimeBodyPart();
        DataHandler dh = new DataHandler(new FileDataSource("D:\Users\Desktop\广告图片.png"));
        image.setDataHandler(dh);
        image.setContentID("pic1.png");

        // 准备正文数据
        MimeBodyPart text1 = new MimeBodyPart();
        text1.setContent("这是一封邮件正文带图片的邮件", "text/html;charset=UTF-8");

        //附件
        MimeBodyPart body3 = new MimeBodyPart();
        body3.setDataHandler(new DataHandler(new FileDataSource("D:\Users\Desktop\log4j.properties")));
        body3.setFileName("log4j.properties"); //附件设置名字

        // 描述数据关系
        MimeMultipart mm = new MimeMultipart();
        mm.addBodyPart(text1);
        mm.addBodyPart(image);
        mm.setSubType("related");

        MimeBodyPart contentText =  new MimeBodyPart();
        contentText.setContent(mm);

        //拼接附件
        MimeMultipart allFile =new MimeMultipart();
        allFile.addBodyPart(body3);  //附件
        allFile.addBodyPart(contentText); //正文
        allFile.setSubType("mixed");

        //设置到消息中,保存修改
        message.setContent(allFile);
        message.saveChanges();
    }



    public static void main(String[] args) {
        try {
            JavaSocket.sendMail(new EmailModel("title", "message","***@qq.com"),2);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

EmailModel类

public class EmailModel {
    private String title;  //邮件标题
    private String message; //邮件内容
    private String emailReciver; //邮件收件人
    public EmailModel(String title, String message, String emailReciver){
        this.title = title;
        this.message = message;
        this.emailReciver = emailReciver;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
    public String getEmailReciver() {
        return emailReciver;
    }
    public void setEmailReciver(String emailReciver) {
        this.emailReciver = emailReciver;
    }
}
2.2、模拟结果

纯文本邮件

文本+图片邮件

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/773477.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号