1

I am making an Email client which use the below helper class to send emails.It works well when the receiving email is valid.But when the receiving email is invalid the program doesn't end(like stuck in loop) nor doesn't throw an exception.I looked at the java mail FAQ but it doesn't provide any solution.

Please note that the helper class given below was taken from a website and i have no knowledge about SMTP(or TLS) or how it works.Also note i have deleted the sender's email(my email and password). Any help is appreciated.


    public class SendEmailTLS {

    
    public static void email_sender(String recipient, String subject,String content) {

        System.out.println("Sending email(s)...");

        final String username = "sender's email";
        final String password = "sender's password";

        Properties prop = new Properties();
        prop.put("mail.smtp.host", "smtp.gmail.com");
        prop.put("mail.smtp.port", "587");
        prop.put("mail.smtp.auth", "true");
        prop.put("mail.smtp.starttls.enable", "true"); //TLS
        
        Session session = Session.getInstance(prop,
                new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(username, password);
                    }
                });

        try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("sender's email"));
            message.setRecipients(
                    Message.RecipientType.TO,
                    InternetAddress.parse(recipient)
            );
            message.setSubject(subject);
            message.setText(content);

            Transport.send(message);

            System.out.println("Email Sents");

        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

}
scout
  • 11
  • 3
  • What does "email address is invalid" mean? Invalid syntax for an email address? Domain does not exist? Recipient not known at destination domain? Or what? – tquadrat Aug 13 '22 at 12:54
  • 1
    You might want to have a look at https://stackoverflow.com/questions/18970409/why-javamail-connection-timeout-is-too-long – halfbit Aug 13 '22 at 13:13
  • tquadrat , an email that does not exist like abdcd@efgh.com – scout Aug 13 '22 at 13:18

0 Answers0