java发送腾讯企业邮箱



    /**
     * 设置邮件
     *
     * @param recipient 接收者
     * @param subject   邮件标题
     * @param content   邮件内容
     */
    public static Boolean formatEmail(String[] recipient, String subject, String content) {
        Properties prop = new Properties();
        //协议
        prop.setProperty("mail.transport.protocol", "smtp");
        //服务器
        prop.setProperty("mail.smtp.host", "smtp.exmail.qq.com");
        //端口
        prop.setProperty("mail.smtp.port", "465");
        //使用smtp身份验证
        prop.setProperty("mail.smtp.auth", "true");
        //企业邮箱必须要SSL
        MailSSLSocketFactory sf = null;
        try {
            sf = new MailSSLSocketFactory();
            sf.setTrustAllHosts(true);
        } catch (GeneralSecurityException e1) {
            e1.printStackTrace();
        }
        prop.put("mail.smtp.ssl.enable", "true");
        prop.put("mail.smtp.ssl.socketFactory", sf);
        //获取Session对象
        Session s = Session.getDefaultInstance(prop, new Authenticator() {
            //此访求返回用户和密码的对象
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                PasswordAuthentication pa = new PasswordAuthentication("发送邮箱账号", "发送邮箱密钥");
                return pa;
            }
        });
        //设置session的调试模式,发布时取消
        s.setDebug(true);
        MimeMessage mimeMessage = new MimeMessage(s);
        try {
            mimeMessage.setFrom(new InternetAddress("发送邮箱账号"));
            //发送多个人
         
            Address[] address = new Address[recipient.length];
         
            for (int i = 0; i < recipient.length; i++) {
                address[i] = new InternetAddress(recipient[i]);
            }


            mimeMessage.addRecipients(Message.RecipientType.TO, address);//接受者
            //设置主题
            mimeMessage.setSubject(subject);//邮件标题
            mimeMessage.setSentDate(new Date());
            //设置内容
            mimeMessage.setText(content);//邮件内容
            mimeMessage.saveChanges();
            //发送
            Transport.send(mimeMessage);
            return true;
        } catch (MessagingException e) {
            e.printStackTrace();
            return false;
        }
    }

以上操作需要javax.mail 1.6以上   javax.mail-1.6.2.jar  

​​​​​​​

  • 11
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值