• 邮件发送


    概述

    以QQ邮箱示例

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

    <!-- https://mvnrepository.com/artifact/javax.mail/mail -->
    <dependency>
      <groupId>javax.mail</groupId>
      <artifactId>mail</artifactId>
      <version>1.4.7</version>
    </dependency>
    
    • 发送协议SMTP
    • 接收协议POP3

    发送文本邮件

    @Test
    public void testMail() throws Exception {
        Properties prop = new Properties();
        prop.setProperty("mail.host", "smtp.qq.com"); // 邮件服务器
        prop.setProperty("mail.transport.protocol", "smtp"); // 邮件协议
        prop.setProperty("mail.smtp.auth", "true"); // 是否验证用户名密码
    
        // SSL加密
        MailSSLSocketFactory sf = new MailSSLSocketFactory();
        sf.setTrustAllHosts(true);
        prop.setProperty("mail.smtp.ssl.enable", "true");
        prop.put("mail.smtp.ssl.socketFactory", sf);
    
        // 1.创建应用程序环境信息session对象
        Session session = Session.getDefaultInstance(prop, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                // qq邮箱用户名,授权码
                return new PasswordAuthentication("xxx@qq.com", "mhcxadh");
            }
        });
        session.setDebug(true); // 开启debug模式
    
        // 2.通过session得到transport对象
        Transport ts = session.getTransport();
    
        // 3.使用邮箱的用户名和授权码连上邮箱服务器
        ts.connect("smtp.qq.com", "xxx@qq.com", "mcadh");
    
        // 4.创建邮件
        // 4.1 创建邮件对象
        MimeMessage message = new MimeMessage(session);
        // 4.2 指定发件人
        message.setFrom(new InternetAddress("xxx@qq.com"));
        // 4.3 指明发件人
        message.setRecipient(Message.RecipientType.TO, new InternetAddress("xxx@icloud.com"));
        // 4.4 邮件的标题
        message.setSubject("邮件标题测试");
        // 4.5 邮件正文
        message.setContent("你好", "text/html;charset=utf-8");
    
        // 5.发送邮件
        ts.sendMessage(message, message.getAllRecipients());
    
        // 6.关闭连接
        ts.close();
    }
    

    正文嵌入图片

    @Test
    public void testMail() throws Exception {
        Properties prop = new Properties();
        prop.setProperty("mail.host", "smtp.qq.com"); // 邮件服务器
        prop.setProperty("mail.transport.protocol", "smtp"); // 邮件协议
        prop.setProperty("mail.smtp.auth", "true"); // 是否验证用户名密码
    
        // SSL加密
        MailSSLSocketFactory sf = new MailSSLSocketFactory();
        sf.setTrustAllHosts(true);
        prop.setProperty("mail.smtp.ssl.enable", "true");
        prop.put("mail.smtp.ssl.socketFactory", sf);
    
        // 1.创建应用程序环境信息session对象
        Session session = Session.getDefaultInstance(prop, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                // qq邮箱用户名,授权码
                return new PasswordAuthentication("xxxxx@qq.com", "mdh");
            }
        });
        session.setDebug(true);
    
        // 2.通过session得到transport对象
        Transport ts = session.getTransport();
    
        // 3.使用邮箱的用户名和授权码连上邮箱服务器
        ts.connect("smtp.qq.com", "xxxxx@qq.com", "mhdh");
    
        // 4.创建邮件
        // 4.1 创建邮件对象
        MimeMessage message = new MimeMessage(session);
        // 4.2 指定发件人
        message.setFrom(new InternetAddress("xxxxx@qq.com"));
        // 4.3 指明发件人
        message.setRecipient(Message.RecipientType.TO, new InternetAddress("xxxxx@icloud.com"));
        // 4.4 邮件的标题
        message.setSubject("邮件标题测试");
    
        // ========================================================
        // 准备图片数据
        // target/Servlet/WEB-INF/classes/动漫头像.jpg
        MimeBodyPart image = new MimeBodyPart();
        DataHandler dh = new DataHandler(new FileDataSource("/Users/collin/IdeaProjects/Kuang/JavaWeb02-muti-maven/Servlet/target/Servlet/WEB-INF/classes/动漫头像.jpg"));
        image.setDataHandler(dh);
        image.setContentID("dm.jpg");
    
        // 准备邮件正文
        MimeBodyPart text = new MimeBodyPart();
        text.setContent("带图片邮件<img src='cid:dm.jpg'>", "text/html;charset=utf-8");
    
        // 描述数据关系
        MimeMultipart mm = new MimeMultipart();
        mm.addBodyPart(text);
        mm.addBodyPart(image);
        mm.setSubType("related");
    
        // 保存到消息中
        message.setContent(mm);
        message.saveChanges();
        // ========================================================
        
        // 5.发送邮件
        ts.sendMessage(message, message.getAllRecipients());
    
        // 6.关闭连接
        ts.close();
    }
    

    附件

    @Test
    public void testMail() throws Exception {
        Properties prop = new Properties();
        prop.setProperty("mail.host", "smtp.qq.com"); // 邮件服务器
        prop.setProperty("mail.transport.protocol", "smtp"); // 邮件协议
        prop.setProperty("mail.smtp.auth", "true"); // 是否验证用户名密码
    
        // SSL加密
        MailSSLSocketFactory sf = new MailSSLSocketFactory();
        sf.setTrustAllHosts(true);
        prop.setProperty("mail.smtp.ssl.enable", "true");
        prop.put("mail.smtp.ssl.socketFactory", sf);
    
        // 1.创建应用程序环境信息session对象
        Session session = Session.getDefaultInstance(prop, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                // qq邮箱用户名,授权码
                return new PasswordAuthentication("xxxxx@qq.com", "mhdh");
            }
        });
        session.setDebug(true);
    
        // 2.通过session得到transport对象
        Transport ts = session.getTransport();
    
        // 3.使用邮箱的用户名和授权码连上邮箱服务器
        ts.connect("smtp.qq.com", "xxxxx@qq.com", "mhcadh");
    
        // 4.创建邮件
        // 4.1 创建邮件对象
        MimeMessage message = new MimeMessage(session);
        // 4.2 指定发件人
        message.setFrom(new InternetAddress("xxxxx@qq.com"));
        // 4.3 指明发件人
        message.setRecipient(Message.RecipientType.TO, new InternetAddress("xxxxx@icloud.com"));
        // 4.4 邮件的标题
        message.setSubject("邮件标题测试");
    
        // ========================================================
        // 准备图片数据
        // target/Servlet/WEB-INF/classes/动漫头像.jpg
        MimeBodyPart image = new MimeBodyPart();
        DataHandler dh = new DataHandler(new FileDataSource("/Users/collin/IdeaProjects/Kuang/JavaWeb02-muti-maven/Servlet/target/Servlet/WEB-INF/classes/动漫头像.jpg"));
        image.setDataHandler(dh);
        image.setContentID("dm.jpg");
    
        // 准备邮件正文
        MimeBodyPart text = new MimeBodyPart();
        text.setContent("带图片邮件<img src='cid:dm.jpg'>", "text/html;charset=utf-8");
    
        // 准备附件1
        MimeBodyPart attachFile1 = new MimeBodyPart();
        attachFile1.setDataHandler(new DataHandler(new FileDataSource("/Users/collin/IdeaProjects/Kuang/JavaWeb02-muti-maven/Servlet/target/Servlet/WEB-INF/classes/动漫头像.jpg")));
        attachFile1.setFileName("图片1.jpg");
        // 准备附件2
        MimeBodyPart attachFile2 = new MimeBodyPart();
        attachFile2.setDataHandler(new DataHandler(new FileDataSource("/Users/collin/IdeaProjects/Kuang/JavaWeb02-muti-maven/Servlet/target/Servlet/WEB-INF/classes/动漫头像.jpg")));
        attachFile2.setFileName("图片2.jpg");
    
        // 拼接正文数据关系
        MimeMultipart mm = new MimeMultipart();
        mm.addBodyPart(text);
        mm.addBodyPart(image);
        mm.setSubType("related");
    
        MimeBodyPart contextText = new MimeBodyPart();
        contextText.setContent(mm);
    
        // 拼接附件
        MimeMultipart allFile = new MimeMultipart();
        allFile.addBodyPart(attachFile1);
        allFile.addBodyPart(attachFile2);
        allFile.addBodyPart(contextText);
        allFile.setSubType("mixed");
    
        // 保存到消息中
        message.setContent(allFile);
        message.saveChanges();
        // ========================================================
    
        // 5.发送邮件
        ts.sendMessage(message, message.getAllRecipients());
    
        // 6.关闭连接
        ts.close();
    }
    
  • 相关阅读:
    微软企业库调用Oracle存储过程返回(1个或多个)数据集
    (转)Oracle表空间
    HTML5操作
    完美实现 ASP.NET 2.0 中的URL重写伪静态(映射) &gt;(转载)的简介与内容
    HTML5 audio 详解
    步步为营:Asp.Net使用HttpWebRequest通知,抓取,采集(转)
    js cookie操作
    多线线程
    js with用法
    asp.net AllowSorting="true"仍然不能排序的原因
  • 原文地址:https://www.cnblogs.com/shenleg/p/14274863.html
Copyright © 2020-2023  润新知