邮件发送的实现

首先要开启QQ/网易等邮箱的POP3/SMTP服务

导入依赖包

<!-- https://mvnrepository.com/artifact/javax.mail/mail -->
<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>mail</artifactId>
    <version>1.4.7</version>
</dependency>

<!-- https://mvnrepository.com/artifact/javax.activation/activation -->
<dependency>
    <groupId>javax.activation</groupId>
    <artifactId>activation</artifactId>
    <version>1.1.1</version>
</dependency>


在这里插入图片描述

简单邮件发送

public class Mail01 {
    //简单邮件
    public static void main(String[] args) throws Exception {

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

        //使用qq邮箱,还需要设置SSL加密
        MailSSLSocketFactory mailSSLSocketFactory = new MailSSLSocketFactory();
        mailSSLSocketFactory.setTrustAllHosts(true);
        properties.put("mail.smtp.ssl.enable","true");
        properties.put("mail.smtp.ssl.socketFactory",mailSSLSocketFactory);


        //使用javaMail发送邮件的5个步骤
            //1、创建定义整个应用程序的环境信息的session对象

        Session session = Session.getDefaultInstance(properties, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("xxx@qq.com","授权码/密码");
            }
        });
        //开启session的debug模式,这样就可以查看到程序发送Email的运行状态
        session.setDebug(true);

            //2、通过session得到transport对象
        Transport transport = session.getTransport();
        //3、使用邮箱的用户名和授权码连接上邮件服务器
        transport.connect("smtp.qq.com","xxx@qq.com","授权码/密码");
            //4、创建邮箱
        MimeMessage mimeMessage = new MimeMessage(session);
        mimeMessage.setFrom(new InternetAddress("xxx@qq.com"));       //发件人
        mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress("xxx@qq.com"));    //收件人
        mimeMessage.setSubject("第一封java邮件");    //邮件标题
        mimeMessage.setContent("你好,麻花疼","text/html;charset=utf-8");
        //5、发送邮箱
        transport.sendMessage(mimeMessage,mimeMessage.getAllRecipients());

        transport.close();
    }
}

带图片的邮件实现(只有邮件内容不同)

public class Mail02 {
    //带图片的邮件
    public static void main(String[] args) throws Exception {

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

        //使用qq邮箱,还需要设置SSL加密
        MailSSLSocketFactory mailSSLSocketFactory = new MailSSLSocketFactory();
        mailSSLSocketFactory.setTrustAllHosts(true);
        properties.put("mail.smtp.ssl.enable","true");
        properties.put("mail.smtp.ssl.socketFactory",mailSSLSocketFactory);


        //使用javaMail发送邮件的5个步骤
        //1、创建定义整个应用程序的环境信息的session对象
        Session session = Session.getDefaultInstance(properties, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("xxx@qq.com","授权码");
            }
        });
        //开启session的debug模式,这样就可以查看到程序发送Email的运行状态
        session.setDebug(true);


        //2、通过session得到transport对象
        Transport transport = session.getTransport();


        //3、使用邮箱的用户名和授权码连接上邮件服务器
        transport.connect("smtp.qq.com","xxx@qq.com","授权码");


        //4、创建邮件
        MimeMessage mimeMessage = new MimeMessage(session);
        mimeMessage.setFrom(new InternetAddress("xxx@qq.com"));       //发件人
        mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress("xxx@qq.com"));    //收件人
        mimeMessage.setSubject("第一封java邮件");    //邮件标题
        //===============================邮件内容================================
        //准备图片数据
        MimeBodyPart image = new MimeBodyPart();
        //图片需要经过数据处理
        DataHandler dataHandler = new DataHandler(new FileDataSource("src/resources/1.jpg"));   //加载文件资源
        image.setDataHandler(dataHandler);      //在主体中放入处理的图片数据
        image.setContentID("abc");     //给图片设置一个ID

        //准备正文数据
        MimeBodyPart text = new MimeBodyPart();
        text.setContent("一封带图片的邮件<img src='cid:abc'>","text/html;charset=utf-8");

        //正文和图片添加到主体中
        MimeMultipart mimeMultipart = new MimeMultipart();
        mimeMultipart.addBodyPart(text);
        mimeMultipart.addBodyPart(image);
        mimeMultipart.setSubType("related");

        mimeMessage.setContent(mimeMultipart);
        mimeMessage.saveChanges();  //保存修改
        //=============================邮件内容结束================================

        //5、发送邮箱
        transport.sendMessage(mimeMessage,mimeMessage.getAllRecipients());

        transport.close();
    }
}

java web实现邮箱注册

待续…

posted @ 2022-02-11 12:07  ︶ㄣ演戲ㄣ  阅读(3)  评论(0编辑  收藏  举报  来源