java实现发送邮件

Posted 背着泰山找黄河

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java实现发送邮件相关的知识,希望对你有一定的参考价值。

/**
     * 发送邮件 可以带有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实现发送邮件的主要内容,如果未能解决你的问题,请参考以下文章

用python实现自动发邮件的功能

Java代码实现发送邮件

java实现邮件发送验证码

纯java实现邮件发送服务(亲测好用)

Java实现发邮件功能---网易邮箱

java代码spring-mvc模式怎样实现从后台直接定时发送邮件