Java没有停止发送电子邮件(JAvaMail Eclipse)

时间:2018-07-17 09:10:04

标签: java eclipse javamail

我正在开发一个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);    
}

2 个答案:

答案 0 :(得分:2)

return sendMail(userActivation);

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

答案 1 :(得分:0)

//return userActivation object instead of returning sendMail(userActivation )

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 userActivation;    
}