电子邮件

image.png

传输协议

邮件接收协议:POP3协议

邮件发送协议:SMTP协议

image.png

概述

image.png
image.png
image.png

文本邮件

创建Java工程,导入jar

image.png

开启POP3/SMTP服务​

image.png

package com.qing;

import com.sun.mail.util.MailSSLSocketFactory;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.security.GeneralSecurityException;
import java.util.Properties;

/**
 * 简单邮件:没有附件和图片,纯文本邮件
 */
public class MailDemo01 {
    public static void main(String[] args) throws GeneralSecurityException, MessagingException {
        // 要发送邮件,需要获得协议和支持,即开启POP3/SMTP服务
        // QQ邮箱开启POP3/SMTP服务,授权码:授权码

        Properties prop = new Properties();
        prop.setProperty("mail.host","smtp.qq.com"); // 设置邮件服务器
        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);

        // 使用JavaMail发送邮件的5个步骤
        // 1.创建定义整个应用程序所需的环境信息的Session对象
        // QQ邮箱才需要,其他邮箱不需要
        Session session = Session.getDefaultInstance(prop, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                // 发件人邮件用户名、授权码
                return new PasswordAuthentication("QQ@qq.com", "授权码");
            }
        });
//        FGRXSBNPAPTGYDVT
        // 开启session的debug模式,就可以查看程序发送email的运行状态
        session.setDebug(true);

        // 2.通过Session对象得到transport对象
        Transport ts = session.getTransport();
        // 3.使用邮箱的用户名和授权码脸上邮件服务器
        ts.connect("smtp.qq.com","QQ@qq.com","授权码");
        // 4.创建邮件
        MimeMessage msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress("QQ@qq.com"));// 发件人
        // TO表示主要接收人,CC表示抄送人,BCC表示秘密抄送人
        msg.setRecipient(Message.RecipientType.TO,new InternetAddress("163@163.com")); // 收件人
        msg.setSubject("我是主题"); // 邮件主题
        msg.setContent("我是内容","text/html;charset=UTF-8"); // 邮件内容
        // 5.发送邮件
        ts.sendMessage(msg,msg.getAllRecipients());
        // 6.关闭连接
        ts.close();
    }
}

复杂邮件

image.png


image.png

package com.qing;

import com.sun.mail.util.MailSSLSocketFactory;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.security.GeneralSecurityException;
import java.util.Properties;

/**
 * 复杂邮件:带附件和图片的邮件
 */
public class MailDemo02 {
    public static void main(String[] args) throws GeneralSecurityException, MessagingException {
        // 要发送邮件,需要获得协议和支持,即开启POP3/SMTP服务
        // QQ邮箱开启POP3/SMTP服务,授权码:授权码

        Properties prop = new Properties();
        prop.setProperty("mail.host","smtp.qq.com"); // 设置邮件服务器
        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);

        // 使用JavaMail发送邮件的5个步骤
        // 1.创建定义整个应用程序所需的环境信息的Session对象
        // QQ邮箱才需要,其他邮箱不需要
        Session session = Session.getDefaultInstance(prop, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                // 发件人邮件用户名、授权码
                return new PasswordAuthentication("QQ@qq.com", "授权码");
            }
        });
//        FGRXSBNPAPTGYDVT
        // 开启session的debug模式,就可以查看程序发送email的运行状态
        session.setDebug(true);

        // 2.通过Session对象得到transport对象
        Transport ts = session.getTransport();
        // 3.使用邮箱的用户名和授权码脸上邮件服务器
        ts.connect("smtp.qq.com","QQ@qq.com","授权码");
        // 4.创建邮件
        MimeMessage msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress("QQ@qq.com"));// 发件人
        // TO表示主要接收人,CC表示抄送人,BCC表示秘密抄送人
        msg.setRecipient(Message.RecipientType.TO,new InternetAddress("163@163.com")); // 收件人
        msg.setSubject("我是复杂邮件"); // 邮件主题

        // 准备图片数据
        MimeBodyPart image = new MimeBodyPart();
        // 图片需要经过数据处理 DataHandler:数据处理
        DataHandler dh = new DataHandler(new FileDataSource("src/1.jpg"));
        image.setDataHandler(dh); // 放入处理的图片数据
        image.setContentID("1.jpg"); // 给图片设置ID,后面正文数据中可以使用cid使用
        // 准备正文数据
        MimeBodyPart text = new MimeBodyPart();
        // 使用cid使用前面的图片数据
        text.setContent("这是带图片<img src='cid:1.jpg'>的邮件","text/html;charset=UTF-8");
        // 描述数据关系
        MimeMultipart mm = new MimeMultipart();
        mm.addBodyPart(text);
        mm.addBodyPart(image);
        mm.setSubType("related");
        // 设置到消息中,保存修改
        msg.setContent(mm);
        msg.saveChanges();

        // 5.发送邮件
        ts.sendMessage(msg,msg.getAllRecipients());
        // 6.关闭连接
        ts.close();
    }
}

image.png
image.png

注册发送邮件

image.png

工具类:多线程实现用户体验

image.png
image.png
image.png

发送邮件

image.png

springboot实现发送邮件

pom.xml添加依赖

image.png

配置文件

image.png

发送邮件

image.png
image.png

内容来源于网络如有侵权请私信删除
你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!