java - 尝试发送电子邮件时出现超时问题
问题描述
因此,当我尝试使用最新的 java 邮件 api 发送电子邮件时,我遇到了超时问题。电子邮件地址有效,密码也有效。我使用 gmail 电子邮件作为我的发件人帐户,并允许访问不太安全的帐户。问题出在第 24 行tr.send(message)
。我没有收到错误。该程序只是变得没有响应。该消息甚至没有进入我的文件夹。我认为这可能与import javax.activation.*;
Intellij 所说的未使用的导入语句有关。当我添加 java 邮件 api 时,我确实将它添加到我的项目库中。我需要将它添加到我的 pom.xml 文件中吗?
我尝试阅读此答案使用 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
解决方案
推荐阅读
- dialogflow-es - 谷歌行动 | 代理上下文在 agent.context.get() 中给出“-”
- google-apps-script - MailApp.sendEmail 向非收件人的用户发送电子邮件
- azure-devops - 为测试自动化框架设置 Azure Pipeline
- linker - dyld:未加载库:但库似乎完全不相关
- firebase - Firestore 文档流和更新
- sql - 用于生成基于新列的匹配字段值的 SQL 逻辑
- angular - 安装浏览器动画后出现角度错误
- spring - 使用休眠的 Spring 多线程
- java - 在 mvn 测试期间显示完整的错误跟踪
- python - 有没有办法在 Flask 服务器和它创建的线程之间共享数据(不使用全局变量?)