开发者社区> 问答> 正文

javamail通过阿里云邮发送邮件连接失败

@RequestMapping(value = "/testSendMail.jspx", method = RequestMethod.GET)
    public void testSendMail(HttpServletRequest request, HttpServletResponse respone) {
        Website web = SiteUtils.getWeb(request);
        //final EmailSender email = web.getEmailSender();
        final MessageTemplate tpl = (MessageTemplate) web.getMessages().get(MessageTemplate.RESET_PASSWORD);
        JavaMailSenderImpl sender = new JavaMailSenderImpl();
        sender.setHost("smtp.aliyun.com");
        sender.setUsername(" tang.xin@gzstone.com.cn");
        sender.setPassword("********");//发帖时故意隐藏
        sender.send(new MimeMessagePreparator() {
            public void prepare(MimeMessage mimemessage) {
                try {
                     MimeMessageHelper msg = new MimeMessageHelper(mimemessage, false, "UTF-8");
                     msg.setSubject("这是一份测试邮件");
                     msg.setTo(" 1292785717@qq.com");
                     msg.setFrom("Sender", "Sender");
                     String text = tpl.getActiveTxt();
                     text = StringUtils.replace(text, "${userName}","testUser01");
                     text = StringUtils.replace(text, "${usernameId}", "1");
                     text = StringUtils.replace(text, "${activationCode}", "1236585");
                     text = StringUtils.replace(text, "${base}", "Base");
                     msg.setText(text,true);
                } catch(Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

展开
收起
tang2016 2016-04-18 15:03:16 4778 0
0 条回答
写回答
取消 提交回答
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载