package com.storlead.sales.mail;

/**
 * @program: sp-sales-platform
 * @description:
 * @author: chenkq
 * @create: 2024-05-24 17:08
 */
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

public class QQEmailSender {


    public static void main(String[] args) {
        String host = "smtp.exmail.qq.com"; // QQ邮箱SMTP服务器地址
        String port = "465"; // QQ邮箱SMTP服务器端口
        String userName = "chenkaiqiang@lcjs43.wecom.work"; // 你的QQ邮箱地址
//        String password = "-Chenkq1992";  // 你的QQ邮箱授权码
        String password = "-Chenkq1992";  // 你的QQ邮箱授权码

        Properties properties = new Properties();
        properties.put("mail.smtp.host", host);
        properties.put("mail.smtp.port", port);
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.user", userName);
        properties.put("mail.smtp.starttls.enable", "true");
        properties.put("mail.smtp.ssl.enable", "true");

        Authenticator auth = new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(userName, password);
            }
        };

        Session session = Session.getInstance(properties, auth);

        try {
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(userName));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("1217276730@qq.com"));
            message.setSubject("Test Subject");
            message.setText("Hello, this is a test email!");
            Transport.send(message);
            System.out.println("Email sent successfully!");
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
}

