程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

2023-06(2)

130_邮件发送

发布于2021-06-14 16:53     阅读(197)     评论(0)     点赞(0)     收藏(0)



电子邮件

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



所属网站分类: 技术文章 > 博客

作者:哦哦好吧

链接:http://www.javaheidong.com/blog/article/222788/f1229567397528a2c82d/

来源:java黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

0 0
收藏该文
已收藏

评论内容:(最多支持255个字符)