关注WX:CodingTechWork

介绍

概述

  1. 在一些业务功能中,发送邮件是必须的,比如告警通知、日报、月报等信息推送,都需要通过邮件服务器来发送邮件。
  2. JavaMail 是 Java 平台上用于处理电子邮件的 API,可以用于发送、接收和处理电子邮件。

配置

mail.smtp.host: SMTP 服务器主机名或 IP 地址。
mail.smtp.port: SMTP 服务器端口号,默认为 25。
mail.smtp.auth: 指定是否需要进行 SMTP 服务器身份验证,通常设置为 "true"。
mail.smtp.user: SMTP 服务器登录用户名。
mail.smtp.password: SMTP 服务器登录密码。
mail.smtp.connectiontimeout: 连接超时时间,以毫秒为单位。
mail.smtp.timeout: I/O 超时时间,以毫秒为单位。
mail.smtp.starttls.enable: 指定是否启用 STARTTLS 加密,通常设置为 "true"。
mail.smtp.starttls.required: 指定是否要求使用 STARTTLS 加密,通常设置为 "true"。
mail.smtp.ssl.enable: 指定是否启用 SSL 加密,通常设置为 "true"。
mail.smtp.ssl.protocols: 指定 SMTP 客户端使用的 SSL 协议版本。
mail.smtp.ssl.trust: 指定 SMTP 客户端信任的服务器证书的名称或别名。
mail.smtp.socketFactory.class: 指定自定义的 SocketFactory 类。
mail.smtp.socketFactory.fallback: 指定是否在创建 SSL 套接字失败时回退到普通套接字。
mail.smtp.socketFactory.port: 指定自定义 SocketFactory 类所使用的端口。
mail.debug: 是否启用调试模式,用于输出调试信息。
mail.mime.charset: 指定邮件的字符编码。
mail.smtp.connectionpoolsize: 指定连接池大小,用于控制与 SMTP 服务器的连接数。

代码模板

依赖

        <dependency>
            <groupId>com.sun.mail</groupId>
            <artifactId>javax.mail</artifactId>
            <version>1.6.2</version> 
        </dependency>

代码实现

package com.test.selfcoding.service;


import javax.mail.*;
import javax.mail.internet.*;

import java.util.Properties;
/**
 * @Description
 * @Author LiaoJy
 * @Date 2024/4/7
 */
public class EmailSender {
    public static void main(String[] args) {
        // 收件人邮箱地址
        String to = "recipient@example.com";
        
        // 抄送人邮箱地址
        String cc = "cc@example.com";
        
        // 密送人邮箱地址
        String bcc = "bcc@example.com";

        // 发件人邮箱地址
        String from = "sender@example.com";

        // 邮件服务器主机名
        String host = "smtp.example.com";

        // 使用的电子邮件协议
        String protocol = "smtp";

        // 邮件服务器需要认证时的用户名和密码
        final String username = "user_name";
        final String password = "user_password";

        // 设置Java Mail属性
        Properties properties = System.getProperties();
        // smtp host
        properties.setProperty("mail.smtp.host", host);
        // smtp 认证开关
        properties.setProperty("mail.smtp.auth", "true");
        // 邮件服务器端口号
        properties.setProperty("mail.smtp.port", "587");
        // 协议
        properties.setProperty("mail.transport.protocol", protocol);
        // 启用 STARTTLS(安全套接层传输协议)开关
        properties.setProperty("mail.smtp.starttls.enabled", "true");
        // STARTTLS(安全套接层传输协议)是否必须开关,确保与 SMTP 服务器之间的通信始终都是加密的
        properties.setProperty("mail.smtp.starttls.required", "true");
        // 指定用于 SMTP 客户端与服务器之间通信的 SSL 协议版本
        properties.setProperty("mail.smtp.ssl.protocols", "TLSv1.2");

        // 创建会话对象
        Session session = Session.getInstance(properties, new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
        });

        try {
            // 创建默认的 MimeMessage 对象
            MimeMessage message = new MimeMessage(session);

            // 设置发件人
            message.setFrom(new InternetAddress(from));

            // 设置收件人
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

            // 设置抄送人
            message.addRecipient(Message.RecipientType.CC, new InternetAddress(cc));

            // 设置密送人
            message.addRecipient(Message.RecipientType.BCC, new InternetAddress(bcc));

            // 设置主题
            message.setSubject("This is the Subject Line!");

            // 设置邮件内容
            message.setText("This is test message");

            // 发送消息
            Transport.send(message);
            System.out.println("Sent message successfully....");
        } catch (MessagingException mex) {
            mex.printStackTrace();
        }
    }
}

运行结果

Sent message successfully....
Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐