码迷,mamicode.com
首页 > 编程语言 > 详细

java实现发送邮件

时间:2020-06-18 22:10:17      阅读:81      评论:0      收藏:0      [点我收藏+]

标签:inf   ext   email   oid   char   smt   arrays   reply   password   

/**
     * 发送邮件 可以带有HTML格式
     * @param toUser
     * @param recipients
     * @param content
     * @param subject
     * @throws Exception
     */
    public static void noticePushCommandForHtml(String toUser, String[] recipients, String content, String subject) throws Exception {
        /*if(!emailEnable()){
            return;
        }*/

        JavaMailSenderImpl mailSender = getJavaMailSenderImpl();
        final Properties p = System.getProperties() ;
        p.setProperty("mail.smtp.host", "smtp.qq.com");
        p.setProperty("mail.smtp.auth", "true");
        p.setProperty("mail.smtp.user", "1131172761@qq.com");
        p.setProperty("mail.smtp.pass", "efghgubdzvhnffdh");
        Session session = Session.getInstance(p, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("1131172761@qq.com", "efghgubdzvhnffdh");
            }
        });

        String titleEncode = "gb2312";
        Message message = new MimeMessage(session);
        Address address = new InternetAddress("1131172761@qq.com", MimeUtility.encodeText("1131172761@qq.com", MimeUtility.mimeCharset(titleEncode), null));
        // message.setFrom(new InternetAddress(p.getProperty("mail.smtp.user"),"邮件助手"));
        message.setFrom(address);
        message.setReplyTo(InternetAddress.parse(fromUser));//发件人
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(toUser));//收件人
        message.setSubject(subject);
        List<String> ccList = new ArrayList<>();
        if (recipients != null && recipients.length >0){
            ccList = new ArrayList(Arrays.asList(recipients));  //工单创建人
        }
        // 设置测试阶段抄送人员
        //ccList.add("zhouweixin@cebbank.com");
        //ccList.add("zh_wy@cebbank.com");
        //ccList.add(createUserEmail);  //工单创建人
        //ccList.add("xiangqiuxu@cebbank.com");
        log.info("发送邮件抄送人个数为:"+ccList.size());
        String[] temp = new String[ccList.size()];
        String[] ccArray = ccList.toArray(temp);
        InternetAddress[] addresssCc = new InternetAddress[ccArray.length];
        for (int i = 0; i < ccArray.length; i++) {
            String cc = ccArray[i];
            InternetAddress a = new InternetAddress(cc);
            addresssCc[i] = a;
        }
        message.setRecipients(Message.RecipientType.CC, addresssCc);
        Multipart multipart = new MimeMultipart();
        BodyPart html = new MimeBodyPart();
        html.setContent(content, "text/html; charset=utf-8");
        multipart.addBodyPart(html);
        message.setContent(multipart);
        Transport.send(message);
    }

 

 

public static void noticePushCommand(String toUser, String[] recipients, String content, String subject) {
        if(!emailEnable()){
            return;
        }
        log.debug("toUser:" + toUser + ",subject:" + subject + ",content:" + content);
        log.info("发送邮件toUser:" + toUser + ",subject:" + subject + ",content:" + content);
        JavaMailSenderImpl mailSender = getJavaMailSenderImpl();
        SimpleMailMessage message = new SimpleMailMessage();//消息构造器
        message.setFrom(fromUser);//发件人
        message.setTo(toUser);//收件人
        message.setText(content);
        message.setSubject(subject);
        List<String> ccList = new ArrayList<>();
        if (recipients != null && recipients.length >0){
            ccList = new ArrayList(Arrays.asList(recipients));  //工单创建人
        }
        // 设置测试阶段抄送人员
        //ccList.add("zhouweixin@cebbank.com");
        //ccList.add("zh_wy@cebbank.com");
        //ccList.add(createUserEmail);  //工单创建人
        ccList.add("xiangqiuxu@cebbank.com");
        log.info("发送邮件抄送人个数为:"+ccList.size());
        recipients = ccList.toArray(new String[0]);
        if (recipients != null && recipients.length > 0) {
            message.setCc(recipients);  //抄送人
        }
        mailSender.send(message);
    }

 

java实现发送邮件

标签:inf   ext   email   oid   char   smt   arrays   reply   password   

原文地址:https://www.cnblogs.com/qcq0703/p/13159483.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!