我如何从Web应用程序发送邮件

时间:2016-01-08 22:20:14

标签: java email web-applications jersey port

我有使用jersey的web应用程序java,我使用tomcat部署我的程序并连接到mysql数据库...我有一个类充电发送邮件给用户。 在localhost上,该类工作正常,邮件已发送,但是我部署了程序并上传到服务器,邮件不会发送给用户......

这就是clas:

 public static boolean SendMail(String from, String password, String message, String to[]) {
    String host = "smtp.gmail.com";
    Properties properties = System.getProperties();
    properties.put("mail.smtp.starttls.enable", "true");
    properties.put("mail.smtp.host", host);
    properties.put("mail.smtp.user", from);
    properties.put("mail.smtp.password", password);
    properties.put("mail.smtp.port", 587);
    properties.put("mail.smtp.auth", "true");
    Session session = Session.getDefaultInstance(properties, null);
    MimeMessage mimeMessage = new MimeMessage(session);
    try {
        mimeMessage.setFrom(new InternetAddress(from));
        InternetAddress[] toAddress = new InternetAddress[to.length];
        for (int index = 0; index < to.length; index++) {
            toAddress[index] = new InternetAddress(to[index]);
        }
        for (int index = 0; index < toAddress.length; index++) {
            mimeMessage.addRecipient(Message.RecipientType.TO, toAddress[index]);
        }
        mimeMessage.setSubject("email From linkeride");
        mimeMessage.setText(message);
        Transport transport = session.getTransport("smtp");
        transport.connect(host, from, password);
        transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());
        transport.close();
        return true;

    } catch (MessagingException me) {
        me.printStackTrace();
    }
    return false;
}

我可能有人可以告诉我问题是什么?

1 个答案:

答案 0 :(得分:0)

如你所说,这段代码可以在一台机器上运行而不在另一台机器上,看起来更像是服务器配置问题,而不是编程。

properties.put("mail.smtp.debug", "true"); 

尝试为SMTP启用调试模式,因此在日志中,您将完全了解您的邮件发送失败的步骤。