Java 使用163邮箱发送邮件

 /**
     * 促销邮件发送后同时给网站管理员发一份游客信息
     *
     * @param bo
     * @return
     */
    @PostMapping("/sendEmailForManager")
    public Boolean sendEmailForMessage(@RequestBody SendSimpleMessageBo bo) {
        boolean success;
        // 1.创建一个程序与邮件服务器会话对象 Session
        Properties props = new Properties();
        // 连接协议
        props.setProperty("mail.transport.protocol", "SMTP");
        props.setProperty("mail.smtp.host", "smtp.163.com");
        // 指定验证为true
        props.setProperty("mail.smtp.auth", "true");
        props.setProperty("mail.smtp.timeout", "10000");
        props.setProperty("mail.smtp.port", "25");

        //ssl配置,本地用不了
        //props.setProperty("mail.smtp.port", "465");
        //MailSSLSocketFactory sf = null;
        //try {
        //    sf = new MailSSLSocketFactory();
        //    sf.setTrustAllHosts(true);
        //} catch (GeneralSecurityException e) {
        //    e.printStackTrace();
        //}
        //props.put("mail.smtp.ssl.enable", true);
        //props.put("mail.smtp.ssl.socketFactory", sf);


        // 验证账号及密码,密码需要是第三方授权码
        Authenticator auth = new Authenticator() {
            @Override
            public PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("****@163.com", "授权吗");
            }
        };
        Session session = Session.getInstance(props, auth);

        // 2.创建一个Message,它相当于是邮件内容
        MimeMessage message = new MimeMessage(session);
        try {
            //防止成为垃圾邮件,披上outlook的马甲(发送到谷歌邮箱容易被标记成垃圾邮件)
            message.addHeader("X-Mailer", "Microsoft Outlook Express 6.00.2900.2869");
            // 设置发送者
            message.setFrom(new InternetAddress("****@163.com"));
            BackendConfigBo configBo = new BackendConfigBo();
            configBo.setConfigName("ManagerEmail");
            List<BackendConfigVo> list = configService.queryList(configBo);
            String toEmail = list.get(0).getRemark();
            // 设置发送方式与接收者
            message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(toEmail));
            // 设置主题
            message.setSubject(bo.getSubject());
            //一个Multipart对象包含一个或多个bodypart对象,组成邮件正文
            MimeMultipart multipart = new MimeMultipart();

            BodyPart text = new MimeBodyPart();
            text.setContent(bo.getText(), "text/html; charset=utf-8");
            multipart.addBodyPart(text);

            //读取本地图片,将图片数据添加到"节点"
            if (!CollUtil.isEmpty(bo.getPicList())) {
                for (String f : bo.getPicList()) {
                    MimeBodyPart imagePart = new MimeBodyPart();
                    String replace = f.replace(NtqConfig.getDomainUrl(), "");
                    DataSource fds = new FileDataSource(NtqConfig.getProfile() + replace);
                    imagePart.setDataHandler(new DataHandler(fds));
                    imagePart.setContentID(f);
                    multipart.addBodyPart(imagePart);
                }
            }
            multipart.setSubType("related");
            message.setContent(multipart);
            // 3.创建 Transport用于将邮件发送
            Transport.send(message);
            success = true;
            System.out.println("给 " + bo.getTo() + " 发送邮件成功");
        } catch (Exception e) {
            System.err.println("给 " + bo.getTo() + " 发送邮件失败");
            //System.out.println(e.getMessage());
            e.printStackTrace();
            success = false;
        }
        return success;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值