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();
}
}
}