java发送电子邮件

邮件发送工具类,支持qq,163,126邮箱

 /**
         * @param to 收件账号
         * @param nickName 昵称
         * @param title 标题
         * @param content 内容
         * @param type 邮件类型
         * @param from 发件账号
         * @param authorization 发件账号授权码
         * @param smtpHost 发件smtp服务器
         * @return
         */
        public static boolean send(String to,
                                   String nickName,
                                   String title,
                                   String content,
                                   String type,
                                   String from,
                                   String authorization,
                                   String smtpHost
        ){
            // 获取系统属性
            Properties properties = System.getProperties();
            // 设置邮件服务器
            properties.setProperty("mail.transport.protocol", "smtp");
            properties.setProperty("mail.smtp.host", smtpHost);
            properties.setProperty("mail.smtp.auth", "true");
            try {
                //设置SSL加密
                MailSSLSocketFactory sf = new MailSSLSocketFactory();
                sf.setTrustAllHosts(true);
                properties.put("mail.smtp.ssl.enable", "true");
                properties.put("mail.smtp.ssl.socketFactory", sf);
            } catch (GeneralSecurityException e) {
                e.printStackTrace();
            }

            // 获取默认session对象
            Session session = Session.getDefaultInstance(properties, new Authenticator() {
                @Override
                public PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(from, authorization);
                }
            });
            session.setDebug(false);
            try {
                // 创建默认的 MimeMessage 对象
                MimeMessage message = new MimeMessage(session);
                message.setSentDate(new Date());

                if (StringUtils.isNotBlank(nickName)) {
                    //设置昵称
                    String nick = MimeUtility.encodeText(nickName, MimeUtility.mimeCharset("gb2312"), null);
                    message.setFrom(new InternetAddress(from, nick));
                } else {
                    message.setFrom(new InternetAddress(from));
                }

                //收件人
                if (StringUtils.isNotBlank(to)){
                    message.setRecipients(Message.RecipientType.TO, getAddress(to));
                }
                //抄送 message.setRecipients(Message.RecipientType.CC, );
                //密送 message.setRecipients(Message.RecipientType.BCC, );

                //标题
                message.setSubject(title);
                //发送邮件格式
                if (EmailType.HTML.type.equals(type)) {
                    // 设置消息体
                    Multipart mainPart = new MimeMultipart();
                    // 创建一个包含HTML内容的MimeBodyPart
                    BodyPart html = new MimeBodyPart();
                    // 设置HTML内容
                    html.setContent(content, "text/html;charset=utf-8");
                    mainPart.addBodyPart(html);
                    // 将MiniMultipart对象设置为邮件内容
                    message.setContent(mainPart);
                } else {
                    message.setText(content);
                }
                //保存更改
                message.saveChanges();
                // 发送消息
                Transport.send(message, message.getAllRecipients());
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
            return true;
        }
    }
    private static Address[] getAddress(String str) throws AddressException {
        String[] array =str.split(";");
        InternetAddress[] addresses = new InternetAddress[array.length];
        for (int i = 0; i < array.length; i++) {
            addresses[i] = new InternetAddress(array[i]);
        }
        return addresses;
    }

 

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值