Scheduling a time in the future to send an email in Java or Python

こ雲淡風輕ζ 提交于 2019-12-02 07:16:36

Quartz Scheduler can be user for this kind of asynchronous jobs.

Quartz is a great Java library for functions that you want to run at a certain time, after a certain time interval, etc.

There is also the Timer class in the JDK.

If you are to use Java, try Quartz, an open source job scheduling framework.

You can build the actual email to send, using JavaMail (with attachments and all), save it to disk, and then delegate a "mail foo@bar.com < textfilefromjavamail" to the Linux batch system.

There is an "at" command which will most likely do exactly what you want.

I don't think standard SMTP protocol has such a feature, so if you want to be platform-independent, you will have to search for another solution.

How about writing your message to a queue (local database, for example) with a timestamp and then have some program watching it periodically and send pending emails out?

Is the delay an exact timedelta or is it "1-2 hours later"? If it is the latter, than you can have an hourly job (cronjob starting every hour or a background job sleeping for an hour), which would then send out the emails.

Answer 1:

In Python, use threading.Timer to schedule in the future; use smtplib to send an email. No external library needed.

Answer 2:

Sounds like you want the sending program to quit rather than having it wait in the background. You may use cron for this. Alternative just use the unix command sleep and mail:

$ { sleep 3600; echo "hello world" | mail -s the-subject destination-email; } &

P.S. I don't believe SMTP have anything for you in this case. You are really looking for an MTA that has scheduling feature. Though I'm not familiar with it to make a recommendation.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!