我的邮件代码不起作用,使用Java我没有任何错误

时间:2019-05-09 23:18:27

标签: java email smtp gmail send

我已经导入了所有需要的api(smtp,邮件,激活) 没有错误,使用Java

并没有给我代码正确的提示

属性props =新的Properties();

    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.port", "587"); 
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");

    Session session = Session.getDefaultInstance(props,new Authenticator() 
    {
        @Override
        protected PasswordAuthentication getPasswordAuthentication() 
        {
            return new PasswordAuthentication("javafx.****@gmail.com", "******");
        }
    });

    try 
    {
        Message message = new MimeMessage(session);

        message.setFrom(new InternetAddress("javafx.tuto@gmail.com"));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("heno.qadd97@gmail.com"));
        message.setSubject("message");
        message.setText("Hello world");

        Transport.send(message);

        System.out.println("Email sended!");

    } catch (MessagingException e) 
    {
        System.out.print("Something happened!");

        throw new RuntimeException(e);
    }



}

0 个答案:

没有答案