So I'm getting a timeout issue when I'm trying to send emails using the most recent java mail api. the email address is valid and so is the password. I am using a gmail email as my sender account and have allowed access to less secure accounts. the problem is on line 24 where it says tr.send(message)
. I don't get an error. the program just becomes unresponsive. The message doesn't even go to my folder. I think it might be related to the statement import javax.activation.*;
which Intellij says is an unused import statement. I did add it to my project's library when I added the java mail api. Do I need to add it in to my pom.xml file?
I've tried reading from this answer Sending an email with an attachment using javamail API and watched this video https://www.youtube.com/watch?v=A7HAB5whD6I That's where I got the bulk of my program from.
public class AutomatedEmails {
@FXML
private TextField email;
@FXML
private TextField name;
private String host = "smtp.gmail.com";
private String user = "emailaddress@gmail.com";
private String emailPassword = "password";
private String TO = "";
private String FROM = "emailaddress@gmail.com";
private String subject = "hello";
private String messageText = "hello";
Properties props = System.getProperties();
public AutomatedEmails() throws IOException, MessagingException {
}
public void sendWelcomeEmail(RegisterController registerController) throws MessagingException {
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");
TO = registerController.getEmail();
Session session = Session.getInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(FROM, emailPassword);
}
});
Message message = prepareMessage(session, FROM, TO);
System.out.println("working still");
// this prints
Transport tr = session.getTransport("smtps");
tr.connect(host, FROM, emailPassword);
Transport.send(message);
tr.close();
System.out.println("message sent successfully");
// this doesn't print
}
public Message prepareMessage(Session session, String FROM, String TO){
try{
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(FROM));
message.setRecipient(Message.RecipientType.TO, new InternetAddress(TO));
message.setSubject(subject);
((MimeMessage) message).setText(messageText);
return message;
} catch (Exception e){
Logger.getLogger(AutomatedEmails.class.getName()).log(Level.SEVERE, null, e);
}
return null;
}
}
The idea is that the email will be sent to the registered user. Instead there is just a timeout with no email being sent. I'm wondering if this is related to the unused import. Do I need to add something to my pom.xml file? Or change up my code. Using a Mac