gpt4 book ai didi

java - 尝试发送电子邮件时 JVM 卡住

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:09:44 26 4
gpt4 key购买 nike

我正在尝试使用以下方法从我的程序发送电子邮件:

    void sendEmails(Tutor t, Date date, Time time, String tuteeName, String tuteeEmail){
System.out.println("sending emails");
Properties props = new Properties();
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", "465");

SimpleDateFormat timeFmt = new SimpleDateFormat("hh:mm a");
SimpleDateFormat dateFmt = new SimpleDateFormat("EEE, MMMM dd");
String datePrint = dateFmt.format(date);
String timePrint = timeFmt.format(time);
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});

try {

Message tutorMessage = new MimeMessage(session);

tutorMessage.setFrom(new InternetAddress("laneycodingclub@gmail.com"));

tutorMessage.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(t.getEmail()));

tutorMessage.setSubject("Tutoring Appointment Scheduled");
tutorMessage.setText("Hey " + t.getName() +
"\n \t You have a new appointment scheduled on " + datePrint + " at " + timePrint +
"with " + tuteeName + ". \n If you cannot make this appointment, please contact club leadership immediately. "
+ "\n Thanks for helping out!");

Transport.send(tutorMessage);

System.out.println("Done sending");

} catch (MessagingException e) {
System.out.println("messagingerror");
e.printStackTrace();
}
}

但是当程序执行到这个方法时,GUI 锁定并且程序卡住。这是我第一次尝试在 Java 程序中使用电子邮件,所以我不确定问题出在哪里。

最佳答案

您最好避免同步发送电子邮件。

通常它会减慢您的应用程序的工作速度,更糟的是 - 它可能会卡住它,例如,如果邮件服务器无法访问或没有响应。

使用一些异步机制。

我确定在这种情况下您的邮件服务器有问题。

使用一些独立的 Java 程序来确保您可以使用这些服务器参数发送电子邮件。

关于java - 尝试发送电子邮件时 JVM 卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15445370/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com