0

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

  • 1
    Are you able to establish a connection to smtp.gmail.com:587 from the machine you're running this on? Are you running on Windows/Linux/Mac/something else? – Not a JD Mar 29 '19 at 19:12
  • @NotaJD I am using a Mac with the latest software. I assumed I was connected because I'm not getting any errors regarding that. I just checked using `if (session.getTransport("smtp") != null){ System.out.println("it works"); }` and it printed "it works" successfully. –  Mar 29 '19 at 19:20
  • 1
    From a terminal can you "telnet smtp.gmail.com 587" and see if you get a 220 reply code? Should be something like "220 smtp.gmail.com ESMTP g12sm1099622otr.18 - gsmtp" – Not a JD Mar 29 '19 at 19:22
  • @NotaJD I tried `openssl s_client -starttls smtp -connect smtp.gmail.com:587` and it spit out a big thing about being connected. it ended with `verify return code: 0 (ok) 250 SMTPUTF8` –  Mar 29 '19 at 19:36
  • @NotaJD I tried to use telnet but I got errors –  Mar 29 '19 at 19:37
  • 1
    no worries, six of one, we've established you can connect. At this point knowing that you can connect, it might be easier to attach a debugger and step through the code and see what the hold up is. I just ran your code using my credentials and the email sent OK. – Not a JD Mar 29 '19 at 19:49
  • @NotaJD if it were a credential issue I would get an error right? –  Mar 29 '19 at 20:28
  • 1
    Yeah when I put in duff credentials it throws up "javax.mail.AuthenticationFailedException: 535-5.7.8 Username and Password not accepted. Learn more at 535 5.7.8 https://support.google.com/mail/?p=BadCredentials" – Not a JD Mar 29 '19 at 20:30
  • @NotaJD okay so all my credentials are right. It's definitely something with the `.send()` . How do I attach a debugger? –  Mar 29 '19 at 20:36
  • @NotaJD I fixed it. I realized I was using JavaMail API 1.6.2 but my maven dependency was for version 1.5.2. thanks for helping!! If you want you can do an official reply or whatever about checking dependencies and I can mark it correct for you. Idk how the points on this site work. –  Mar 29 '19 at 20:44
  • 1
    You're good dude, glad you got it fixed! :) – Not a JD Mar 29 '19 at 20:48

0 Answers0