2016-01-08 22 views
0

您好我有web应用程序java使用球衣,我部署我的程序与tomcat和连接到MySQL数据库...我有一个类负责发送邮件给用户。 localhost上一流的做工精细和邮件发送,但AFER我部署的方案,并上传到服务器犯规向用户发送邮件...我怎么能从web应用程序发送邮件

这个IHS的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

您的主机是否运行邮件服务器?如果您想进行一些测试,我知道有一种方法可以通过您的Gmail帐户管理电子邮件。至于制作推出,你需要一个邮件服务器来连接。 –

+0

可能是您的服务器无法访问SMTP服务器或某个端口被阻止的简单情况。 –

+0

是否有错误?您的网络是否需要网关进行此类连接? – Perdomoff

回答

0

如您所述,此代码在一台机器上运行,而不在另一台机器上运行,似乎更像服务器配置问题而不是编程。

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

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

+0

添加到我的代码这是我从browesr得到: –

+0

HTTP状态500 - com.sun.mail.util .MailConnectException:无法连接到主机,端口:smtp.gmail.com,587;超时-1; –

+0

java.lang.RuntimeException:com.sun.mail.util.MailConnectException:无法连接到主机,端口:smtp.gmail.com,587;超时-1; 嵌套的异常是: –