最代码广告位
最代码官方的gravatar头像
最代码官方2012-07-27 15:47:27

java实现发邮件的代码

通过java发送邮件的代码

如果是QQ或163的smtp服务,那port是25,而gmail的port是587

String host = "smtp.163.com";
        int port = 25;
        String username = "zuidaima";
        String password = "xxxxxx;

        Properties javaMailProperties = new Properties();
        javaMailProperties.put("mail.smtp.auth", "true");
        javaMailProperties.put("mail.smtp.starttls.enable", "true");
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        mailSender.setHost(host);
        mailSender.setPort(port);
        mailSender.setUsername(username);
        mailSender.setPassword(password);
        mailSender.setJavaMailProperties(javaMailProperties);
        String email = "zuidaima@gmail.com";
        String nickname = "张三";

        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper help = new MimeMessageHelper(message, true, "UTF-8");
        help.setFrom("zuidaima@163.com");
        help.setTo(new String[]{"3314687058@qq.com", email});
        help.setSubject("测试");
        String content = "你好" + nickname;
        help.setText(content, true);
        try {
            mailSender.send(message);
        } catch (Throwable e) {
            e.printStackTrace();
        }

运行截图

java实现发邮件的代码

相关lib包百度网盘下载

https://pan.baidu.com/s/1hr7JWGG

java实现发邮件的代码

项目截图

java实现发邮件的代码

最代码广告位

打赏

文件名:MailSender.zip,文件大小:0K下载
顶部客服微信二维码底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友