package cn.com.dao;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * 邮件工具类
 */
public class MailUtil {
    /**
     * 发送邮件
     * @param to 给谁发
     * @param text 发送内容
     */
    public static void send_mail(String to,String text) throws MessagingException {
        //创建连接对象 连接到邮件服务器
        Properties properties = new Properties();
        //设置发送邮件的基本参数
        //发送邮件服务器
        properties.put("mail.smtp.host", "smtp.qq.com");
        //发送端口
        properties.put("mail.smtp.port", "25");
        properties.put("mail.smtp.auth", "true");
        //设置发送邮件的账号和密码
        Session session = Session.getInstance(properties, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                //两个参数分别是发送邮件的账户和邮件客户端授权码
                return new PasswordAuthentication("填发送邮件用的qq邮箱","邮件客户端授权码");
            }
        });

        //创建邮件对象
        Message message = new MimeMessage(session);
        //设置发件人
        message.setFrom(new InternetAddress("填发送邮件用的qq邮箱"));
        //设置收件人
        message.setRecipient(Message.RecipientType.TO,new InternetAddress(to));
        //设置主题
        message.setSubject("五道口科学研究院身份验证");
        //设置邮件正文  第二个参数是邮件发送的类型
        message.setContent(text,"text/html;charset=UTF-8");
        //发送一封邮件
        Transport.send(message);
    }
}
最近下载更多
3263394665  LV9 2023年11月29日
你爹正在加载中  LV4 2023年7月12日
全栈小白  LV34 2023年5月23日
ewan007  LV29 2023年2月20日
263648  LV7 2023年2月14日
stonewan  LV10 2022年11月25日
485415187  LV6 2022年11月24日
bluesky2016  LV15 2022年7月6日
pureshyness  LV6 2022年4月28日
卡迪猫诺  LV2 2022年4月22日
最近浏览更多
dingyb  LV11 4月21日
AlexYHX 4月17日
暂无贡献等级
荣》Cowboy  LV12 4月11日
Lilei66 3月27日
暂无贡献等级
logan123 3月17日
暂无贡献等级
duoduo1234  LV2 1月15日
lilong007  LV20 1月12日
廖业贵  LV18 1月1日
asddwh  LV12 2023年12月26日
求学的熊猫  LV6 2023年12月25日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友