尝试发送电子邮件时出现超时问题

时间:2019-03-29 19:10:48

标签: java api maven email

因此,当我尝试使用最新的Java邮件api发送电子邮件时,出现了超时问题。电子邮件地址有效,密码也有效。我将gmail电子邮件用作发件人帐户,并允许访问安全性较低的帐户。问题出在第24行,显示为tr.send(message)。我没有错。该程序只是变得无响应。该消息甚至都没有进入我的文件夹。我认为这可能与Intellij表示未使用的导入语句的语句import javax.activation.*;有关。添加Java邮件api时,确实将其添加到了项目的库中。是否需要将其添加到pom.xml文件中?

我已尝试从此答案Sending an email with an attachment using javamail API中阅读 并观看了此视频https://www.youtube.com/watch?v=A7HAB5whD6I 那是我从中获得大部分程序的地方。

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

想法是将电子邮件发送给注册用户。相反,只有超时,没有电子邮件发送。我想知道这是否与未使用的导入有关。我是否需要在pom.xml文件中添加一些内容?或更改我的代码。使用Mac

0 个答案:

没有答案