Java发送邮件

一、邮件协议

电子邮件的在网络中传输和网页一样需要遵从特定的协议,常用的电子邮件协议包括 SMTP,POP3,IMAP。其中邮件的创建和发送只需要用到 SMTP协议,SMTP 是 Simple Mail Transfer Protocol 的简称,即简单邮件传输协议

二、JavaMail

JavaMail 是 Java 官方提供对电子邮件协议封装的 Java 类库

<!-- JavaMail -->
<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>mail</artifactId>
    <version>1.5.0-b01</version>
</dependency>
三、代码实现

邮件工具类

import java.util.Date;
import java.util.Properties;

import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class MailUtils {

    public static void sendMessage(InternetAddress from, InternetAddress to, String subject, String info, String password, Properties properties) throws Exception {
        sendMessage(from, to, null, subject, info, password, properties);
    }

    public static void sendMessage(InternetAddress from, InternetAddress to, InternetAddress cc, String subject, String info, String password, Properties properties) throws Exception {
        sendMessage(from, to, cc, null, subject, info, password, properties);
    }

    /**
     * 发送邮件
     * 
     * @param from       发件人
     * @param to         收件人(可以多个)
     * @param cc         抄送人(可以多个)
     * @param bcc        密送人(可以多个)
     * @param subject    邮件主题 
     * @param info       邮件内容
     * @param password   发件人邮件授权码
     * @param properties 
     */
    public static void sendMessage(InternetAddress from, InternetAddress to, InternetAddress cc, InternetAddress bcc, String subject, String info, String password, Properties properties) throws Exception {
        // 根据配置创建会话对象, 用于和邮件服务器交互
        Session session = Session.getDefaultInstance(properties);
        // 设置为debug模式, 可以查看详细的发送 log
        session.setDebug(false);
        // 创建一封邮件
        MimeMessage message = new MimeMessage(session);
        // From: 发件人
        message.setFrom(from);
        // To: 收件人
        if (to != null) {
            message.setRecipient(MimeMessage.RecipientType.TO, to);
        }
        // Cc: 抄送(可选)
        if (cc != null) {
            message.setRecipient(MimeMessage.RecipientType.CC, cc);
        }
        // Bcc: 密送(可选)
        if (bcc != null) {
            message.setRecipient(MimeMessage.RecipientType.BCC, bcc);
        }
        // Subject: 邮件主题
        message.setSubject(subject, "UTF-8");
        // Content: 邮件正文(可以使用html标签)
        message.setContent(info, "text/html;charset=UTF-8");
        // 设置发件时间
        message.setSentDate(new Date());
        // 保存设置
        message.saveChanges();
        // 根据 Session 获取邮件传输对象
        Transport transport = session.getTransport();
        // 使用 邮箱账号 和 密码 连接邮件服务器, 这里认证的邮箱必须与 message 中的发件人邮箱一致, 否则报错
        transport.connect(from.getAddress(), password);
        // 发送邮件, 发到所有的收件地址, message.getAllRecipients() 获取到的是在创建邮件对象时添加的所有收件人, 抄送人, 密送人
        transport.sendMessage(message, message.getAllRecipients());
        // 关闭连接
        transport.close();
    }
}

发送邮件

import java.util.Properties;

import javax.mail.internet.InternetAddress;

public class Email {

    public static void main(String[] args) throws Exception {
        // 创建参数配置, 用于连接邮件服务器的参数配置
        Properties properties = new Properties();
        properties.setProperty("mail.transport.protocol", "smtp"); // 使用的协议(JavaMail规范要求)
        properties.setProperty("mail.smtp.host", "smtp.163.com"); // 发件人的邮箱的 SMTP 服务器地址
        properties.setProperty("mail.smtp.auth", "true"); // 需要请求认证
        // SMTP 服务器的端口
        // properties.setProperty("mail.smtp.port", "465"); // SSL 安全认证
        // properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        // properties.setProperty("mail.smtp.socketFactory.fallback", "false");
        // properties.setProperty("mail.smtp.socketFactory.port", "465");

        // 发送人邮箱信息
        InternetAddress from = new InternetAddress("这里填写发送人邮箱地址", "发送人邮件昵称", "UTF-8");
        String password = ""; // 邮箱授权码

        // 接收人邮箱信息
        InternetAddress to = new InternetAddress("这里填写接收人邮箱地址", "接收人邮件昵称", "UTF-8");

        String subject = "邮件的主题";
        String info = "邮件的内容";

        // 发送邮件
        MailUtils.sendMessage(from, to, subject, info, password, properties);
    }

}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值