JavaMail 一段时间后停止发送邮件

发布于 2024-09-18 00:18:59 字数 2186 浏览 4 评论 0原文

我目前正在做 Java 程序员的暑期工作。我们有一个应用程序,人们可以在其中输入他们的任务、议程等。该程序是一个客户端-服务器程序,因此所有数据都存储在服务器上。

我的老板让我做一个邮件通知系统。例如,当任务的截止日期临近时,它会向分配给该任务的人员发送电子邮件。

我使用 JavaMail 在服务器(24/24 运行)中实现了这个系统,它运行得很好。但过了一会儿(不确定多久)JavaMail 停止发送邮件。这是我得到的异常:

...
[Mailer] enqueuing mail
[Mailer] enqueuing mail
[Mailer] enqueuing mail
[Mailer] enqueuing mail
[Mailer] enqueuing mail
[Mailer] enqueuing mail
[Mailer] enqueuing mail
...
[Mailer] flushing mail queue (10 mails)
[Mailer] exception
javax.mail.SendFailedException: Invalid Addresses;
  nested exception is:
    com.sun.mail.smtp.SMTPAddressFailedException: 553 sorry, that domain isn't in my list of allowed rcpthosts (#5.7.1)

    at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1446)
    at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:736)
    at javax.mail.Transport.send0(Transport.java:191)
    at javax.mail.Transport.send(Transport.java:120)
    at Server.Mailer.send(Mailer.java:119)
    at Server.Mailer.flush(Mailer.java:84)
    at Server.Mailer.run(Mailer.java:103)
    at java.util.TimerThread.mainLoop(Unknown Source)
    at java.util.TimerThread.run(Unknown Source)
Caused by: com.sun.mail.smtp.SMTPAddressFailedException: 553 sorry, that domain isn't in my list of allowed rcpthosts (#5.7.1)

    at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1297)
    ... 8 more

当运行时间超过 X 小时时,它会不断给出这些异常。所以我认为是因为与SMTP服务器的连接超时。所以我将这段代码更改

Session session = Session.getDefaultInstance(properties, authenticator);

为:

Session session = Session.getInstance(properties, authenticator);

这样它每次都会创建一个新会话。我认为这会迫使JavaMail重新连接到SMTP服务器,然后问题就可以解决。但这并没有解决问题,我仍然遇到这些异常...

有人知道如何解决这个问题吗?

PS:这是我的 send 函数的代码

Session session = Session.getInstance(properties, authenticator);
MimeMessage message = new MimeMessage(session);

message.setSubject(mail.getSubject());
message.setContent(mail.getHTML().toString(), "text/html");
message.setFrom(mail.getSender());
message.setRecipients(javax.mail.Message.RecipientType.TO, mail.getRecipients());

Transport.send(message);

I am currently doing a summer job as Java programmer. We have an application where people can enter their tasks, agenda, etc. The program is a client-server program, so all data is stored on a server.

My boss asked me to make a mail notification system. For example, when a deadline of a task is near, it sends an email to the person assigned to that task.

I implemented this system in the server (which runs 24/24) using JavaMail and it works very well. But after a while (not sure how long) JavaMail stops sending mails. This is the exception I get:

...
[Mailer] enqueuing mail
[Mailer] enqueuing mail
[Mailer] enqueuing mail
[Mailer] enqueuing mail
[Mailer] enqueuing mail
[Mailer] enqueuing mail
[Mailer] enqueuing mail
...
[Mailer] flushing mail queue (10 mails)
[Mailer] exception
javax.mail.SendFailedException: Invalid Addresses;
  nested exception is:
    com.sun.mail.smtp.SMTPAddressFailedException: 553 sorry, that domain isn't in my list of allowed rcpthosts (#5.7.1)

    at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1446)
    at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:736)
    at javax.mail.Transport.send0(Transport.java:191)
    at javax.mail.Transport.send(Transport.java:120)
    at Server.Mailer.send(Mailer.java:119)
    at Server.Mailer.flush(Mailer.java:84)
    at Server.Mailer.run(Mailer.java:103)
    at java.util.TimerThread.mainLoop(Unknown Source)
    at java.util.TimerThread.run(Unknown Source)
Caused by: com.sun.mail.smtp.SMTPAddressFailedException: 553 sorry, that domain isn't in my list of allowed rcpthosts (#5.7.1)

    at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1297)
    ... 8 more

When running longer than X hours, it keeps giving these exceptions. So I thought it was because the connection with the SMTP server timed-out. So I changed this code:

Session session = Session.getDefaultInstance(properties, authenticator);

to:

Session session = Session.getInstance(properties, authenticator);

so it would create a new session every time. I thought this would force JavaMail to reconnect to the SMTP server and then the problem would be solved. But that didn't solve it, I still get these exceptions...

Does anyone know how to fix this?

PS: This is the code of my send function

Session session = Session.getInstance(properties, authenticator);
MimeMessage message = new MimeMessage(session);

message.setSubject(mail.getSubject());
message.setContent(mail.getHTML().toString(), "text/html");
message.setFrom(mail.getSender());
message.setRecipients(javax.mail.Message.RecipientType.TO, mail.getRecipients());

Transport.send(message);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

面犯桃花 2024-09-25 00:18:59

我将从捕获 SMTPAddressFailedException 异常开始,

try {
    Transport.send(message);
} catch (SMTPAddressFailedException e) {
    throw new SendFailedException("Unable to send to " + mail.getRecipients(), e);
}

这可能很简单,就像您的某个用户在其电子邮件配置中出现拼写错误一样简单,如果是这种情况,您可能应该做的是在相关帐户上设置一个标志这将提示用户下次登录您的应用程序时验证其电子邮件设置。

I'd start with catching SMTPAddressFailedException exceptions

try {
    Transport.send(message);
} catch (SMTPAddressFailedException e) {
    throw new SendFailedException("Unable to send to " + mail.getRecipients(), e);
}

It could be as simple as one of your users having a typo in their email configuration, if thats the case what you should probably do is set a flag on the relevent account which will promt the user next time they login to your application to verify their email settings.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文