最代碼廣告位
最代碼官方的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 = "[email protected]";
        String nickname = "張三";

        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper help = new MimeMessageHelper(message, true, "UTF-8");
        help.setFrom("[email protected]");
        help.setTo(new String[]{"[email protected]", 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下載
頂部客服微信二維碼底部
>掃描二維碼關注最代碼為好友掃描二維碼關注最代碼為好友
2008大乐透走势图