java中邮件通知

// 客户信息
        Tkhxx tkhxx = new Tkhxx();

try {
            String msg = tkhxx.toString();
            MailUtil.simpleMailSend(MailUtil.mail_emp_username, "客户"+tkhxx.getKhh()+"注册,请审批", msg);
            Tqylxxx cs = this.getContactMail(bean.getProvinces(),bean.getCity(),bean.getArea());
            if(cs!=null && !CommonUtils.isEmpty(cs.getMail())) {
                MailUtil.simpleMailSend(cs.getMail(), "客户"+tkhxx.getKhh()+"注册,请审批", msg);
            }
        }
        catch(Exception e) {
            System.out.println(e.getMessage());
        }



package com.zw.wechat.utils;

import java.util.Properties;

import javax.mail.internet.MimeMessage;

import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;

public class MailUtil {
    // #服务器主机名
    public static String mail_host = "";
    // #发送邮箱地址
    public static String mail_username = "";
    // #你的授权码
    public static String mail_password = "";
    // #接受邮箱地址
    public static String mail_emp_username = "";
    // #接受邮箱地址
    public static String mail_mgr_username = "";

    public static String getMail_host() {
        return mail_host;
    }

    public static void setMail_host(String mail_host) {
        MailUtil.mail_host = mail_host;
    }

    public static String getMail_username() {
        return mail_username;
    }

    public static void setMail_username(String mail_username) {
        MailUtil.mail_username = mail_username;
    }

    public static String getMail_password() {
        return mail_password;
    }

    public static void setMail_password(String mail_password) {
        MailUtil.mail_password = mail_password;
    }

    public static String getMail_emp_username() {
        return mail_emp_username;
    }

    public static void setMail_emp_username(String mail_emp_username) {
        MailUtil.mail_emp_username = mail_emp_username;
    }

    public static String getMail_mgr_username() {
        return mail_mgr_username;
    }

    public static void setMail_mgr_username(String mail_mgr_username) {
        MailUtil.mail_mgr_username = mail_mgr_username;
    }
    
    public static JavaMailSender initJavaMailSender() {  
        Properties properties = new Properties();    
        //properties.setProperty("mail.debug", "true");// 是否显示调试信息(可选)    
        properties.setProperty("mail.smtp.auth", "true");    
        properties.put("mail.smtp.timeout", "25000");  
        JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();    
        javaMailSender.setJavaMailProperties(properties);    
        javaMailSender.setHost(MailUtil.mail_host);  
        javaMailSender.setUsername(MailUtil.mail_username); // username  
        javaMailSender.setPassword(MailUtil.mail_password); // password  
    
        javaMailSender.setDefaultEncoding("UTF-8");    
        return javaMailSender;    
    } 

    public static void sendText(JavaMailSender sender, String array, String subject, String msg) {
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setTo(array);
        mailMessage.setSubject("----" + subject + "----");
        mailMessage.setText(msg);
        sender.send(mailMessage);
    }
    
    public static void sendText(JavaMailSender sender, String array[], String subject, String msg) {
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setTo(array);
        mailMessage.setSubject("----" + subject + "----");
        mailMessage.setText(msg);
        sender.send(mailMessage);
    }

    public static void sendHtml(JavaMailSender sender, String array[], String subject, String msg) throws Exception {
        MimeMessage mailMessage = sender.createMimeMessage();
        MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage, true, "GBK");
        messageHelper.setTo(array);
        messageHelper.setSubject("----" + subject + "----");
        messageHelper.setText(msg, true);
        sender.send(mailMessage);
    }

    @Async
    public static void simpleMailSend(String email, String subject, String msg) {
        try {
            JavaMailSender sender = initJavaMailSender();
            if (email.contains(",")) {
                String[] addr = email.split(",");
                sendText(sender,addr,subject,msg);
            } else if (email.contains(";")) {
                String[] addr = email.split(";");
                sendText(sender,addr,subject,msg);
            } else {
                sendText(sender,email,subject,msg);
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

}

 

posted @ 2020-06-10 14:14  红尘沙漏  阅读(666)  评论(0编辑  收藏  举报