CollectInfoServiceImpl.java 3.36 KB
package com.subsidy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.subsidy.common.exception.HttpException;
import com.subsidy.dto.collect.InfoDTO;
import com.subsidy.model.CollectInfoDO;
import com.subsidy.mapper.CollectInfoMapper;
import com.subsidy.service.CollectInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.subsidy.util.ConstantUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * <p>
 * 首页手机信息 服务实现类
 * </p>
 *
 * @author Tuyp
 * @since 2024-11-02
 */
@Service
public class CollectInfoServiceImpl extends ServiceImpl<CollectInfoMapper, CollectInfoDO> implements CollectInfoService {

    @Transactional(rollbackFor = Exception.class)
    public String collect(CollectInfoDO collectInfoDO) {

        int count = this.baseMapper.selectCount(new QueryWrapper<CollectInfoDO>()
        .lambda()
        .eq(CollectInfoDO::getTelephone,collectInfoDO.getTelephone()));

        if (count > 0){
            throw new HttpException(30003);
        }

        this.baseMapper.insert(collectInfoDO);

        //发邮件
        sendMail(collectInfoDO);

        return ConstantUtils.ADD_SUCCESS;
    }

    public IPage<CollectInfoDO> info(InfoDTO infoDTO){
        Page pager = new Page(infoDTO.getPageNum(), infoDTO.getPageSize());
        return this.baseMapper.info(pager);
    }


    public void sendMail(CollectInfoDO collectInfoDO) {

        final String username = "alextyp@163.com"; // 替换为你的邮箱用户名
        final String password = "CYWjrN8NeYYsCy4q"; // 替换为你的邮箱密码

        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", "smtp.163.com");
        props.put("mail.smtp.port", "25");

        Session session = Session.getInstance(props, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
        });

        try {
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("alextyp@163.com")); // 设置发件人
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("tuyaping@ykhl.com")); // 设置收件人
            message.setSubject("有新的联系需求"); // 设置邮件主题
            message.setText("院校名称:"+collectInfoDO.getCollegeName()+"\n" +
                    "所在部门或院系:"+collectInfoDO.getDept()+"\n" +
                    "职位:"+collectInfoDO.getJobName()+"\n" +
                    "称呼:"+collectInfoDO.getUserName()+"\n" +
                    "联系方式:"+collectInfoDO.getTelephone()); // 设置邮件正文

            Transport.send(message);

            System.out.println("Email sent successfully!");
        } catch (MessagingException e) {
            throw new RuntimeException("Error: cannot send email", e);
        }
    }

}