java 发送邮件mail

​​​# 题外话
> 如果各位客官有需要开发一些小小需求,可以私我哦,承接小需求开发,或问题定位(仅限java),价格私聊哈

一、导入Maven依赖

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


二、编写发送邮件工具类

import com.sun.mail.util.MailSSLSocketFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

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

public class MailUtil {
    /**
     * 发件人邮箱
     */
    private String fromAddress = "";
    /**
     * 发送的服务器,139服务器:smtp.139.com
     */
    private String host = "";
    /**
     * qq需要用授权码(在ie浏览器打开qq邮箱网址,然后申请),大部分邮箱都是直接用密码
     * 139邮箱用密码
     */
    private String authCode = "";

    /**
     * 给单独一个人发送邮件
     *
     * @param subject   邮件标题
     * @param content   邮件内容
     * @param toAddress 收件人邮件地址
     */
    public void sendEmail(String subject, String content, String toAddress) throws GeneralSecurityException, MessagingException {
        List<String> toAddresses = new ArrayList<>();
        toAddresses.add(toAddress);
        this.sendEmail(subject, content, toAddresses);
    }

    /**
     * 给多个人发送同一封邮件
     *
     * @param subject     邮件标题
     * @param content     邮件内容
     * @param toAddresses 收件人邮件地址数组
     */
    public void sendEmail(String subject, String content, List<String> toAddresses) throws GeneralSecurityException, MessagingException {

        Properties properties = System.getProperties();
        properties.setProperty("mail.smtp.host", host);
        properties.setProperty("mail.smtp.auth", "true");
        properties.setProperty("mail.transport.protocol", "smtp");

        //qq邮箱需要SSL加密
        MailSSLSocketFactory sf = new MailSSLSocketFactory();
        sf.setTrustAllHosts(true);
        properties.put("mail.smtp.ssl.enable", "true");
        properties.put("mail.smtp.ssl.socketFactory", sf);

        //获取默认session对象
        Session session = Session.getDefaultInstance(properties);

        //创建邮件对象
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(fromAddress));
        //添加收件人
        Address[] addresses = InternetAddress.parse(String.join(",", toAddresses), false);
        message.addRecipients(Message.RecipientType.TO, addresses);
        message.setSubject(subject);
        message.setSentDate(new Date());
        message.setContent(content, "text/html;charset=UTF-8");
        message.saveChanges();

        //发送邮件
        Transport sender = session.getTransport();
        sender.connect(host, fromAddress, authCode);
        sender.sendMessage(message, message.getAllRecipients());
        sender.close();
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

颜艾青

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值