从crontab发送电子邮件(Gmail)时出错,但是从Java类开始工作

时间:2014-03-10 06:31:52

标签: java python gmail crontab

如果我从Python脚本启动java类,我得到以下代码

脚本

/usr/bin/python /home/tripwire/email/notify.py

Java类

public static void main(String[] args) {
        String from = USER_NAME;
        String pass = PASSWORD;
        String[] to = { RECIPIENT }; // list of recipient email addresses
        String subject = "Hotspots Area Detected";
        String body = "Bla bla bla...";


        sendFromGMail(from, pass, to, subject, body);
    }

    private static void sendFromGMail(String from, String pass, String[] to, String subject, String body) {
        Properties props = System.getProperties();
        String host = "smtp.gmail.com";
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.user", from);
        props.put("mail.smtp.password", pass);
        props.put("mail.smtp.port", "587");
        props.put("mail.smtp.auth", "true");

        Session session = Session.getDefaultInstance(props);
        MimeMessage message = new MimeMessage(session);

        try {
            message.setFrom(new InternetAddress(from));
            InternetAddress[] toAddress = new InternetAddress[to.length];

            // To get the array of addresses
            for( int i = 0; i < to.length; i++ ) {
                toAddress[i] = new InternetAddress(to[i]);
            }

            for( int i = 0; i < toAddress.length; i++) {
                message.addRecipient(Message.RecipientType.TO, toAddress[i]);
            }

            message.setSubject(subject);
            message.setText(body);
            Transport transport = session.getTransport("smtp");
            transport.connect(host, from, pass);
            transport.sendMessage(message, message.getAllRecipients());
            transport.close();
        }
        catch (AddressException ae) {
            ae.printStackTrace();
        }
        catch (MessagingException me) {
            me.printStackTrace();
        }
    }

但是当我通过cron作业调用Python脚本时,我在日志中遇到以下错误:

Mar 10 09:50:01 ubuntu-dashboard-service CRON[3586]: (tripwire) CMD (/usr/bin/python /home/tripwire/email/notify.py)
Mar 10 09:50:06 ubuntu-dashboard-service CRON[3584]: (CRON) error (grandchild #3586 failed with exit status 1)
Mar 10 09:50:07 ubuntu-dashboard-service sendmail[3589]: s2A6o643003589: from=tripwire, size=783, class=0, nrcpts=1, msgid=<201403100650.s2A6o643003589@ubuntu-dashboard-service.qf.org.qa>, relay=tripwire@localhost
Mar 10 09:50:07 ubuntu-dashboard-service sm-mta[3590]: s2A6o79O003590: from=<tripwire@ubuntu-dashboard-service.qf.org.qa>, size=1131, class=0, nrcpts=1, msgid=<201403100650.s2A6o643003589@ubuntu-dashboard-service.qf.org.qa>, proto=ESMTP, daemon=MTA-v4, relay=localhost [127.0.0.1]
Mar 10 09:50:07 ubuntu-dashboard-service sendmail[3589]: s2A6o643003589: to=tripwire, ctladdr=tripwire (1000/1000), delay=00:00:01, xdelay=00:00:00, mailer=relay, pri=30783, relay=[127.0.0.1] [127.0.0.1], dsn=2.0.0, stat=Sent (s2A6o79O003590 Message accepted for delivery)
Mar 10 09:50:07 ubuntu-dashboard-service sm-mta[3592]: s2A6o79O003590: to=<tripwire@ubuntu-dashboard-service.qf.org.qa>, delay=00:00:00, xdelay=00:00:00, mailer=esmtp, pri=121131, relay=ubuntu-dashboard-service.qf.org.qa. [10.153.33.140], dsn=4.0.0, stat=Deferred: Connection refused by ubuntu-dashboard-service.qf.org.qa.

电子邮件,Python和Java类以及crontab似乎工作得很好......所以必须是允许从crontab发送电子邮件的东西。有什么不同吗?

我在Ubuntu 11.04上运行整个事情。

谢谢,

修改

直接在python中添加了电子邮件部分,以避免cronjob问题。它有效!

1 个答案:

答案 0 :(得分:0)

直接在python中添加了电子邮件部分,以避免cronjob问题。它有效!