首页 > 解决方案 > Java 不停止发送电子邮件 (JAvaMail Eclipse)

问题描述

我正在开发一个 Eclipse 项目。我正在制作注册激活链接,但 Java 邮件不断发送消息

这是我发送电子邮件的方法:

public static Mailer  sendMail(Mailer userActivation) {

        final String username = "harfrank2@gmail.com";
        final String password = "";password for the email

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

        Session session = Session.getInstance(props,
          new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
          });

         String userEmail = userActivation.getActvEmail();
         String ActMessage = userActivation.getActLink();

        try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("harfrank2@gmail.com"));
            message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse(userEmail));
            message.setSubject("E-Dealing Activation Link");
            message.setText("Dear "+userEmail
                + "\n\n You have a registered" 
                + ", Please Click the link bellow to activate your acoount"
                + "\n Product "+ActMessage
                    );

            Transport.send(message);

            System.out.println("Done");

        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
return sendMail(userActivation);    
}

标签: javaeclipsejakarta-mail

解决方案


return sendMail(userActivation);

将无限调用该方法。删除递归调用并返回正确的邮件。


推荐阅读