springboot利用线程池实现邮件发送

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_40693828/article/details/89156659

最近实现一个邮件推送功能,整理如下:

1  编写线程类实现runnable接口:(其中JavaMailUtil为封装的发送邮件工具类)

import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author an-ping
 * @date 2019/3/31
 */
@Data
public class MailThread implements   Runnable {
    String to;
    String subject;
    String name;
    String company;
    String photo;
    int jid;
    public MailThread( String to, String subject, String name, String company, String photo, int jid) {
        this.to = to;
        this.subject = subject;
        this.name = name;
        this.company = company;
        this.photo = photo;
        this.jid = jid;
    }
    JavaMailUtil javaMailUtil=(JavaMailUtil) ApplicationContextProvider.getBean("javaMailUtil");
    @Override
    public void run() {
            javaMailUtil.sendTemplateMail(to,subject,name,company,photo,jid);
    }
}

2 JavaMailUtil为发送邮件工具类:

import org.apache.catalina.webresources.FileResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.thymeleaf.IEngineConfiguration;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.templatemode.TemplateMode;
import org.thymeleaf.templateresolver.ITemplateResolver;
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;
import org.thymeleaf.templateresolver.TemplateResolution;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Map;

/**
 * @author an-ping
 * @date 2018/12/4
 */
@Component

public class JavaMailUtil  {
    @Autowired
     JavaMailSender MailSender;

    public JavaMailSender getMailSender() {
        return MailSender;
    }

    public void setMailSender(JavaMailSender mailSender) {
        MailSender = mailSender;
    }

    public TemplateEngine getTemplateEngine() {
        return templateEngine;
    }

    public void setTemplateEngine(TemplateEngine templateEngine) {
        this.templateEngine = templateEngine;
    }

    @Value("${mail.fromMail.addr}")
    private String from="tap3249899068@163.com";

    //发送简单消息
    public   int sendSimpleMail(String to,String text,String subject){
        SimpleMailMessage simpleMailMessage=new SimpleMailMessage();
        simpleMailMessage.setFrom(from);
        simpleMailMessage.setTo(to);
        simpleMailMessage.setSubject(subject);
        simpleMailMessage.setText(text);

        try {
            System.out.println(simpleMailMessage);
            System.out.println(MailSender);
            MailSender.send(simpleMailMessage);
            return 1;//发送成功
        } catch (MailException e) {
            e.printStackTrace();
            return 0;//发送失败
        }

    }

//发送html格式文本
    public int sendHtmlMail(String to,String subject,String text){
        MimeMessage mimeMessage=MailSender.createMimeMessage();
        try {
            MimeMessageHelper mimeMessageHelper=new MimeMessageHelper(mimeMessage,true);
            mimeMessageHelper.setFrom(from);
            mimeMessageHelper.setTo(to);
            mimeMessageHelper.setSubject(subject);
            mimeMessageHelper.setText(text,true);//html格式为true
            MailSender.send(mimeMessage);
            return 1;
        } catch (MessagingException e) {
            e.printStackTrace();
            return 0;
        }


    }


    public int sendAttachMail(String to,String text,String subject,String path){
        MimeMessage mimeMessage=MailSender.createMimeMessage();
        try {
            MimeMessageHelper mimeMessageHelper=new MimeMessageHelper(mimeMessage,true,"utf-8");
            mimeMessageHelper.setFrom(from);
            mimeMessageHelper.setText(text,true);
            mimeMessageHelper.setSubject(subject);
            mimeMessageHelper.setTo(to);
            FileSystemResource fileSystemResource=new FileSystemResource(new File(path));
            String fileName=path.substring(path.lastIndexOf(File.separator));
            mimeMessageHelper.addAttachment(fileName,fileSystemResource);
            MailSender.send(mimeMessage);
            return 1;

        } catch (MessagingException e) {
            e.printStackTrace();
            return 0;
        }

    }



    public int sendPicMail(String to,String text,String subject,String path,String id){
        MimeMessage mimeMessage=MailSender.createMimeMessage();
        try {
            MimeMessageHelper mimeMessageHelper=new MimeMessageHelper(mimeMessage,true);
            mimeMessageHelper.setTo(to);
            mimeMessageHelper.setFrom(from);
            mimeMessageHelper.setText(text,true);
            mimeMessageHelper.setSubject(subject);
            FileSystemResource fileSystemResource=new FileSystemResource(new File(path));
            mimeMessageHelper.addInline(id,fileSystemResource);
            MailSender.send(mimeMessage);
            return 1;


        } catch (MessagingException e) {
            e.printStackTrace();
            return 0;
        }

    }
@Autowired
TemplateEngine templateEngine;

    public int sendTemplateMail(String to,String subject,String name,String company,String photo,int jid){
        //创建邮件正文
        Context context = new Context();
        context.setVariable("name", name);
        context.setVariable("company", company);
        context.setVariable("url", "http://www.tap2019.top:2019/front/jobpage.action?"+jid);
        context.setVariable("photo","http://www.tap2019.top:2019/"+photo);
        System.out.println(templateEngine+"模板引擎");
        String emailContent = templateEngine.process("j", context);
        int result=sendHtmlMail(to,subject,emailContent);
        return result;
    }

}

3 控制层:

@RequestMapping(value="/tuiJobs.action",method = RequestMethod.POST)
   @ResponseBody
    public JsonMode tuiSend(String company,String photo,int jid){
       JsonMode  jsonMode=new JsonMode();
        TempPage<People> list= peopleService.findEmailAndName(new People());
        List peoplelist=list.getRows();
         People people=null;
ExecutorService  executorService=new ThreadPoolExecutor(2,4,30, TimeUnit.SECONDS,new LinkedBlockingDeque<>(),new ThreadPoolExecutor.CallerRunsPolicy());
       try {
           for(int i=0;i<peoplelist.size();i++){
              people=(People)peoplelist.get(i);

               executorService.execute(new MailThread(people.getEmail(),"jobguru-职位推荐",people.getName(),company,photo,jid));
              jsonMode.setCode(Common.Request_success);

           }
       } catch (Exception e) {
           jsonMode.setCode(Common.Request_fail);
           e.printStackTrace();
       }


       return jsonMode;
    }
}

 

 

 

展开阅读全文

没有更多推荐了,返回首页