java 发送邮件


一:

java 发送邮件


二:

public class MailUtils {

    public static void main(String[] args) {
        try {
           //发送给qq邮箱
         sendMail("[email protected]","6666");
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }


    /**
     * 发送邮件
     *
     * @param to   给谁发送
     * @param code **码
     */
    public static void sendMail(String to, String code) throws MessagingException {
        //创建链接对象
        Properties properties = new Properties();
        properties.setProperty("mail.transport.protocol", "smtp");   // 使用的协议(JavaMail规范要求)
        properties.setProperty("mail.smtp.host", "smtp.163.com");   // 发件人的邮箱的 SMTP 服务器地址
        properties.setProperty("mail.smtp.auth", "true");
        //properties.setProperty("host","value")因为是本地,不用设置
        Session session = Session.getInstance(properties, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                //输入邮箱的账号和密码就可以登陆了
                return new PasswordAuthentication("[email protected]", "密码");
            }
        });

        //创建邮件对象
        Message message = new MimeMessage(session);
        //设置发件人
        message.setFrom(new InternetAddress("[email protected]"));
        //设置收件人   BC;CC需要了解
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
        //设置主题
        message.setSubject("邮件测试");
        //设置邮件的正文
        message.setContent("<h1>**点击链接</h1><a href='http://localhost:8080/mail?code=" + code + "'>http://localhost:8080/mail?code=" + code + "</a>", "text/html;charset=UTF-8");//第二个参数是设置这是一个文本还是一个链接
        //发送**邮件
        Transport.send(message);
    }

}

三:效果图,我使用的是foxmail

java 发送邮件