【问题标题】:Error sending Email (Gmail) from crontab but works from Java class从 crontab 发送电子邮件 (Gmail) 时出错,但适用于 Java 类
【发布时间】:2014-04-13 03:51:14
【问题描述】:

如果我从 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 问题。有效!

【问题讨论】:

  • 请将其添加为答案并接受它,这样问题就不会显示为未解决。

标签: java python gmail crontab


【解决方案1】:

将电子邮件部分直接添加到 python 以避免 cronjob 问题。有效!

【讨论】:

    猜你喜欢
    • 2021-10-09
    • 1970-01-01
    • 2014-09-24
    • 1970-01-01
    • 2016-06-03
    • 2019-05-06
    • 2012-03-16
    • 2014-01-04
    • 2016-11-30
    相关资源
    最近更新 更多