Blame view

CollectInfoServiceImpl.java 3.36 KB
涂亚平 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
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);
        }
    }

}