package com.ruoyi.system.utils;

import com.ruoyi.system.domain.ZsOperationSmtp;
import com.ruoyi.system.mapper.ZsOperationSmtpMapper;
import com.sun.mail.util.MailSSLSocketFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
//邮件发送工具类
@Component
@Slf4j
public class EmailUtils {
    @Autowired
    private ZsOperationSmtpMapper zsOperationSmtpMapper;
    /**
     * 发送邮件
     * @param sendEmail 发送邮箱
     * @param receiveEmail 接收邮箱
     * @param content 邮件标题
     * @param receiveEmail 邮件内容
     * @param smtpId 邮件配置id
     */
    @Async
    public void sendMailByTransport(String sendEmail ,String receiveEmail,String title,String content,Long smtpId) {
        try {
            ZsOperationSmtp smtp = zsOperationSmtpMapper.selectById(smtpId);
            Properties props = new Properties();
            props.put("mail.smtp.host", smtp.getHost());
            props.put("mail.smtp.socketFactory.port", String.valueOf(smtp.getPort()));
            MailSSLSocketFactory sf = new MailSSLSocketFactory();
            sf.setTrustAllHosts(true);
            props.put("mail.smtp.ssl.enable", "true");
            props.put("mail.smtp.ssl.socketFactory", sf);
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.port", String.valueOf(smtp.getPort()));
            final String username = smtp.getUsername().trim();
            final String password = smtp.getPassword().trim();
            Session session = Session.getInstance(props,
                new Authenticator() {
                    @Override
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(username,password);
                    }
                });
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(sendEmail));
            message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(receiveEmail));
            message.setSubject(title);
            message.setText(content);
            Transport.send(message);
        } catch (Exception e) {
            log.error("邮件发送失败",e);
        }
    }
   // @Resource
   // private JavaMailSender javaMailSender;
    public void sendMailByTransportHtml(String sendEmail ,String receiveEmail,String title,String content,Long smtpId) {
        try {
            ZsOperationSmtp smtp = zsOperationSmtpMapper.selectById(smtpId);
            Properties props = new Properties();
            props.put("mail.smtp.host", smtp.getHost());
            props.put("mail.smtp.socketFactory.port", String.valueOf(smtp.getPort()));
            MailSSLSocketFactory sf = new MailSSLSocketFactory();
            sf.setTrustAllHosts(true);
            props.put("mail.smtp.ssl.enable", "true");
            props.put("mail.smtp.ssl.socketFactory", sf);
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.port", String.valueOf(smtp.getPort()));
            final String username = smtp.getUsername().trim();
            final String password = smtp.getPassword().trim();
            Session session = Session.getInstance(props,
                new Authenticator() {
                    @Override
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(username,password);
                    }
                });
            MimeMessage message = new MimeMessage(session);
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(message,true,"utf-8");
            mimeMessageHelper.setFrom(new InternetAddress(sendEmail));
            mimeMessageHelper.setTo(InternetAddress.parse(receiveEmail));
            mimeMessageHelper.setSubject(title);
            mimeMessageHelper.setText(content,true);
            Transport.send(message);
        } catch (Exception e) {
            log.error("邮件发送失败",e);
        }
    }
}
